-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add better support for multiple top-level values #2783
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,8 +70,9 @@ | |
* The behavior of this reader can be customized with the following methods: | ||
* | ||
* <ul> | ||
* <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT} | ||
* <li>{@link #setMultiTopLevelValuesAllowed(boolean)}, the default is {@code false} | ||
* <li>{@link #setNestingLimit(int)}, the default is {@value #DEFAULT_NESTING_LIMIT} | ||
* <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT} | ||
* </ul> | ||
* | ||
* The default configuration of {@code JsonReader} instances used internally by the {@link Gson} | ||
|
@@ -257,6 +258,8 @@ public class JsonReader implements Closeable { | |
static final int DEFAULT_NESTING_LIMIT = 255; | ||
private int nestingLimit = DEFAULT_NESTING_LIMIT; | ||
|
||
private boolean multiTopLevelValuesEnabled = false; | ||
|
||
static final int BUFFER_SIZE = 1024; | ||
|
||
/** | ||
|
@@ -380,7 +383,8 @@ public final boolean isLenient() { | |
* <li>Streams that start with the <a href="#nonexecuteprefix">non-execute prefix</a>, | ||
* {@code ")]}'\n"} | ||
* <li>Streams that include multiple top-level values. With legacy strict or strict | ||
* parsing, each stream must contain exactly one top-level value. | ||
* parsing, each stream must contain exactly one top-level value. Can be enabled | ||
* independently using {@link #setMultiTopLevelValuesAllowed(boolean)}. | ||
* <li>Numbers may be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() | ||
* infinities} represented by {@code NaN} and {@code (-)Infinity} respectively. | ||
* <li>End of line comments starting with {@code //} or {@code #} and ending with a | ||
|
@@ -402,8 +406,8 @@ public final boolean isLenient() { | |
* @since 2.11.0 | ||
*/ | ||
public final void setStrictness(Strictness strictness) { | ||
Objects.requireNonNull(strictness); | ||
this.strictness = strictness; | ||
this.strictness = Objects.requireNonNull(strictness); | ||
setMultiTopLevelValuesAllowed(strictness == Strictness.LENIENT); | ||
} | ||
|
||
/** | ||
|
@@ -416,6 +420,31 @@ public final Strictness getStrictness() { | |
return strictness; | ||
} | ||
|
||
/** | ||
* Sets whether multiple top-level values are allowed. Only whitespace is supported as separator | ||
* between top-level values. Values may also be concatenated without any whitespace in between, | ||
* but for some values this causes ambiguities, for example for JSON numbers as top-level values. | ||
* | ||
* <p>This setting overwrites and is overwritten by whether {@link #setStrictness(Strictness)} | ||
* enabled support for multiple top-level values. | ||
* | ||
* @see #isMultiTopLevelValuesAllowed() | ||
* @since $next-version$ | ||
*/ | ||
public final void setMultiTopLevelValuesAllowed(boolean enabled) { | ||
this.multiTopLevelValuesEnabled = enabled; | ||
} | ||
|
||
/** | ||
* Returns whether multiple top-level values are allowed. | ||
* | ||
* @see #setMultiTopLevelValuesAllowed(boolean) | ||
* @since $next-version$ | ||
*/ | ||
public final boolean isMultiTopLevelValuesAllowed() { | ||
return multiTopLevelValuesEnabled; | ||
} | ||
|
||
/** | ||
* Sets the nesting limit of this reader. | ||
* | ||
|
@@ -661,10 +690,13 @@ int doPeek() throws IOException { | |
int c = nextNonWhitespace(false); | ||
if (c == -1) { | ||
return peeked = PEEKED_EOF; | ||
} else { | ||
checkLenient(); | ||
pos--; | ||
} else if (!multiTopLevelValuesEnabled) { | ||
throw new MalformedJsonException( | ||
"Multiple top-level values support has not been enabled, use" | ||
+ " `JsonReader.setMultiTopLevelValuesAllowed(true)`," | ||
+ locationString()); | ||
} | ||
pos--; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this |
||
} else if (peekStack == JsonScope.CLOSED) { | ||
throw new IllegalStateException("JsonReader is closed"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
* output | ||
* <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT} | ||
* <li>{@link #setSerializeNulls(boolean)}, by default {@code null} is serialized | ||
* <li>{@link #setTopLevelSeparator(String)}, by default {@code null} (= disabled) | ||
* </ul> | ||
* | ||
* The default configuration of {@code JsonWriter} instances used internally by the {@link Gson} | ||
|
@@ -218,6 +219,11 @@ public class JsonWriter implements Closeable, Flushable { | |
|
||
private Strictness strictness = Strictness.LEGACY_STRICT; | ||
|
||
/** | ||
* Separator between top-level values; {@code null} if multiple top-level values are not allowed | ||
*/ | ||
private String topLevelSeparator = null; | ||
|
||
private boolean htmlSafe; | ||
|
||
private String deferredName; | ||
|
@@ -334,9 +340,13 @@ public boolean isLenient() { | |
* <dd>The behavior of these is currently identical. In these strictness modes, the writer only | ||
* writes JSON in accordance with RFC 8259. | ||
* <dt>{@link Strictness#LENIENT} | ||
* <dd>This mode relaxes the behavior of the writer to allow the writing of {@link | ||
* Double#isNaN() NaNs} and {@link Double#isInfinite() infinities}. It also allows writing | ||
* multiple top level values. | ||
* <dd>In lenient mode, the following departures from RFC 8259 are permitted: | ||
* <ul> | ||
* <li>Writing of {@link Double#isNaN() NaNs} and {@link Double#isInfinite() infinities}. | ||
* <li>Writing multiple top level values. The values are concatenated without any | ||
* separating whitespace. Can be enabled independently and can be further customized | ||
* using {@link #setTopLevelSeparator(String)}. | ||
* </ul> | ||
* </dl> | ||
* | ||
* @param strictness the new strictness of this writer. May not be {@code null}. | ||
|
@@ -345,6 +355,7 @@ public boolean isLenient() { | |
*/ | ||
public final void setStrictness(Strictness strictness) { | ||
this.strictness = Objects.requireNonNull(strictness); | ||
setTopLevelSeparator(strictness == Strictness.LENIENT ? "" : null); | ||
} | ||
|
||
/** | ||
|
@@ -357,6 +368,33 @@ public final Strictness getStrictness() { | |
return strictness; | ||
} | ||
|
||
/** | ||
* Sets the separator to use between multiple top-level values. When writing multiple top-level | ||
* values the separator is written between the values, this can for example be useful for formats | ||
* such as <a href="https://jsonlines.org/">JSON Lines</a>.<br> | ||
* Using {@code null} disables support for multiple top-level values (the default). | ||
* | ||
* <p>This setting overwrites and is overwritten by whether {@link #setStrictness(Strictness)} | ||
* enabled support for multiple top-level values. | ||
* | ||
* @param separator separator between top-level values, or {@code null} to disable. | ||
* @see #getTopLevelSeparator() | ||
* @since $next-version$ | ||
*/ | ||
public final void setTopLevelSeparator(String separator) { | ||
this.topLevelSeparator = separator; | ||
} | ||
|
||
/** | ||
* Returns the top-level separator, or {@code null} if disabled. | ||
* | ||
* @see #setTopLevelSeparator(String) | ||
* @since $next-version$ | ||
*/ | ||
public final String getTopLevelSeparator() { | ||
return topLevelSeparator; | ||
} | ||
|
||
/** | ||
* Configures this writer to emit JSON that's safe for direct inclusion in HTML and XML documents. | ||
* This escapes the HTML characters {@code <}, {@code >}, {@code &}, {@code =} and {@code '} | ||
|
@@ -807,10 +845,14 @@ private void beforeName() throws IOException { | |
private void beforeValue() throws IOException { | ||
switch (peek()) { | ||
case NONEMPTY_DOCUMENT: | ||
if (strictness != Strictness.LENIENT) { | ||
throw new IllegalStateException("JSON must have only one top-level value."); | ||
if (topLevelSeparator == null) { | ||
throw new IllegalStateException( | ||
"Multiple top-level values support has not been enabled, use" | ||
+ " `JsonWriter.setTopLevelSeparator(String)`"); | ||
} | ||
// fall-through | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this "fall-through" was previously redundant: The next case is |
||
out.append(topLevelSeparator); | ||
break; | ||
|
||
case EMPTY_DOCUMENT: // first in document | ||
replaceTop(NONEMPTY_DOCUMENT); | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not completely happy about these method names
setMultiTopLevelValuesAllowed
andisMultiTopLevelValuesAllowed
yet. Any feedback and suggestions are welcome.