Skip to content

Commit

Permalink
Fix #815, for specific case of ObjectNode deserializer access.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 5, 2015
1 parent ea2c9a4 commit 6456a9a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.databind.node;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand Down Expand Up @@ -320,6 +322,7 @@ public JsonNode set(String fieldName, JsonNode value)
*
* @since 2.1
*/
@JsonIgnore // work-around for [databind#815]
public JsonNode setAll(Map<String,? extends JsonNode> properties)
{
for (Map.Entry<String,? extends JsonNode> en : properties.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.introspect.TestNamingStrategyCustom.PersonBean;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* Unit tests to verify functioning of
Expand All @@ -23,12 +24,6 @@
*/
public class TestNamingStrategyStd extends BaseMapTest
{
/*
/**********************************************************
/* Helper types
/**********************************************************
*/

@JsonPropertyOrder({"www", "some_url", "some_uris"})
static class Acronyms
{
Expand Down Expand Up @@ -95,7 +90,12 @@ static class BoringBean {
public String firstName = "Bob";
public String lastName = "Burger";
}


public static class ClassWithObjectNodeField {
public String id;
public ObjectNode json;
}

/*
/**********************************************************
/* Set up
Expand Down Expand Up @@ -154,14 +154,14 @@ static class BoringBean {
{"_Bars", "bars" }
});

private ObjectMapper mapper;
private ObjectMapper _lcWithUndescoreMapper;

@Override
public void setUp() throws Exception
{
super.setUp();
mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
_lcWithUndescoreMapper = new ObjectMapper();
_lcWithUndescoreMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
}

/*
Expand Down Expand Up @@ -190,11 +190,11 @@ public void testLowerCaseStrategyStandAlone()
public void testLowerCaseTranslations() throws Exception
{
// First serialize
String json = mapper.writeValueAsString(new PersonBean("Joe", "Sixpack", 42));
String json = _lcWithUndescoreMapper.writeValueAsString(new PersonBean("Joe", "Sixpack", 42));
assertEquals("{\"first_name\":\"Joe\",\"last_name\":\"Sixpack\",\"age\":42}", json);

// then deserialize
PersonBean result = mapper.readValue(json, PersonBean.class);
PersonBean result = _lcWithUndescoreMapper.readValue(json, PersonBean.class);
assertEquals("Joe", result.firstName);
assertEquals("Sixpack", result.lastName);
assertEquals(42, result.age);
Expand All @@ -203,11 +203,11 @@ public void testLowerCaseTranslations() throws Exception
public void testLowerCaseAcronymsTranslations() throws Exception
{
// First serialize
String json = mapper.writeValueAsString(new Acronyms("world wide web", "http://jackson.codehaus.org", "/path1/,/path2/"));
String json = _lcWithUndescoreMapper.writeValueAsString(new Acronyms("world wide web", "http://jackson.codehaus.org", "/path1/,/path2/"));
assertEquals("{\"www\":\"world wide web\",\"some_url\":\"http://jackson.codehaus.org\",\"some_uris\":\"/path1/,/path2/\"}", json);

// then deserialize
Acronyms result = mapper.readValue(json, Acronyms.class);
Acronyms result = _lcWithUndescoreMapper.readValue(json, Acronyms.class);
assertEquals("world wide web", result.WWW);
assertEquals("http://jackson.codehaus.org", result.someURL);
assertEquals("/path1/,/path2/", result.someURIs);
Expand All @@ -216,11 +216,11 @@ public void testLowerCaseAcronymsTranslations() throws Exception
public void testLowerCaseOtherNonStandardNamesTranslations() throws Exception
{
// First serialize
String json = mapper.writeValueAsString(new OtherNonStandardNames("Results", "_User", "___", "$User"));
String json = _lcWithUndescoreMapper.writeValueAsString(new OtherNonStandardNames("Results", "_User", "___", "$User"));
assertEquals("{\"results\":\"Results\",\"user\":\"_User\",\"__\":\"___\",\"$_user\":\"$User\"}", json);

// then deserialize
OtherNonStandardNames result = mapper.readValue(json, OtherNonStandardNames.class);
OtherNonStandardNames result = _lcWithUndescoreMapper.readValue(json, OtherNonStandardNames.class);
assertEquals("Results", result.Results);
assertEquals("_User", result._User);
assertEquals("___", result.___);
Expand All @@ -230,11 +230,11 @@ public void testLowerCaseOtherNonStandardNamesTranslations() throws Exception
public void testLowerCaseUnchangedNames() throws Exception
{
// First serialize
String json = mapper.writeValueAsString(new UnchangedNames("from_user", "_user", "from$user", "from7user", "_x"));
String json = _lcWithUndescoreMapper.writeValueAsString(new UnchangedNames("from_user", "_user", "from$user", "from7user", "_x"));
assertEquals("{\"from_user\":\"from_user\",\"user\":\"_user\",\"from$user\":\"from$user\",\"from7user\":\"from7user\",\"x\":\"_x\"}", json);

// then deserialize
UnchangedNames result = mapper.readValue(json, UnchangedNames.class);
UnchangedNames result = _lcWithUndescoreMapper.readValue(json, UnchangedNames.class);
assertEquals("from_user", result.from_user);
assertEquals("_user", result._user);
assertEquals("from$user", result.from$user);
Expand Down Expand Up @@ -274,7 +274,7 @@ public void testPascalCaseStandAlone()
}

/**
* [Issue#428]
* For [databind#428]
*/
public void testIssue428PascalWithOverrides() throws Exception {

Expand All @@ -288,15 +288,32 @@ public void testIssue428PascalWithOverrides() throws Exception {
}

/**
* For [Issue#461]
* For [databind#461]
*/
public void testSimpleLowerCase() throws Exception
{
final BoringBean input = new BoringBean();
ObjectMapper m = new ObjectMapper();
ObjectMapper m = objectMapper();

assertEquals(aposToQuotes("{'firstname':'Bob','lastname':'Burger'}"),
m.writeValueAsString(input));
}
}

/**
* Test [databind#815], problems with ObjectNode, naming strategy
*/
public void testNamingWithObjectNode() throws Exception
{
ObjectMapper m = new ObjectMapper();
m.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE);
ClassWithObjectNodeField result =
m.readValue(
"{ \"id\": \"1\", \"json\": { \"foo\": \"bar\", \"baz\": \"bing\" } }",
ClassWithObjectNodeField.class);
assertNotNull(result);
assertEquals("1", result.id);
assertNotNull(result.json);
assertEquals(2, result.json.size());
assertEquals("bing", result.json.path("baz").asText());
}
}

0 comments on commit 6456a9a

Please sign in to comment.