Skip to content

Commit

Permalink
Implement #787
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 12, 2015
1 parent 0abef48 commit a936f43
Showing 26 changed files with 194 additions and 181 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -36,6 +36,8 @@ Project: jackson-databind
#769: Fix `JacksonAnnotationIntrospector.findDeserializer` to return `Object` (as per
`AnnotationIntrospector`); similarly for other `findXxx(De)Serializer(...)` methods
#781: Support handling of `@JsonProperty.required` for Creator methods
#787: Add `ObjectMapper setFilterProvider(FilterProvider)` to allow chaining
(suggested by rgoldberg@githin)
- Remove old cglib compatibility tests; cause problems in Eclipse

2.5.4 (not yet released)
257 changes: 134 additions & 123 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -136,7 +136,7 @@ protected ObjectReader objectReader() {
}

protected ObjectReader objectReader(Class<?> cls) {
return SHARED_MAPPER.reader(cls);
return SHARED_MAPPER.readerFor(cls);
}

/*
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public void testObjectWriter() throws IOException

public void testObjectReader() throws IOException
{
ObjectReader origReader = MAPPER.reader(MyPojo.class);
ObjectReader origReader = MAPPER.readerFor(MyPojo.class);
final String JSON = "{\"x\":1,\"y\":2}";
MyPojo p1 = origReader.readValue(JSON);
assertEquals(2, p1.y);
14 changes: 7 additions & 7 deletions src/test/java/com/fasterxml/jackson/databind/TestRootName.java
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public void testRootViaWriterAndReader() throws Exception
ObjectMapper mapper = rootMapper();
String json = mapper.writer().writeValueAsString(new Bean());
assertEquals("{\"rudy\":{\"a\":3}}", json);
Bean bean = mapper.reader(Bean.class).readValue(json);
Bean bean = mapper.readerFor(Bean.class).readValue(json);
assertNotNull(bean);
}

@@ -67,14 +67,14 @@ public void testReconfiguringOfWrapping() throws Exception
Bean result = mapper.readValue(jsonUnwrapped, Bean.class);
assertNotNull(result);
try { // must not have extra wrapping
result = mapper.reader(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
result = mapper.readerFor(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
.readValue(jsonUnwrapped);
fail("Should have failed");
} catch (JsonMappingException e) {
verifyException(e, "Root name 'a'");
}
// except wrapping may be expected:
result = mapper.reader(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
result = mapper.readerFor(Bean.class).with(DeserializationFeature.UNWRAP_ROOT_VALUE)
.readValue(jsonWrapped);
assertNotNull(result);
}
@@ -87,7 +87,7 @@ public void testRootUsingExplicitConfig() throws Exception
String json = writer.writeValueAsString(new Bean());
assertEquals("{\"wrapper\":{\"a\":3}}", json);

ObjectReader reader = mapper.reader(Bean.class).withRootName("wrapper");
ObjectReader reader = mapper.readerFor(Bean.class).withRootName("wrapper");
Bean bean = reader.readValue(json);
assertNotNull(bean);

@@ -102,16 +102,16 @@ public void testRootUsingExplicitConfig() throws Exception
json = wrapping.writer().withoutRootName().writeValueAsString(new Bean());
assertEquals("{\"a\":3}", json);

bean = wrapping.reader(Bean.class).withRootName("").readValue(json);
bean = wrapping.readerFor(Bean.class).withRootName("").readValue(json);
assertNotNull(bean);
assertEquals(3, bean.a);

bean = wrapping.reader(Bean.class).withoutRootName().readValue("{\"a\":4}");
bean = wrapping.readerFor(Bean.class).withoutRootName().readValue("{\"a\":4}");
assertNotNull(bean);
assertEquals(4, bean.a);

// and back to defaults
bean = wrapping.reader(Bean.class).readValue("{\"rudy\":{\"a\":7}}");
bean = wrapping.readerFor(Bean.class).readValue("{\"rudy\":{\"a\":7}}");
assertNotNull(bean);
assertEquals(7, bean.a);
}
Original file line number Diff line number Diff line change
@@ -50,13 +50,13 @@ static class TestPOJO
public void testSimplePerCall() throws Exception
{
final String INPUT = aposToQuotes("[{'value':'a'},{'value':'b'}]");
TestPOJO[] pojos = MAPPER.reader(TestPOJO[].class).readValue(INPUT);
TestPOJO[] pojos = MAPPER.readerFor(TestPOJO[].class).readValue(INPUT);
assertEquals(2, pojos.length);
assertEquals("a/0", pojos[0].value);
assertEquals("b/1", pojos[1].value);

// and verify that state does not linger
TestPOJO[] pojos2 = MAPPER.reader(TestPOJO[].class).readValue(INPUT);
TestPOJO[] pojos2 = MAPPER.readerFor(TestPOJO[].class).readValue(INPUT);
assertEquals(2, pojos2.length);
assertEquals("a/0", pojos2[0].value);
assertEquals("b/1", pojos2[1].value);
@@ -65,13 +65,13 @@ public void testSimplePerCall() throws Exception
public void testSimpleDefaults() throws Exception
{
final String INPUT = aposToQuotes("{'value':'x'}");
TestPOJO pojo = MAPPER.reader(TestPOJO.class)
TestPOJO pojo = MAPPER.readerFor(TestPOJO.class)
.withAttribute(KEY, Integer.valueOf(3))
.readValue(INPUT);
assertEquals("x/3", pojo.value);

// as above, should not carry on state
TestPOJO pojo2 = MAPPER.reader(TestPOJO.class)
TestPOJO pojo2 = MAPPER.readerFor(TestPOJO.class)
.withAttribute(KEY, Integer.valueOf(5))
.readValue(INPUT);
assertEquals("x/5", pojo2.value);
@@ -80,7 +80,7 @@ public void testSimpleDefaults() throws Exception
public void testHierarchic() throws Exception
{
final String INPUT = aposToQuotes("[{'value':'x'},{'value':'y'}]");
ObjectReader r = MAPPER.reader(TestPOJO[].class).withAttribute(KEY, Integer.valueOf(2));
ObjectReader r = MAPPER.readerFor(TestPOJO[].class).withAttribute(KEY, Integer.valueOf(2));
TestPOJO[] pojos = r.readValue(INPUT);
assertEquals(2, pojos.length);
assertEquals("x/2", pojos[0].value);
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ public FascistPoint(@JsonProperty(value="x", required=true) int x,
}
}

private final ObjectReader POINT_READER = objectMapper().reader(FascistPoint.class);
private final ObjectReader POINT_READER = objectMapper().readerFor(FascistPoint.class);

public void testRequiredAnnotatedParam() throws Exception
{
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ public void testManualPolymorphicCatWithReorder() throws Exception
public void testManualPolymorphicWithNumbered() throws Exception
{
final ObjectWriter w = MAPPER.writerFor(AbstractRoot.class);
final ObjectReader r = MAPPER.reader(AbstractRoot.class);
final ObjectReader r = MAPPER.readerFor(AbstractRoot.class);

AbstractRoot input = AbstractRoot.make(1, "oh hai!");
String json = w.writeValueAsString(input);
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ void setB(String b) {
public void testValueUpdateWithCreator() throws Exception
{
Bean bean = new Bean("abc", "def");
new ObjectMapper().reader(Bean.class).withValueToUpdate(bean).readValue("{\"a\":\"ghi\",\"b\":\"jkl\"}");
new ObjectMapper().readerFor(Bean.class).withValueToUpdate(bean).readValue("{\"a\":\"ghi\",\"b\":\"jkl\"}");
assertEquals("ghi", bean.getA());
assertEquals("jkl", bean.getB());
}
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public void testSimpleIgnore() throws Exception

public void testFailOnIgnore() throws Exception
{
ObjectReader r = MAPPER.reader(NoYOrZ.class);
ObjectReader r = MAPPER.readerFor(NoYOrZ.class);

// First, fine to get "x":
NoYOrZ result = r.readValue(aposToQuotes("{'x':3}"));
Original file line number Diff line number Diff line change
@@ -269,7 +269,7 @@ public void testPOJOFromEmptyString() throws Exception
assertValidLocation(e.getLocation());
}
// should be ok to enable dynamically
ObjectReader r = MAPPER.reader(Bean.class)
ObjectReader r = MAPPER.readerFor(Bean.class)
.with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
Bean result = r.readValue(quote(""));
assertNull(result);
@@ -291,7 +291,7 @@ public void testPOJOFromEmptyArray() throws Exception
}

// should be ok to enable dynamically:
ObjectReader r = MAPPER.reader(Bean.class)
ObjectReader r = MAPPER.readerFor(Bean.class)
.with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
Bean result = r.readValue(JSON);
assertNull(result);
@@ -318,7 +318,7 @@ public void testCaseInsensitiveDeserialization() throws Exception
// Definitely not OK to enable dynamically - the BeanPropertyMap (which is the consumer of this particular feature) gets cached.
mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
ObjectReader r = mapper.reader(Issue476Bean.class);
ObjectReader r = mapper.readerFor(Issue476Bean.class);
Issue476Bean result = r.readValue(JSON);
assertEquals(result.value1.name, "fruit");
assertEquals(result.value1.value, "apple");
Original file line number Diff line number Diff line change
@@ -390,7 +390,7 @@ public void testCustomDateWithAnnotation() throws Exception

// 27-Mar-2014, tatu: Let's verify that changing Locale won't break it;
// either via context Locale
result = MAPPER.reader(DateAsStringBean.class)
result = MAPPER.readerFor(DateAsStringBean.class)
.with(Locale.GERMANY)
.readValue(INPUT);
assertNotNull(result);
@@ -405,7 +405,7 @@ public void testCustomDateWithAnnotation() throws Exception
assertEquals(25, c.get(Calendar.DAY_OF_MONTH));

// or, via annotations
DateAsStringBeanGermany result2 = MAPPER.reader(DateAsStringBeanGermany.class).readValue(INPUT);
DateAsStringBeanGermany result2 = MAPPER.readerFor(DateAsStringBeanGermany.class).readValue(INPUT);
assertNotNull(result2);
assertNotNull(result2.date);
l = result2.date.getTime();
Original file line number Diff line number Diff line change
@@ -275,7 +275,7 @@ public void testNumbersToEnums() throws Exception
assertSame(TestEnum.RULES, value);

// but can also be changed to errors:
ObjectReader r = MAPPER.reader(TestEnum.class)
ObjectReader r = MAPPER.readerFor(TestEnum.class)
.with(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
try {
value = r.readValue("1");
Original file line number Diff line number Diff line change
@@ -312,7 +312,7 @@ public void testMapFromEmptyArray() throws Exception
verifyException(e, "START_ARRAY token");
}
// should be ok to enable dynamically:
ObjectReader r = MAPPER.reader(Map.class)
ObjectReader r = MAPPER.readerFor(Map.class)
.with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);

Map<?,?> result = r.readValue(JSON);
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ public void testCustomRootNulls() throws Exception
assertEquals("funny", str);

// as well as via ObjectReader
ObjectReader reader = mapper.reader(String.class);
ObjectReader reader = mapper.readerFor(String.class);
str = reader.readValue("null");
assertNotNull(str);
assertEquals("funny", str);
@@ -67,7 +67,7 @@ public void testListOfNulls() throws Exception
assertEquals(list.get(0), deser.get(0));

// as well as via ObjectReader
ObjectReader reader = mapper.reader(type);
ObjectReader reader = mapper.readerFor(type);
deser = reader.readValue("[null]");
assertNotNull(deser);
assertEquals(1, deser.size());
@@ -90,7 +90,7 @@ public void testMapOfNulls() throws Exception
assertEquals("funny", deser.get("key"));

// as well as via ObjectReader
ObjectReader reader = mapper.reader(type);
ObjectReader reader = mapper.readerFor(type);
deser = reader.readValue("{\"key\":null}");
assertNotNull(deser);
assertEquals(1, deser.size());
Original file line number Diff line number Diff line change
@@ -422,7 +422,7 @@ public void testBase64Variants() throws Exception
Assert.assertArrayEquals(INPUT, MAPPER.readValue(
quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="),
byte[].class));
ObjectReader reader = MAPPER.reader(byte[].class);
ObjectReader reader = MAPPER.readerFor(byte[].class);
Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MIME_NO_LINEFEEDS).readValue(
quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="
)));
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ public void testUnknownHandlingIgnoreWithHandlerAndObjectReader()
{
ObjectMapper mapper = new ObjectMapper();
mapper.clearProblemHandlers();
TestBean result = mapper.reader(TestBean.class).withHandler(new MyHandler()).readValue(new StringReader(JSON_UNKNOWN_FIELD));
TestBean result = mapper.readerFor(TestBean.class).withHandler(new MyHandler()).readValue(new StringReader(JSON_UNKNOWN_FIELD));
assertNotNull(result);
assertEquals(1, result._a);
assertEquals(-1, result._b);
Original file line number Diff line number Diff line change
@@ -115,48 +115,48 @@ static class ImplFor656 extends BaseFor656 {

public void testDeserializationWithObject() throws Exception
{
Inter inter = MAPPER.reader(Inter.class).readValue("{\"type\": \"mine\", \"blah\": [\"a\", \"b\", \"c\"]}");
Inter inter = MAPPER.readerFor(Inter.class).readValue("{\"type\": \"mine\", \"blah\": [\"a\", \"b\", \"c\"]}");
assertTrue(inter instanceof MyInter);
assertFalse(inter instanceof LegacyInter);
assertEquals(Arrays.asList("a", "b", "c"), ((MyInter) inter).blah);
}

public void testDeserializationWithString() throws Exception
{
Inter inter = MAPPER.reader(Inter.class).readValue("\"a,b,c,d\"");
Inter inter = MAPPER.readerFor(Inter.class).readValue("\"a,b,c,d\"");
assertTrue(inter instanceof LegacyInter);
assertEquals(Arrays.asList("a", "b", "c", "d"), ((MyInter) inter).blah);
}

public void testDeserializationWithArray() throws Exception
{
Inter inter = MAPPER.reader(Inter.class).readValue("[\"a\", \"b\", \"c\", \"d\"]");
Inter inter = MAPPER.readerFor(Inter.class).readValue("[\"a\", \"b\", \"c\", \"d\"]");
assertTrue(inter instanceof LegacyInter);
assertEquals(Arrays.asList("a", "b", "c", "d"), ((MyInter) inter).blah);
}

public void testDeserializationWithArrayOfSize2() throws Exception
{
Inter inter = MAPPER.reader(Inter.class).readValue("[\"a\", \"b\"]");
Inter inter = MAPPER.readerFor(Inter.class).readValue("[\"a\", \"b\"]");
assertTrue(inter instanceof LegacyInter);
assertEquals(Arrays.asList("a", "b"), ((MyInter) inter).blah);
}

// [Databind#148]
public void testDefaultAsNoClass() throws Exception
{
Object ob = MAPPER.reader(DefaultWithNoClass.class).readValue("{ }");
Object ob = MAPPER.readerFor(DefaultWithNoClass.class).readValue("{ }");
assertNull(ob);
ob = MAPPER.reader(DefaultWithNoClass.class).readValue("{ \"bogus\":3 }");
ob = MAPPER.readerFor(DefaultWithNoClass.class).readValue("{ \"bogus\":3 }");
assertNull(ob);
}

// same, with 2.5 and Void.class
public void testDefaultAsVoid() throws Exception
{
Object ob = MAPPER.reader(DefaultWithVoidAsDefault.class).readValue("{ }");
Object ob = MAPPER.readerFor(DefaultWithVoidAsDefault.class).readValue("{ }");
assertNull(ob);
ob = MAPPER.reader(DefaultWithVoidAsDefault.class).readValue("{ \"bogus\":3 }");
ob = MAPPER.readerFor(DefaultWithVoidAsDefault.class).readValue("{ \"bogus\":3 }");
assertNull(ob);
}

Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ public void testIssue353() throws Exception

SavedCookie savedCookie = new SavedCookie("key", "v");
String json = mapper.writeValueAsString(savedCookie);
SavedCookie out = mapper.reader(SavedCookie.class).readValue(json);
SavedCookie out = mapper.readerFor(SavedCookie.class).readValue(json);

assertEquals("key", out.name);
assertEquals("v", out.value);
Original file line number Diff line number Diff line change
@@ -345,7 +345,7 @@ public void testUnresolvedForwardReference()
// [databind#299]: Allow unresolved ids to become nulls
public void testUnresolvableAsNull() throws Exception
{
IdWrapper w = MAPPER.reader(IdWrapper.class)
IdWrapper w = MAPPER.readerFor(IdWrapper.class)
.without(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS)
.readValue(aposToQuotes("{'node':123}"));
assertNotNull(w);
@@ -433,7 +433,7 @@ public void testCustomPoolResolver() throws Exception
pool.put(5, new WithCustomResolution(5, 5));
ContextAttributes attrs = MAPPER.getDeserializationConfig().getAttributes().withSharedAttribute(POOL_KEY, pool);
String content = "{\"data\":[1,2,3,4,5]}";
CustomResolutionWrapper wrapper = MAPPER.reader(CustomResolutionWrapper.class).with(attrs).readValue(content);
CustomResolutionWrapper wrapper = MAPPER.readerFor(CustomResolutionWrapper.class).with(attrs).readValue(content);
assertFalse(wrapper.data.isEmpty());
for (WithCustomResolution ob : wrapper.data) {
assertSame(pool.get(ob.id), ob);
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ public void testParserFeatures() throws Exception
{
final String JSON = "[ /* foo */ 7 ]";
// default won't accept comments, let's change that:
ObjectReader reader = MAPPER.reader(int[].class)
ObjectReader reader = MAPPER.readerFor(int[].class)
.with(JsonParser.Feature.ALLOW_COMMENTS);

int[] value = reader.readValue(JSON);
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ static class Bean {
public void testRootBeans() throws Exception
{
final String JSON = aposToQuotes("{'a':3} {'b':5}");
MappingIterator<Bean> it = MAPPER.reader(Bean.class).readValues(JSON);
MappingIterator<Bean> it = MAPPER.readerFor(Bean.class).readValues(JSON);
// First one should be fine
assertTrue(it.hasNextValue());
Bean bean = it.nextValue();
Loading

0 comments on commit a936f43

Please sign in to comment.