11
11
import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
12
12
import com .fasterxml .jackson .databind .annotation .JsonNaming ;
13
13
import com .fasterxml .jackson .databind .introspect .TestNamingStrategyCustom .PersonBean ;
14
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
14
15
15
16
/**
16
17
* Unit tests to verify functioning of
23
24
*/
24
25
public class TestNamingStrategyStd extends BaseMapTest
25
26
{
26
- /*
27
- /**********************************************************
28
- /* Helper types
29
- /**********************************************************
30
- */
31
-
32
27
@ JsonPropertyOrder ({"www" , "some_url" , "some_uris" })
33
28
static class Acronyms
34
29
{
@@ -95,7 +90,12 @@ static class BoringBean {
95
90
public String firstName = "Bob" ;
96
91
public String lastName = "Burger" ;
97
92
}
98
-
93
+
94
+ public static class ClassWithObjectNodeField {
95
+ public String id ;
96
+ public ObjectNode json ;
97
+ }
98
+
99
99
/*
100
100
/**********************************************************
101
101
/* Set up
@@ -154,14 +154,14 @@ static class BoringBean {
154
154
{"_Bars" , "bars" }
155
155
});
156
156
157
- private ObjectMapper mapper ;
157
+ private ObjectMapper _lcWithUndescoreMapper ;
158
158
159
159
@ Override
160
160
public void setUp () throws Exception
161
161
{
162
162
super .setUp ();
163
- mapper = new ObjectMapper ();
164
- mapper .setPropertyNamingStrategy (PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES );
163
+ _lcWithUndescoreMapper = new ObjectMapper ();
164
+ _lcWithUndescoreMapper .setPropertyNamingStrategy (PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES );
165
165
}
166
166
167
167
/*
@@ -190,11 +190,11 @@ public void testLowerCaseStrategyStandAlone()
190
190
public void testLowerCaseTranslations () throws Exception
191
191
{
192
192
// First serialize
193
- String json = mapper .writeValueAsString (new PersonBean ("Joe" , "Sixpack" , 42 ));
193
+ String json = _lcWithUndescoreMapper .writeValueAsString (new PersonBean ("Joe" , "Sixpack" , 42 ));
194
194
assertEquals ("{\" first_name\" :\" Joe\" ,\" last_name\" :\" Sixpack\" ,\" age\" :42}" , json );
195
195
196
196
// then deserialize
197
- PersonBean result = mapper .readValue (json , PersonBean .class );
197
+ PersonBean result = _lcWithUndescoreMapper .readValue (json , PersonBean .class );
198
198
assertEquals ("Joe" , result .firstName );
199
199
assertEquals ("Sixpack" , result .lastName );
200
200
assertEquals (42 , result .age );
@@ -203,11 +203,11 @@ public void testLowerCaseTranslations() throws Exception
203
203
public void testLowerCaseAcronymsTranslations () throws Exception
204
204
{
205
205
// First serialize
206
- String json = mapper .writeValueAsString (new Acronyms ("world wide web" , "http://jackson.codehaus.org" , "/path1/,/path2/" ));
206
+ String json = _lcWithUndescoreMapper .writeValueAsString (new Acronyms ("world wide web" , "http://jackson.codehaus.org" , "/path1/,/path2/" ));
207
207
assertEquals ("{\" www\" :\" world wide web\" ,\" some_url\" :\" http://jackson.codehaus.org\" ,\" some_uris\" :\" /path1/,/path2/\" }" , json );
208
208
209
209
// then deserialize
210
- Acronyms result = mapper .readValue (json , Acronyms .class );
210
+ Acronyms result = _lcWithUndescoreMapper .readValue (json , Acronyms .class );
211
211
assertEquals ("world wide web" , result .WWW );
212
212
assertEquals ("http://jackson.codehaus.org" , result .someURL );
213
213
assertEquals ("/path1/,/path2/" , result .someURIs );
@@ -216,11 +216,11 @@ public void testLowerCaseAcronymsTranslations() throws Exception
216
216
public void testLowerCaseOtherNonStandardNamesTranslations () throws Exception
217
217
{
218
218
// First serialize
219
- String json = mapper .writeValueAsString (new OtherNonStandardNames ("Results" , "_User" , "___" , "$User" ));
219
+ String json = _lcWithUndescoreMapper .writeValueAsString (new OtherNonStandardNames ("Results" , "_User" , "___" , "$User" ));
220
220
assertEquals ("{\" results\" :\" Results\" ,\" user\" :\" _User\" ,\" __\" :\" ___\" ,\" $_user\" :\" $User\" }" , json );
221
221
222
222
// then deserialize
223
- OtherNonStandardNames result = mapper .readValue (json , OtherNonStandardNames .class );
223
+ OtherNonStandardNames result = _lcWithUndescoreMapper .readValue (json , OtherNonStandardNames .class );
224
224
assertEquals ("Results" , result .Results );
225
225
assertEquals ("_User" , result ._User );
226
226
assertEquals ("___" , result .___ );
@@ -230,11 +230,11 @@ public void testLowerCaseOtherNonStandardNamesTranslations() throws Exception
230
230
public void testLowerCaseUnchangedNames () throws Exception
231
231
{
232
232
// First serialize
233
- String json = mapper .writeValueAsString (new UnchangedNames ("from_user" , "_user" , "from$user" , "from7user" , "_x" ));
233
+ String json = _lcWithUndescoreMapper .writeValueAsString (new UnchangedNames ("from_user" , "_user" , "from$user" , "from7user" , "_x" ));
234
234
assertEquals ("{\" from_user\" :\" from_user\" ,\" user\" :\" _user\" ,\" from$user\" :\" from$user\" ,\" from7user\" :\" from7user\" ,\" x\" :\" _x\" }" , json );
235
235
236
236
// then deserialize
237
- UnchangedNames result = mapper .readValue (json , UnchangedNames .class );
237
+ UnchangedNames result = _lcWithUndescoreMapper .readValue (json , UnchangedNames .class );
238
238
assertEquals ("from_user" , result .from_user );
239
239
assertEquals ("_user" , result ._user );
240
240
assertEquals ("from$user" , result .from$user );
@@ -274,7 +274,7 @@ public void testPascalCaseStandAlone()
274
274
}
275
275
276
276
/**
277
- * [Issue #428]
277
+ * For [databind #428]
278
278
*/
279
279
public void testIssue428PascalWithOverrides () throws Exception {
280
280
@@ -288,15 +288,32 @@ public void testIssue428PascalWithOverrides() throws Exception {
288
288
}
289
289
290
290
/**
291
- * For [Issue #461]
291
+ * For [databind #461]
292
292
*/
293
293
public void testSimpleLowerCase () throws Exception
294
294
{
295
295
final BoringBean input = new BoringBean ();
296
- ObjectMapper m = new ObjectMapper ();
296
+ ObjectMapper m = objectMapper ();
297
297
298
298
assertEquals (aposToQuotes ("{'firstname':'Bob','lastname':'Burger'}" ),
299
299
m .writeValueAsString (input ));
300
300
}
301
- }
302
301
302
+ /**
303
+ * Test [databind#815], problems with ObjectNode, naming strategy
304
+ */
305
+ public void testNamingWithObjectNode () throws Exception
306
+ {
307
+ ObjectMapper m = new ObjectMapper ();
308
+ m .setPropertyNamingStrategy (PropertyNamingStrategy .LOWER_CASE );
309
+ ClassWithObjectNodeField result =
310
+ m .readValue (
311
+ "{ \" id\" : \" 1\" , \" json\" : { \" foo\" : \" bar\" , \" baz\" : \" bing\" } }" ,
312
+ ClassWithObjectNodeField .class );
313
+ assertNotNull (result );
314
+ assertEquals ("1" , result .id );
315
+ assertNotNull (result .json );
316
+ assertEquals (2 , result .json .size ());
317
+ assertEquals ("bing" , result .json .path ("baz" ).asText ());
318
+ }
319
+ }
0 commit comments