@@ -46,6 +46,53 @@ public DataImpl(JsonNode n) {
46
46
47
47
private final ObjectMapper MAPPER = objectMapper ();
48
48
49
+ public void testSimpleObject () throws Exception
50
+ {
51
+ String JSON = "{ \" key\" : 1, \" b\" : \" x\" }" ;
52
+ JsonNode root = MAPPER .readTree (JSON );
53
+
54
+ // basic properties first:
55
+ assertFalse (root .isValueNode ());
56
+ assertTrue (root .isContainerNode ());
57
+ assertFalse (root .isArray ());
58
+ assertTrue (root .isObject ());
59
+ assertEquals (2 , root .size ());
60
+
61
+ // Related to [JACKSON-50]:
62
+ Iterator <JsonNode > it = root .iterator ();
63
+ assertNotNull (it );
64
+ assertTrue (it .hasNext ());
65
+ JsonNode n = it .next ();
66
+ assertNotNull (n );
67
+ assertEquals (IntNode .valueOf (1 ), n );
68
+
69
+ assertTrue (it .hasNext ());
70
+ n = it .next ();
71
+ assertNotNull (n );
72
+ assertEquals (TextNode .valueOf ("x" ), n );
73
+
74
+ assertFalse (it .hasNext ());
75
+
76
+ // Ok, then, let's traverse via extended interface
77
+ ObjectNode obNode = (ObjectNode ) root ;
78
+ Iterator <Map .Entry <String ,JsonNode >> fit = obNode .fields ();
79
+ // we also know that LinkedHashMap is used, i.e. order preserved
80
+ assertTrue (fit .hasNext ());
81
+ Map .Entry <String ,JsonNode > en = fit .next ();
82
+ assertEquals ("key" , en .getKey ());
83
+ assertEquals (IntNode .valueOf (1 ), en .getValue ());
84
+
85
+ assertTrue (fit .hasNext ());
86
+ en = fit .next ();
87
+ assertEquals ("b" , en .getKey ());
88
+ assertEquals (TextNode .valueOf ("x" ), en .getValue ());
89
+
90
+ // Plus: we should be able to modify the node via iterator too:
91
+ fit .remove ();
92
+ assertEquals (1 , obNode .size ());
93
+ assertEquals (IntNode .valueOf (1 ), root .get ("key" ));
94
+ assertNull (root .get ("b" ));
95
+ }
49
96
// for [Issue#346]
50
97
public void testEmptyNodeAsValue () throws Exception
51
98
{
@@ -309,4 +356,14 @@ public void testEqualityWrtOrder() throws Exception
309
356
assertTrue (ob1 .equals (ob2 ));
310
357
assertTrue (ob2 .equals (ob1 ));
311
358
}
359
+
360
+ public void testSimplePath () throws Exception
361
+ {
362
+ JsonNode root = MAPPER .readTree ("{ \" results\" : { \" a\" : 3 } }" );
363
+ assertTrue (root .isObject ());
364
+ JsonNode rnode = root .path ("results" );
365
+ assertNotNull (rnode );
366
+ assertTrue (rnode .isObject ());
367
+ assertEquals (3 , rnode .path ("a" ).intValue ());
368
+ }
312
369
}
0 commit comments