@@ -152,12 +152,6 @@ public Class<? extends Map> getMapType() {
152152 */
153153 public static final Object NULL = new Null ();
154154
155- // strict mode checks after constructor require access to this object
156- private JSONTokener jsonTokener ;
157-
158- // strict mode checks after constructor require access to this object
159- private JSONParserConfiguration jsonParserConfiguration ;
160-
161155 /**
162156 * Construct an empty JSONObject.
163157 */
@@ -217,18 +211,11 @@ public JSONObject(JSONTokener x) throws JSONException {
217211 */
218212 public JSONObject (JSONTokener x , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
219213 this ();
220-
221- if (this .jsonParserConfiguration == null ) {
222- this .jsonParserConfiguration = jsonParserConfiguration ;
223- }
224- if (this .jsonTokener == null ) {
225- this .jsonTokener = x ;
226- this .jsonTokener .setJsonParserConfiguration (this .jsonParserConfiguration );
227- }
228-
229214 char c ;
230215 String key ;
231216
217+ boolean isInitial = x .getPrevious () == 0 ;
218+
232219 if (x .nextClean () != '{' ) {
233220 throw x .syntaxError ("A JSONObject text must begin with '{'" );
234221 }
@@ -238,6 +225,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
238225 case 0 :
239226 throw x .syntaxError ("A JSONObject text must end with '}'" );
240227 case '}' :
228+ if (isInitial && jsonParserConfiguration .isStrictMode () && x .nextClean () != 0 ) {
229+ throw x .syntaxError ("Strict mode error: Unparsed characters found at end of input text" );
230+ }
241231 return ;
242232 default :
243233 key = x .nextSimpleValue (c ).toString ();
@@ -288,6 +278,9 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
288278 x .back ();
289279 break ;
290280 case '}' :
281+ if (isInitial && jsonParserConfiguration .isStrictMode () && x .nextClean () != 0 ) {
282+ throw x .syntaxError ("Strict mode error: Unparsed characters found at end of input text" );
283+ }
291284 return ;
292285 default :
293286 throw x .syntaxError ("Expected a ',' or '}'" );
@@ -456,11 +449,6 @@ public JSONObject(Object object, String ... names) {
456449 */
457450 public JSONObject (String source ) throws JSONException {
458451 this (source , new JSONParserConfiguration ());
459- // Strict mode does not allow trailing chars
460- if (this .jsonParserConfiguration .isStrictMode () &&
461- this .jsonTokener .nextClean () != 0 ) {
462- throw new JSONException ("Strict mode error: Unparsed characters found at end of input text" );
463- }
464452 }
465453
466454 /**
@@ -478,12 +466,7 @@ public JSONObject(String source) throws JSONException {
478466 * duplicated key.
479467 */
480468 public JSONObject (String source , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
481- this (new JSONTokener (source ), jsonParserConfiguration );
482- // Strict mode does not allow trailing chars
483- if (this .jsonParserConfiguration .isStrictMode () &&
484- this .jsonTokener .nextClean () != 0 ) {
485- throw new JSONException ("Strict mode error: Unparsed characters found at end of input text" );
486- }
469+ this (new JSONTokener (source , jsonParserConfiguration ), jsonParserConfiguration );
487470 }
488471
489472 /**
@@ -1280,7 +1263,7 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
12801263 static BigDecimal objectToBigDecimal (Object val , BigDecimal defaultValue ) {
12811264 return objectToBigDecimal (val , defaultValue , true );
12821265 }
1283-
1266+
12841267 /**
12851268 * @param val value to convert
12861269 * @param defaultValue default value to return is the conversion doesn't work or is null.
0 commit comments