@@ -42,10 +42,15 @@ public void objectConstructorAppliesExplicitStrictModeToValues() {
4242 JSONParserConfiguration configuration = new JSONParserConfiguration ().withStrictMode ();
4343 JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
4444 for (String source : Arrays .asList (
45+ "[]" ,
46+ "{" ,
47+ "{} trailing" ,
4548 "{\" value\" :'text'}" ,
4649 "{\" value\" :[1,]}" ,
4750 "{\" value\" :{\" nested\" :1,}}" )) {
48- assertThrows (JSONException .class , () -> new JSONObject (new JSONTokener (source , lenient ), configuration ));
51+ JSONTokener tokener = new JSONTokener (source , lenient );
52+ assertThrows (JSONException .class , () -> new JSONObject (tokener , configuration ));
53+ assertSame (lenient , tokener .getJsonParserConfiguration ());
4954 }
5055 }
5156
@@ -54,27 +59,58 @@ public void arrayConstructorAppliesExplicitStrictModeToValues() {
5459 JSONParserConfiguration configuration = new JSONParserConfiguration ().withStrictMode ();
5560 JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
5661 for (String source : Arrays .asList (
62+ "{}" ,
63+ "[" ,
64+ "[] trailing" ,
5765 "['text']" ,
5866 "[[1,]]" ,
5967 "[{\" nested\" :1,}]" )) {
60- assertThrows (JSONException .class , () -> new JSONArray (new JSONTokener (source , lenient ), configuration ));
68+ JSONTokener tokener = new JSONTokener (source , lenient );
69+ assertThrows (JSONException .class , () -> new JSONArray (tokener , configuration ));
70+ assertSame (lenient , tokener .getJsonParserConfiguration ());
6171 }
6272 }
6373
6474 @ Test
6575 public void explicitConfigurationOverridesTokenerStrictMode () {
6676 JSONParserConfiguration strict = new JSONParserConfiguration ().withStrictMode ();
6777 JSONParserConfiguration lenient = new JSONParserConfiguration ().withStrictMode (false );
68- JSONTokener objectTokener = new JSONTokener ("{\" value\" :[{'nested':'text',}]} " , strict );
69- JSONTokener arrayTokener = new JSONTokener ("[{\" value\" :['text',]}] " , strict );
78+ JSONTokener objectTokener = new JSONTokener ("{\" value\" :[{'nested':'text',}], 'other':'text'} 'next' " , strict );
79+ JSONTokener arrayTokener = new JSONTokener ("[{\" value\" :['text',]},'text'] 'next' " , strict );
7080
7181 JSONObject object = new JSONObject (objectTokener , lenient );
7282 JSONArray array = new JSONArray (arrayTokener , lenient );
7383
7484 assertEquals ("text" , object .getJSONArray ("value" ).getJSONObject (0 ).getString ("nested" ));
7585 assertEquals ("text" , array .getJSONObject (0 ).getJSONArray ("value" ).getString (0 ));
76- assertSame (lenient , objectTokener .getJsonParserConfiguration ());
77- assertSame (lenient , arrayTokener .getJsonParserConfiguration ());
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+ }
78114 }
79115
80116 @ Test
0 commit comments