2929import com .fasterxml .jackson .core .JsonParser ;
3030import com .fasterxml .jackson .databind .JsonNode ;
3131import com .fasterxml .jackson .databind .ObjectMapper ;
32+ import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
3233import com .fasterxml .jackson .databind .node .POJONode ;
3334import com .fasterxml .jackson .databind .util .RawValue ;
3435
@@ -49,6 +50,7 @@ public class JsonRawValueExtendedTest {
4950 public void setup () throws Exception {
5051 mapper = new ObjectMapper ();
5152 mapper .configure (JsonParser .Feature .ALLOW_SINGLE_QUOTES , true );
53+ mapper .setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE );
5254 }
5355
5456 @ Test
@@ -58,13 +60,15 @@ public void testValueToJson() throws Exception {
5860 model .setFoo ("12341234" );
5961 model .setBar (1234 );
6062 model .setMetadata ("{\" foo\" :\" bar\" }" );
63+ model .setHogeMetadata ("{\" baz\" :\" qux\" }" );
6164 // exercise
6265 String actual = mapper .writeValueAsString (model );
6366 // verify
6467 with (actual )
6568 .assertThat ("$.foo" , is ("12341234" ))
6669 .assertThat ("$.bar" , is (1234 ))
67- .assertThat ("$.metadata.foo" , is ("bar" ));
70+ .assertThat ("$.metadata.foo" , is ("bar" ))
71+ .assertThat ("$.hoge_metadata.baz" , is ("qux" ));
6872 }
6973
7074 @ Test
@@ -74,6 +78,7 @@ public void testValueToTreeToValue() throws Exception {
7478 model .setFoo ("12341234" );
7579 model .setBar (1234 );
7680 model .setMetadata ("{\" foo\" :\" bar\" }" );
81+ model .setHogeMetadata ("{\" baz\" :\" qux\" }" );
7782
7883 // exercise 1: Value to Tree
7984 JsonNode actualNode = mapper .valueToTree (model );
@@ -89,6 +94,12 @@ public void testValueToTreeToValue() throws Exception {
8994 RawValue rawValue = (RawValue ) pojoNode .getPojo ();
9095 assertThat (rawValue .rawValue (), is ("{\" foo\" :\" bar\" }" ));
9196
97+ assertThat (actualNode .path ("hoge_metadata" ).isPojo (), is (true ));
98+ POJONode pojoNodeHoge = (POJONode ) actualNode .path ("hoge_metadata" );
99+ assertThat (pojoNodeHoge .getPojo (), is (instanceOf (RawValue .class )));
100+ RawValue rawValueHoge = (RawValue ) pojoNodeHoge .getPojo ();
101+ assertThat (rawValueHoge .rawValue (), is ("{\" baz\" :\" qux\" }" ));
102+
92103 // exercise 2: Tree to Value
93104 Model actualValue = mapper .treeToValue (actualNode , Model .class );
94105 // verify 2
@@ -104,12 +115,16 @@ public void testJsonToValue() throws Exception {
104115 + " 'bar':1234,"
105116 + " 'metadata':{"
106117 + " 'foo':'bar'"
118+ + " },"
119+ + " 'hoge_metadata':{"
120+ + " 'baz':'qux'"
107121 + " }"
108122 + "}" ;
109123 Model expected = new Model ();
110124 expected .setFoo ("12341234" );
111125 expected .setBar (1234 );
112126 expected .setMetadata ("{\" foo\" :\" bar\" }" );
127+ expected .setHogeMetadata ("{\" baz\" :\" qux\" }" );
113128 // exercise
114129 Model actual = mapper .readValue (modelJson , Model .class );
115130 // verify
@@ -125,12 +140,16 @@ public void testJsonToTreeToValue() throws Exception {
125140 + " 'bar':1234,"
126141 + " 'metadata':{"
127142 + " 'foo':'bar'"
143+ + " },"
144+ + " 'hoge_metadata':{"
145+ + " 'baz':'qux'"
128146 + " }"
129147 + "}" ;
130148 Model model = new Model ();
131149 model .setFoo ("12341234" );
132150 model .setBar (1234 );
133151 model .setMetadata ("{\" foo\" :\" bar\" }" );
152+ model .setHogeMetadata ("{\" baz\" :\" qux\" }" );
134153
135154 // exercise 1: Json to Tree
136155 JsonNode actualNode = mapper .readTree (modelJson );
@@ -144,6 +163,9 @@ public void testJsonToTreeToValue() throws Exception {
144163 assertThat (actualNode .path ("metadata" ).isObject (), is (true ));
145164 assertThat (actualNode .path ("metadata" ).path ("foo" ).textValue (), is ("bar" ));
146165
166+ assertThat (actualNode .path ("hoge_metadata" ).isObject (), is (true ));
167+ assertThat (actualNode .path ("hoge_metadata" ).path ("baz" ).textValue (), is ("qux" ));
168+
147169 // exercise 2: Tree to Value
148170 Model actualValue = mapper .treeToValue (actualNode , Model .class );
149171 // verify 2
@@ -162,4 +184,7 @@ class Model {
162184 @ JsonRawValueExtended
163185 private String metadata ;
164186
187+ @ JsonRawValueExtended
188+ private String hogeMetadata ;
189+
165190}
0 commit comments