Skip to content

Commit aeaef68

Browse files
committed
making the JSONWriter backport non-public, working around potentially missing JSONString ; removing
1 parent 2bd97ae commit aeaef68

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

core/src/main/java/org/everit/json/schema/internal/JSONWriter.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package org.everit.json.schema.internal;
22

33
import java.io.IOException;
4+
import java.util.Arrays;
45
import java.util.Collection;
56
import java.util.Map;
67
import java.util.regex.Pattern;
78

89
import org.json.JSONArray;
910
import org.json.JSONException;
1011
import org.json.JSONObject;
11-
12-
interface JSONString {
13-
String toJSONString();
14-
}
15-
12+
import org.json.JSONString;
1613

1714
/*
1815
Copyright (c) 2006 JSON.org
@@ -73,6 +70,16 @@ class JSONWriter {
7370

7471
private static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?");
7572

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+
7683
/**
7784
* The comma flag determines if a comma should be output before the next
7885
* value.
@@ -87,7 +94,7 @@ class JSONWriter {
8794
* 'k' (key),
8895
* 'o' (object).
8996
*/
90-
protected char mode;
97+
private char mode;
9198

9299
/**
93100
* The object/array stack.
@@ -102,12 +109,12 @@ class JSONWriter {
102109
/**
103110
* The writer that will receive the output.
104111
*/
105-
protected Appendable writer;
112+
private Appendable writer;
106113

107114
/**
108115
* Make a fresh JSONWriter. It can be used to build one JSON text.
109116
*/
110-
public JSONWriter(Appendable w) {
117+
JSONWriter(Appendable w) {
111118
this.comma = false;
112119
this.mode = 'i';
113120
this.stack = new JSONObject[maxdepth];
@@ -160,7 +167,7 @@ private JSONWriter append(String string) throws JSONException {
160167
* started in the wrong place (for example as a key or after the end of the
161168
* outermost array or object).
162169
*/
163-
public JSONWriter array() throws JSONException {
170+
JSONWriter array() throws JSONException {
164171
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
165172
this.push(null);
166173
this.append("[");
@@ -208,7 +215,7 @@ private JSONWriter end(char m, char c) throws JSONException {
208215
* @throws JSONException
209216
* If incorrectly nested.
210217
*/
211-
public JSONWriter endArray() throws JSONException {
218+
JSONWriter endArray() throws JSONException {
212219
return this.end('a', ']');
213220
}
214221

@@ -220,7 +227,7 @@ public JSONWriter endArray() throws JSONException {
220227
* @throws JSONException
221228
* If incorrectly nested.
222229
*/
223-
public JSONWriter endObject() throws JSONException {
230+
JSONWriter endObject() throws JSONException {
224231
return this.end('k', '}');
225232
}
226233

@@ -235,7 +242,7 @@ public JSONWriter endObject() throws JSONException {
235242
* If the key is out of place. For example, keys
236243
* do not belong in arrays or if the key is null.
237244
*/
238-
public JSONWriter key(String string) throws JSONException {
245+
JSONWriter key(String string) throws JSONException {
239246
if (string == null) {
240247
throw new JSONException("Null key.");
241248
}
@@ -276,7 +283,7 @@ public JSONWriter key(String string) throws JSONException {
276283
* started in the wrong place (for example as a key or after the end of the
277284
* outermost array or object).
278285
*/
279-
public JSONWriter object() throws JSONException {
286+
JSONWriter object() throws JSONException {
280287
if (this.mode == 'i') {
281288
this.mode = 'o';
282289
}
@@ -355,11 +362,12 @@ private void push(JSONObject jo) throws JSONException {
355362
* @throws JSONException
356363
* If the value is or contains an invalid number.
357364
*/
358-
public static String valueToString(Object value) throws JSONException {
365+
static String valueToString(Object value) throws JSONException {
359366
if (value == null || value.equals(null)) {
360367
return "null";
361368
}
362-
if (value instanceof JSONString) {
369+
370+
if (implementsJSONString(value)) {
363371
String object;
364372
try {
365373
object = ((JSONString) value).toJSONString();
@@ -412,7 +420,7 @@ public static String valueToString(Object value) throws JSONException {
412420
* @return this
413421
* @throws JSONException
414422
*/
415-
public JSONWriter value(boolean b) throws JSONException {
423+
JSONWriter value(boolean b) throws JSONException {
416424
return this.append(b ? "true" : "false");
417425
}
418426

@@ -425,7 +433,7 @@ public JSONWriter value(boolean b) throws JSONException {
425433
* @throws JSONException
426434
* If the number is not finite.
427435
*/
428-
public JSONWriter value(double d) throws JSONException {
436+
JSONWriter value(double d) throws JSONException {
429437
return this.value(Double.valueOf(d));
430438
}
431439

@@ -437,7 +445,7 @@ public JSONWriter value(double d) throws JSONException {
437445
* @return this
438446
* @throws JSONException
439447
*/
440-
public JSONWriter value(long l) throws JSONException {
448+
JSONWriter value(long l) throws JSONException {
441449
return this.append(Long.toString(l));
442450
}
443451

@@ -451,7 +459,7 @@ public JSONWriter value(long l) throws JSONException {
451459
* @throws JSONException
452460
* If the value is out of sequence.
453461
*/
454-
public JSONWriter value(Object object) throws JSONException {
462+
JSONWriter value(Object object) throws JSONException {
455463
return this.append(valueToString(object));
456464
}
457465
}

tests/vanilla/src/main/java/org/everit/json/schema/IssueTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public IssueTest(String issueDir, String testCaseName) {
7575

7676
private Optional<InputStream> fileByName(final String fileName) {
7777
return Optional.ofNullable(getClass().getResourceAsStream(issueDir + "/" + fileName));
78-
// return Arrays.stream(issueDir.listFiles())
79-
// .filter(file -> file.getName().equals(fileName))
80-
// .findFirst();
8178
}
8279

8380
private void initJetty() {

0 commit comments

Comments
 (0)