@@ -56,24 +56,46 @@ public void testJsonReaderDuplicateKey2() {
5656 assertEquals ("Duplicate key 'a' is not allowed" , e .getMessage ());
5757 }
5858 }
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+
6082 @ Test
6183 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" );
6587 }
66-
88+
6789 @ Test
6890 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 ) {
7597 assertTrue (e instanceof IllegalStateException );
7698 assertEquals ("Duplicate key 'a' is not allowed" , e .getMessage ());
77- }
99+ }
78100 }
79101}
0 commit comments