@@ -616,6 +616,46 @@ public void jsonObjectByMapWithNullValue() {
616
616
assertTrue ("expected \" doubleKey\" :-23.45e67" , Double .valueOf ("-23.45e67" ).equals (jsonObject .query ("/doubleKey" )));
617
617
Util .checkJSONObjectMaps (jsonObject );
618
618
}
619
+
620
+ @ Test
621
+ public void jsonObjectByMapWithNullValueAndParserConfiguration () {
622
+ Map <String , Object > map = new HashMap <String , Object >();
623
+ map .put ("nullKey" , null );
624
+
625
+ // by default, null values are ignored
626
+ JSONObject obj1 = new JSONObject (map );
627
+ assertTrue ("expected null value to be ignored by default" , obj1 .isEmpty ());
628
+
629
+ // if configured, null values are written as such into the JSONObject.
630
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration ().withUseNativeNulls (true );
631
+ JSONObject obj2 = new JSONObject (map , parserConfiguration );
632
+ assertFalse ("expected null value to accepted when configured" , obj2 .isEmpty ());
633
+ assertTrue (obj2 .has ("nullKey" ));
634
+ assertEquals (JSONObject .NULL , obj2 .get ("nullKey" ));
635
+ }
636
+
637
+ @ Test
638
+ public void jsonObjectByMapWithNestedNullValueAndParserConfiguration () {
639
+ Map <String , Object > map = new HashMap <String , Object >();
640
+ Map <String , Object > nestedMap = new HashMap <String , Object >();
641
+ nestedMap .put ("nullKey" , null );
642
+ map .put ("nestedMap" , nestedMap );
643
+ List <Map <String , Object >> nestedList = new ArrayList <Map <String ,Object >>();
644
+ nestedList .add (nestedMap );
645
+ map .put ("nestedList" , nestedList );
646
+
647
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration ().withUseNativeNulls (true );
648
+ JSONObject jsonObject = new JSONObject (map , parserConfiguration );
649
+
650
+ JSONObject nestedObject = jsonObject .getJSONObject ("nestedMap" );
651
+ assertTrue (nestedObject .has ("nullKey" ));
652
+ assertEquals (JSONObject .NULL , nestedObject .get ("nullKey" ));
653
+
654
+ JSONArray nestedArray = jsonObject .getJSONArray ("nestedList" );
655
+ assertEquals (1 , nestedArray .length ());
656
+ assertTrue (nestedArray .getJSONObject (0 ).has ("nullKey" ));
657
+ assertEquals (JSONObject .NULL , nestedArray .getJSONObject (0 ).get ("nullKey" ));
658
+ }
619
659
620
660
/**
621
661
* JSONObject built from a bean. In this case all but one of the
0 commit comments