1616import java .util .stream .Stream ;
1717
1818import static org .junit .Assert .assertEquals ;
19+ import static org .junit .Assert .assertSame ;
1920import static org .junit .Assert .assertThrows ;
2021import static org .junit .Assert .assertTrue ;
2122import static org .junit .Assert .fail ;
@@ -36,6 +37,92 @@ public void testOverwrite() {
3637 assertEquals ("duplicate key should be overwritten" , "value2" , jsonObject .getString ("key" ));
3738 }
3839
40+ @ Test
41+ public void objectConstructorAppliesExplicitStrictModeToValues () {
42+ JSONParserConfiguration configuration = new JSONParserConfiguration ().withStrictMode ();
43+ JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
44+ for (String source : Arrays .asList (
45+ "[]" ,
46+ "{" ,
47+ "{} trailing" ,
48+ "{\" value\" :'text'}" ,
49+ "{\" value\" :[1,]}" ,
50+ "{\" value\" :{\" nested\" :1,}}" )) {
51+ JSONTokener tokener = new JSONTokener (source , lenient );
52+ assertThrows (JSONException .class , () -> new JSONObject (tokener , configuration ));
53+ assertSame (lenient , tokener .getJsonParserConfiguration ());
54+ }
55+ }
56+
57+ @ Test
58+ public void arrayConstructorAppliesExplicitStrictModeToValues () {
59+ JSONParserConfiguration configuration = new JSONParserConfiguration ().withStrictMode ();
60+ JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
61+ for (String source : Arrays .asList (
62+ "{}" ,
63+ "[" ,
64+ "[] trailing" ,
65+ "['text']" ,
66+ "[[1,]]" ,
67+ "[{\" nested\" :1,}]" )) {
68+ JSONTokener tokener = new JSONTokener (source , lenient );
69+ assertThrows (JSONException .class , () -> new JSONArray (tokener , configuration ));
70+ assertSame (lenient , tokener .getJsonParserConfiguration ());
71+ }
72+ }
73+
74+ @ Test
75+ public void explicitConfigurationOverridesTokenerStrictMode () {
76+ JSONParserConfiguration strict = new JSONParserConfiguration ().withStrictMode ();
77+ JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
78+ JSONTokener objectTokener = new JSONTokener ("{\" value\" :[{'nested':'text',}], 'other':'text'} 'next'" , strict );
79+ JSONTokener arrayTokener = new JSONTokener ("[{\" value\" :['text',]},'text'] 'next'" , strict );
80+
81+ JSONObject object = new JSONObject (objectTokener , lenient );
82+ JSONArray array = new JSONArray (arrayTokener , lenient );
83+
84+ assertEquals ("text" , object .getJSONArray ("value" ).getJSONObject (0 ).getString ("nested" ));
85+ assertEquals ("text" , array .getJSONObject (0 ).getJSONArray ("value" ).getString (0 ));
86+ assertEquals ("text" , object .getString ("other" ));
87+ assertEquals ("text" , array .getString (1 ));
88+ assertSame (strict , objectTokener .getJsonParserConfiguration ());
89+ assertSame (strict , arrayTokener .getJsonParserConfiguration ());
90+ assertThrows (JSONException .class , () -> objectTokener .nextValue ());
91+ assertThrows (JSONException .class , () -> arrayTokener .nextValue ());
92+ }
93+
94+ @ Test
95+ public void objectConstructorRestoresTokenerConfiguration () {
96+ JSONParserConfiguration original = new JSONParserConfiguration ().withStrictMode (false );
97+ JSONParserConfiguration strict = new JSONParserConfiguration ().withStrictMode ();
98+ for (String source : Arrays .asList ("{}" , "{\" value\" :[{\" nested\" :\" text\" }],\" other\" :true}" )) {
99+ JSONTokener tokener = new JSONTokener (source , original );
100+ new JSONObject (tokener , strict );
101+ assertSame (original , tokener .getJsonParserConfiguration ());
102+ }
103+ }
104+
105+ @ Test
106+ public void arrayConstructorRestoresTokenerConfiguration () {
107+ JSONParserConfiguration original = new JSONParserConfiguration ().withStrictMode (false );
108+ JSONParserConfiguration strict = new JSONParserConfiguration ().withStrictMode ();
109+ for (String source : Arrays .asList ("[]" , "[{\" value\" :[\" text\" ]},true]" )) {
110+ JSONTokener tokener = new JSONTokener (source , original );
111+ new JSONArray (tokener , strict );
112+ assertSame (original , tokener .getJsonParserConfiguration ());
113+ }
114+ }
115+
116+ @ Test
117+ public void explicitConfigurationAppliesToNestedDuplicateKeys () {
118+ JSONParserConfiguration configuration = new JSONParserConfiguration ().withOverwriteDuplicateKey (true );
119+ JSONObject object = new JSONObject (new JSONTokener ("{\" value\" :[{\" key\" :1,\" key\" :2}]}" ), configuration );
120+ JSONArray array = new JSONArray (new JSONTokener ("[{\" value\" :{\" key\" :1,\" key\" :2}}]" ), configuration );
121+
122+ assertEquals (2 , object .getJSONArray ("value" ).getJSONObject (0 ).getInt ("key" ));
123+ assertEquals (2 , array .getJSONObject (0 ).getJSONObject ("value" ).getInt ("key" ));
124+ }
125+
39126 @ Test
40127 public void strictModeIsCloned (){
41128 JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration ()
0 commit comments