| 1 |
package com.fasterxml.jackson.jr.ob.impl; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
|
| 5 |
import com.fasterxml.jackson.jr.ob.*; |
| 6 |
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier; |
| 7 |
|
| 8 |
public class POJODefinitionOverrideTest extends TestBase |
| 9 |
{ |
| 10 |
static class MyPropertyModifier extends ReaderWriterModifier |
| 11 |
{ |
| 12 |
private final String _toDrop; |
| 13 |
|
| 14 |
public MyPropertyModifier(String toDrop) { |
| 15 |
_toDrop = toDrop; |
| 16 |
} |
| 17 |
|
| 18 |
@Override |
| 19 |
public POJODefinition pojoDefinitionForDeserialization(JSONReader readContext, |
| 20 |
Class<?> pojoType) |
| 21 |
{ |
| 22 |
POJODefinition def = BeanPropertyIntrospector.instance().pojoDefinitionForDeserialization(readContext, pojoType); |
| 23 |
List<POJODefinition.Prop> newProps = new ArrayList<POJODefinition.Prop>(); |
| 24 |
for (POJODefinition.Prop prop : def.getProperties()) { |
| 25 |
if (!_toDrop.equals(prop.name)) { |
| 26 |
newProps.add(prop); |
| 27 |
} |
| 28 |
} |
| 29 |
return def.withProperties(newProps); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public POJODefinition pojoDefinitionForSerialization(JSONWriter writeContext, |
| 34 |
Class<?> pojoType) |
| 35 |
{ |
| 36 |
POJODefinition def = BeanPropertyIntrospector.instance().pojoDefinitionForSerialization(writeContext, pojoType); |
| 37 |
// and then reverse-order |
| 38 |
Map<String, POJODefinition.Prop> newProps = new TreeMap<String, POJODefinition.Prop>(Collections.reverseOrder()); |
| 39 |
for (POJODefinition.Prop prop : def.getProperties()) { |
| 40 |
newProps.put(prop.name, prop); |
| 41 |
} |
| 42 |
return def.withProperties(newProps.values()); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static class NoOpModifier extends ReaderWriterModifier { } |
| 47 |
|
| 48 |
public void testReadIgnoreProperty() throws Exception |
| 49 |
{ |
| 50 |
// verify default read first |
| 51 |
final String INPUT = aposToQuotes("{'first':'Bob','last':'Burger'}"); |
| 52 |
NameBean bean = JSON.std.beanFrom(NameBean.class, INPUT); |
| 53 |
assertEquals("Bob", bean.getFirst()); |
| 54 |
assertEquals("Burger", bean.getLast()); |
| 55 |
|
| 56 |
// but then use customized POJO introspection |
| 57 |
bean = jsonWithModifier(new MyPropertyModifier("last")) |
| 58 |
.beanFrom(NameBean.class, INPUT); |
| 59 |
assertEquals("Bob", bean.getFirst()); |
| 60 |
assertNull(bean.getLast()); |
| 61 |
|
| 62 |
// and last, to ensure no leakage of customizations |
| 63 |
bean = JSON.std.beanFrom(NameBean.class, INPUT); |
| 64 |
assertEquals("Bob", bean.getFirst()); |
| 65 |
assertEquals("Burger", bean.getLast()); |
| 66 |
} |
| 67 |
|
| 68 |
public void testModifierPairForReading() throws Exception |
| 69 |
{ |
| 70 |
final String INPUT = aposToQuotes("{'first':'Bob','last':'Burger'}"); |
| 71 |
NameBean bean = jsonWithModifiers(new NoOpModifier(), new MyPropertyModifier("last")) |
| 72 |
.beanFrom(NameBean.class, INPUT); |
| 73 |
assertEquals("Bob", bean.getFirst()); |
| 74 |
assertNull(bean.getLast()); |
| 75 |
|
| 76 |
// or with null |
| 77 |
bean = jsonWithModifiers(null, new MyPropertyModifier("last")) |
| 78 |
.beanFrom(NameBean.class, INPUT); |
| 79 |
assertEquals("Bob", bean.getFirst()); |
| 80 |
assertNull(bean.getLast()); |
| 81 |
} |
| 82 |
|
| 83 |
public void testWriteInReverseOrder() throws Exception |
| 84 |
{ |
| 85 |
// verify default write first |
| 86 |
final NameBean input = new NameBean("Bob", "Burger"); |
| 87 |
final String EXP_DEFAULT = aposToQuotes("{'first':'Bob','last':'Burger'}"); |
| 88 |
|
| 89 |
assertEquals(EXP_DEFAULT, JSON.std.asString(input)); |
| 90 |
|
| 91 |
// but then use customized POJO introspection |
| 92 |
String json = jsonWithModifier(new MyPropertyModifier("xxx")) |
| 93 |
.asString(input); |
| 94 |
assertEquals(aposToQuotes("{'last':'Burger','first':'Bob'}"), json); |
| 95 |
|
| 96 |
// and last, to ensure no leakage of customizations |
| 97 |
assertEquals(EXP_DEFAULT, JSON.std.asString(input)); |
| 98 |
} |
| 99 |
|
| 100 |
public void testModifierPairForWriting() throws Exception |
| 101 |
{ |
| 102 |
final NameBean input = new NameBean("Bill", "Burger"); |
| 103 |
|
| 104 |
String json = jsonWithModifiers(new NoOpModifier(), new MyPropertyModifier("xxx")) |
| 105 |
.asString(input); |
| 106 |
assertEquals(aposToQuotes("{'last':'Burger','first':'Bill'}"), json); |
| 107 |
|
| 108 |
// and nulls fine too wrt chaining |
| 109 |
json = jsonWithModifiers(null, new MyPropertyModifier("xxx")) |
| 110 |
.asString(input); |
| 111 |
assertEquals(aposToQuotes("{'last':'Burger','first':'Bill'}"), json); |
| 112 |
} |
| 113 |
} |