diff --git a/src/main/java/org/openjax/json/JsonReader.java b/src/main/java/org/openjax/json/JsonReader.java index 0285890..85330af 100644 --- a/src/main/java/org/openjax/json/JsonReader.java +++ b/src/main/java/org/openjax/json/JsonReader.java @@ -87,33 +87,57 @@ public static JsonReader of(final Reader reader) { private int nextStart = 0; private final boolean ignoreWhitespace; + private final boolean strict; /** - * Construct a new {@link JsonReader} for JSON content to be read from the specified {@link Reader}, that ignores inter-token - * whitespace. This constructor is equivalent to calling {@code new JsonReader(reader, true)}. + * Construct a new {@link JsonReader} for JSON content to be read from the specified {@link Reader}, that ignores inter-token + * whitespace, and enforces strict compliance to the JSON + * Specification. + *
+ * This constructor is equivalent to calling {@code new JsonReader(reader, true, true)}. * * @param reader The {@link Reader} from which JSON is to be read. * @throws NullPointerException If {@code reader} is null. */ public JsonReader(final Reader reader) { - this(reader, true); + this(reader, true, true); } /** - * Construct a new {@link JsonReader} for JSON content to be read from the specified {@link Reader}. + * Construct a new {@link JsonReader} for JSON content to be read from the specified {@link Reader}, that enforces strict + * compliance to the JSON Specification. + *
+ * This constructor is equivalent to calling {@code new JsonReader(reader, ignoreWhitespace, true)}. * * @param reader The {@link Reader} from which JSON is to be read. * @param ignoreWhitespace If {@code ignoreWhitespace == false}, inter-token whitespace will not be skipped. * @throws NullPointerException If {@code reader} is null. */ public JsonReader(final Reader reader, final boolean ignoreWhitespace) { + this(reader, ignoreWhitespace, true); + } + + /** + * Construct a new {@link JsonReader} for JSON content to be read from the specified {@link Reader}. + * + * @param reader The {@link Reader} from which JSON is to be read. + * @param ignoreWhitespace If {@code ignoreWhitespace == false}, inter-token whitespace will not be skipped. + * @param strict Whether to enforce strict compliance to the JSON Specification + * while parsing the JSON document. + * @throws NullPointerException If {@code reader} is null. + */ + public JsonReader(final Reader reader, final boolean ignoreWhitespace, final boolean strict) { super(reader, DEFAULT_BUFFER_SIZE); this.ignoreWhitespace = ignoreWhitespace; + this.strict = strict; } /** * Construct a new {@link JsonReader} for JSON content to be read from the specified string, that ignores inter-token - * whitespace. This constructor is equivalent to calling {@code new JsonReader(str, true)}. + * whitespace, and enforces strict compliance to the JSON + * Specification. + *
+ * This constructor is equivalent to calling {@code new JsonReader(str, true, true)}. * * @param str The string with the JSON document to be read. * @throws NullPointerException If {@code str} is null. @@ -123,15 +147,52 @@ public JsonReader(final String str) { } /** - * Construct a new {@link JsonReader} for JSON content to be read from the specified string. + * Construct a new {@link JsonReader} for JSON content to be read from the specified string, that enforces strict compliance to + * the JSON Specification. + *
+ * This constructor is equivalent to calling {@code new JsonReader(str, ignoreWhitespace, true)}.
*
* @param str The string with the JSON document to be read.
* @param ignoreWhitespace If {@code ignoreWhitespace == false}, inter-token whitespace will not be skipped.
* @throws NullPointerException If {@code str} is null.
*/
public JsonReader(final String str, final boolean ignoreWhitespace) {
+ this(str, ignoreWhitespace, true);
+ }
+
+ /**
+ * Construct a new {@link JsonReader} for JSON content to be read from the specified string.
+ *
+ * @param str The string with the JSON document to be read.
+ * @param strict Whether to enforce strict compliance to the JSON Specification
+ * while parsing the JSON document.
+ * @param ignoreWhitespace If {@code ignoreWhitespace == false}, inter-token whitespace will not be skipped.
+ * @throws NullPointerException If {@code str} is null.
+ */
+ public JsonReader(final String str, final boolean ignoreWhitespace, final boolean strict) {
super(new UnsynchronizedStringReader(str), DEFAULT_BUFFER_SIZE);
this.ignoreWhitespace = ignoreWhitespace;
+ this.strict = strict;
+ }
+
+ /**
+ * Returns whether this {@link JsonReader} skips inter-token whitespace.
+ *
+ * @return Whether this {@link JsonReader} skips inter-token whitespace.
+ */
+ public boolean isIgnoreWhitespace() {
+ return ignoreWhitespace;
+ }
+
+ /**
+ * Returns whether this {@link JsonReader} enforces strict compliance to the JSON
+ * Specification.
+ *
+ * @return Whether this {@link JsonReader} enforces strict compliance to the JSON
+ * Specification.
+ */
+ public boolean isStrict() {
+ return strict;
}
/**
@@ -826,7 +887,8 @@ else if (ch < '0' || '9' < ch) {
break;
}
else if (prev == '0' && i == (first == '~' ? 2 : 1)) {
- throw new JsonParseException("Leading zeros are not allowed", getPosition() - 2);
+ if (strict)
+ throw new JsonParseException("Leading zeros are not allowed", getPosition() - 2);
}
}
diff --git a/src/main/java/org/openjax/json/JsonUtil.java b/src/main/java/org/openjax/json/JsonUtil.java
index 199d29c..c449f56 100644
--- a/src/main/java/org/openjax/json/JsonUtil.java
+++ b/src/main/java/org/openjax/json/JsonUtil.java
@@ -64,6 +64,8 @@ public static boolean isWhitespace(final int ch) {
* @param