-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add better support for multiple top-level values #2783
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
Open
Marcono1234
wants to merge
5
commits into
google:main
Choose a base branch
from
Marcono1234:marcono1234/multi-top-level-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4732c7f
Add better support for multiple top-level values
Marcono1234 89834ea
Merge branch 'main' into marcono1234/multi-top-level-value
Marcono1234 c8c9814
Extend tests
Marcono1234 7960590
Rename JsonReader methods to suggested names
Marcono1234 acb3990
Sort methods alphabetically & fix duplicate line from merge
Marcono1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,8 +68,9 @@ | |
| * FormattingStyle#COMPACT} | ||
| * <li>{@link #setHtmlSafe(boolean)}, by default HTML characters are not escaped in the JSON | ||
| * output | ||
| * <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT} | ||
| * <li>{@link #setSerializeNulls(boolean)}, by default {@code null} is serialized | ||
| * <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT} | ||
| * <li>{@link #setTopLevelSeparator(String)}, by default {@code null} (= disabled) | ||
| * </ul> | ||
| * | ||
| * The default configuration of {@code JsonWriter} instances used internally by the {@link | ||
|
|
@@ -217,6 +218,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; | ||
|
|
@@ -333,9 +339,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}. | ||
|
|
@@ -344,6 +354,7 @@ public boolean isLenient() { | |
| */ | ||
| public final void setStrictness(Strictness strictness) { | ||
| this.strictness = Objects.requireNonNull(strictness); | ||
| setTopLevelSeparator(strictness == Strictness.LENIENT ? "" : null); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -356,6 +367,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) { | ||
|
Contributor
Author
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. Not sure if we should also support an It seems JSON Lines recommends adding a trailing line break, though does not require it. Not sure about other multi-value JSON standards. |
||
| 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 '} | ||
|
|
@@ -806,10 +844,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 | ||
| out.append(topLevelSeparator); | ||
| break; | ||
|
|
||
| case EMPTY_DOCUMENT: // first in document | ||
| replaceTop(NONEMPTY_DOCUMENT); | ||
| break; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.