1
1
package org .everit .json .schema .internal ;
2
2
3
3
import java .io .IOException ;
4
+ import java .util .Arrays ;
4
5
import java .util .Collection ;
5
6
import java .util .Map ;
6
7
import java .util .regex .Pattern ;
7
8
8
9
import org .json .JSONArray ;
9
10
import org .json .JSONException ;
10
11
import org .json .JSONObject ;
11
-
12
- interface JSONString {
13
- String toJSONString ();
14
- }
15
-
12
+ import org .json .JSONString ;
16
13
17
14
/*
18
15
Copyright (c) 2006 JSON.org
@@ -73,6 +70,16 @@ class JSONWriter {
73
70
74
71
private static final Pattern NUMBER_PATTERN = Pattern .compile ("-?(?:0|[1-9]\\ d*)(?:\\ .\\ d+)?(?:[eE][+-]?\\ d+)?" );
75
72
73
+ private static boolean subclassOfJSONString (Class <?> clazz ) {
74
+ return clazz != null && (Arrays .stream (clazz .getInterfaces ())
75
+ .anyMatch (intf -> "org.json.JSONString" .equals (intf .getCanonicalName ()))
76
+ || subclassOfJSONString (clazz .getSuperclass ()));
77
+ }
78
+
79
+ static boolean implementsJSONString (Object o ) {
80
+ return o != null && subclassOfJSONString (o .getClass ());
81
+ }
82
+
76
83
/**
77
84
* The comma flag determines if a comma should be output before the next
78
85
* value.
@@ -87,7 +94,7 @@ class JSONWriter {
87
94
* 'k' (key),
88
95
* 'o' (object).
89
96
*/
90
- protected char mode ;
97
+ private char mode ;
91
98
92
99
/**
93
100
* The object/array stack.
@@ -102,12 +109,12 @@ class JSONWriter {
102
109
/**
103
110
* The writer that will receive the output.
104
111
*/
105
- protected Appendable writer ;
112
+ private Appendable writer ;
106
113
107
114
/**
108
115
* Make a fresh JSONWriter. It can be used to build one JSON text.
109
116
*/
110
- public JSONWriter (Appendable w ) {
117
+ JSONWriter (Appendable w ) {
111
118
this .comma = false ;
112
119
this .mode = 'i' ;
113
120
this .stack = new JSONObject [maxdepth ];
@@ -160,7 +167,7 @@ private JSONWriter append(String string) throws JSONException {
160
167
* started in the wrong place (for example as a key or after the end of the
161
168
* outermost array or object).
162
169
*/
163
- public JSONWriter array () throws JSONException {
170
+ JSONWriter array () throws JSONException {
164
171
if (this .mode == 'i' || this .mode == 'o' || this .mode == 'a' ) {
165
172
this .push (null );
166
173
this .append ("[" );
@@ -208,7 +215,7 @@ private JSONWriter end(char m, char c) throws JSONException {
208
215
* @throws JSONException
209
216
* If incorrectly nested.
210
217
*/
211
- public JSONWriter endArray () throws JSONException {
218
+ JSONWriter endArray () throws JSONException {
212
219
return this .end ('a' , ']' );
213
220
}
214
221
@@ -220,7 +227,7 @@ public JSONWriter endArray() throws JSONException {
220
227
* @throws JSONException
221
228
* If incorrectly nested.
222
229
*/
223
- public JSONWriter endObject () throws JSONException {
230
+ JSONWriter endObject () throws JSONException {
224
231
return this .end ('k' , '}' );
225
232
}
226
233
@@ -235,7 +242,7 @@ public JSONWriter endObject() throws JSONException {
235
242
* If the key is out of place. For example, keys
236
243
* do not belong in arrays or if the key is null.
237
244
*/
238
- public JSONWriter key (String string ) throws JSONException {
245
+ JSONWriter key (String string ) throws JSONException {
239
246
if (string == null ) {
240
247
throw new JSONException ("Null key." );
241
248
}
@@ -276,7 +283,7 @@ public JSONWriter key(String string) throws JSONException {
276
283
* started in the wrong place (for example as a key or after the end of the
277
284
* outermost array or object).
278
285
*/
279
- public JSONWriter object () throws JSONException {
286
+ JSONWriter object () throws JSONException {
280
287
if (this .mode == 'i' ) {
281
288
this .mode = 'o' ;
282
289
}
@@ -355,11 +362,12 @@ private void push(JSONObject jo) throws JSONException {
355
362
* @throws JSONException
356
363
* If the value is or contains an invalid number.
357
364
*/
358
- public static String valueToString (Object value ) throws JSONException {
365
+ static String valueToString (Object value ) throws JSONException {
359
366
if (value == null || value .equals (null )) {
360
367
return "null" ;
361
368
}
362
- if (value instanceof JSONString ) {
369
+
370
+ if (implementsJSONString (value )) {
363
371
String object ;
364
372
try {
365
373
object = ((JSONString ) value ).toJSONString ();
@@ -412,7 +420,7 @@ public static String valueToString(Object value) throws JSONException {
412
420
* @return this
413
421
* @throws JSONException
414
422
*/
415
- public JSONWriter value (boolean b ) throws JSONException {
423
+ JSONWriter value (boolean b ) throws JSONException {
416
424
return this .append (b ? "true" : "false" );
417
425
}
418
426
@@ -425,7 +433,7 @@ public JSONWriter value(boolean b) throws JSONException {
425
433
* @throws JSONException
426
434
* If the number is not finite.
427
435
*/
428
- public JSONWriter value (double d ) throws JSONException {
436
+ JSONWriter value (double d ) throws JSONException {
429
437
return this .value (Double .valueOf (d ));
430
438
}
431
439
@@ -437,7 +445,7 @@ public JSONWriter value(double d) throws JSONException {
437
445
* @return this
438
446
* @throws JSONException
439
447
*/
440
- public JSONWriter value (long l ) throws JSONException {
448
+ JSONWriter value (long l ) throws JSONException {
441
449
return this .append (Long .toString (l ));
442
450
}
443
451
@@ -451,7 +459,7 @@ public JSONWriter value(long l) throws JSONException {
451
459
* @throws JSONException
452
460
* If the value is out of sequence.
453
461
*/
454
- public JSONWriter value (Object object ) throws JSONException {
462
+ JSONWriter value (Object object ) throws JSONException {
455
463
return this .append (valueToString (object ));
456
464
}
457
465
}
0 commit comments