|
| 1 | +package com.fasterxml.jackson.jr.ob.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonParser; |
| 6 | +import com.fasterxml.jackson.jr.ob.JSON; |
| 7 | +import com.fasterxml.jackson.jr.ob.JSONObjectException; |
| 8 | +import com.fasterxml.jackson.jr.ob.TestBase; |
| 9 | +import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider; |
| 10 | +import com.fasterxml.jackson.jr.ob.api.ValueReader; |
| 11 | + |
| 12 | +public class CustomValueHandlersTest extends TestBase |
| 13 | +{ |
| 14 | + static class CustomValue { |
| 15 | + public int value; |
| 16 | + |
| 17 | + // 2nd arg just to avoid discovery |
| 18 | + public CustomValue(int v, boolean b) { |
| 19 | + // and to ensure it goes through constructor, add 1 |
| 20 | + value = v + 1; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static class CustomValueBean { |
| 25 | + public CustomValue custom; |
| 26 | + |
| 27 | + protected CustomValueBean() { } |
| 28 | + public CustomValueBean(int v) { |
| 29 | + custom = new CustomValue(v, false); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + static class CustomValueReader extends ValueReader { |
| 34 | + public CustomValueReader() { |
| 35 | + super(CustomValue.class); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Object read(JSONReader reader, JsonParser p) throws IOException { |
| 40 | + return new CustomValue(p.getIntValue(), true); |
| 41 | + } |
| 42 | + |
| 43 | + // Base class impl should be fine, although we'd use this for optimal |
| 44 | + /* |
| 45 | + @Override |
| 46 | + public Object readNext(JSONReader reader, JsonParser p) throws IOException { |
| 47 | + return new CustomValue(p.nextIntValue(-1), true); |
| 48 | + } |
| 49 | + */ |
| 50 | + } |
| 51 | + |
| 52 | + static class CustomReaders extends ReaderWriterProvider { |
| 53 | + @Override |
| 54 | + public ValueReader findBeanReader(JSONReader readContext, Class<?> type) { |
| 55 | + if (type.equals(CustomValue.class)) { |
| 56 | + return new CustomValueReader(); |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /* |
| 63 | + /********************************************************************** |
| 64 | + /* Test methdods |
| 65 | + /********************************************************************** |
| 66 | + */ |
| 67 | + |
| 68 | + public void testSimpleCustomReader() throws Exception |
| 69 | + { |
| 70 | + // First: without handler, will fail to map |
| 71 | + try { |
| 72 | + JSON.std.beanFrom(CustomValue.class, "123"); |
| 73 | + fail("Should not pass"); |
| 74 | + } catch (JSONObjectException e) { |
| 75 | + verifyException(e, ".CustomValue"); |
| 76 | + verifyException(e, "constructor to use"); |
| 77 | + } |
| 78 | + |
| 79 | + // then with custom, should be fine |
| 80 | + JSON json = JSON.std |
| 81 | + .with(new CustomReaders()); |
| 82 | + CustomValue v = json.beanFrom(CustomValue.class, "123"); |
| 83 | + assertEquals(124, v.value); |
| 84 | + |
| 85 | + // similarly with wrapper |
| 86 | + CustomValueBean bean = json.beanFrom(CustomValueBean.class, |
| 87 | + aposToQuotes("{ 'custom' : 137 }")); |
| 88 | + assertEquals(138, bean.custom.value); |
| 89 | + } |
| 90 | +} |
0 commit comments