@@ -56,24 +56,46 @@ public void testJsonReaderDuplicateKey2() {
56
56
assertEquals ("Duplicate key 'a' is not allowed" , e .getMessage ());
57
57
}
58
58
}
59
-
59
+
60
+ @ Test
61
+ public void testJsonReaderDuplicateKey3 () {
62
+ String json = "{\" a\" :\" b\" ,\" b\" :{\" c\" :\" d\" ,\" c\" :\" e\" }}" ;
63
+ JsonReader jsonReader = Json .createReader (new StringReader (json ));
64
+ JsonObject jsonObject = jsonReader .readObject ();
65
+ assertEquals (jsonObject .getJsonObject ("b" ).getString ("c" ), "e" );
66
+ }
67
+
68
+ @ Test
69
+ public void testJsonReaderDuplicateKey4 () {
70
+ String json = "{\" a\" :\" b\" ,\" b\" :{\" c\" :\" d\" ,\" c\" :\" e\" }}" ;;
71
+ JsonReaderFactory jsonReaderFactory = Json .createReaderFactory (Collections .singletonMap (JsonConfig .REJECT_DUPLICATE_KEYS , true ));
72
+ JsonReader jsonReader = jsonReaderFactory .createReader (new StringReader (json ));
73
+ try {
74
+ jsonReader .readObject ();
75
+ fail ();
76
+ } catch (Exception e ) {
77
+ assertTrue (e instanceof JsonParsingException );
78
+ assertEquals ("Duplicate key 'c' is not allowed" , e .getMessage ());
79
+ }
80
+ }
81
+
60
82
@ Test
61
83
public void testJsonObjectBuilderDuplcateKey1 () {
62
- JsonObjectBuilder objectBuilder = Json .createObjectBuilder ();
63
- JsonObject jsonObject = objectBuilder .add ("a" , "b" ).add ("a" , "c" ).build ();
64
- assertEquals (jsonObject .getString ("a" ), "c" );
84
+ JsonObjectBuilder objectBuilder = Json .createObjectBuilder ();
85
+ JsonObject jsonObject = objectBuilder .add ("a" , "b" ).add ("a" , "c" ).build ();
86
+ assertEquals (jsonObject .getString ("a" ), "c" );
65
87
}
66
-
88
+
67
89
@ Test
68
90
public void testJsonObjectBuilderDuplcateKey2 () {
69
- JsonBuilderFactory jsonBuilderFactory = Json .createBuilderFactory (Collections .singletonMap (JsonConfig .REJECT_DUPLICATE_KEYS , true ));
70
- JsonObjectBuilder objectBuilder = jsonBuilderFactory .createObjectBuilder ();
71
- try {
72
- objectBuilder .add ("a" , "b" ).add ("a" , "c" ).build ();
73
- fail ();
74
- } catch (Exception e ) {
91
+ JsonBuilderFactory jsonBuilderFactory = Json .createBuilderFactory (Collections .singletonMap (JsonConfig .REJECT_DUPLICATE_KEYS , true ));
92
+ JsonObjectBuilder objectBuilder = jsonBuilderFactory .createObjectBuilder ();
93
+ try {
94
+ objectBuilder .add ("a" , "b" ).add ("a" , "c" ).build ();
95
+ fail ();
96
+ } catch (Exception e ) {
75
97
assertTrue (e instanceof IllegalStateException );
76
98
assertEquals ("Duplicate key 'a' is not allowed" , e .getMessage ());
77
- }
99
+ }
78
100
}
79
101
}
0 commit comments