fix(serialize-json): allow singular name at document root

This commit is contained in:
Johannes Frohnmeyer 2024-04-20 16:39:18 +02:00
parent 2a9a6300ef
commit ae854cc040
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 9 additions and 3 deletions

View File

@ -574,7 +574,10 @@ public class JsonReader extends SerializeReader<IOException, JsonReader> impleme
default -> {
// If we are in an array, allow reading an in inferred name once
if (!wroteName) {
if (stack[stackSize - 1] == JsonScope.EMPTY_ARRAY || stack[stackSize - 1] == JsonScope.NONEMPTY_ARRAY) {
if (stack[stackSize - 1] == JsonScope.EMPTY_ARRAY
|| stack[stackSize - 1] == JsonScope.NONEMPTY_ARRAY
|| stack[stackSize - 1] == JsonScope.EMPTY_DOCUMENT
|| stack[stackSize - 1] == JsonScope.NONEMPTY_DOCUMENT) {
wroteName = true;
return heuristics.guessArrayElementName(getPath());
}

View File

@ -209,7 +209,9 @@ public class JsonWriter extends SerializeWriter<IOException, JsonWriter> impleme
int context = peek();
if (context != EMPTY_OBJECT && context != NONEMPTY_OBJECT) {
if (lenient) {
if (context != EMPTY_ARRAY && context != NONEMPTY_ARRAY) throw new IllegalStateException("Please begin an object or array before writing a name.");
if (context != EMPTY_ARRAY && context != NONEMPTY_ARRAY
&& context != EMPTY_DOCUMENT && context != NONEMPTY_DOCUMENT)
throw new IllegalStateException("Please begin an object or array before writing a name.");
} else {
throw new IllegalStateException("Please begin an object before writing a name.");
}
@ -221,7 +223,8 @@ public class JsonWriter extends SerializeWriter<IOException, JsonWriter> impleme
private void writeDeferredName() throws IOException {
if (deferredName != null) {
int context = peek();
if (context == EMPTY_ARRAY || context == NONEMPTY_ARRAY) {
if (context == EMPTY_ARRAY || context == NONEMPTY_ARRAY
|| context == EMPTY_DOCUMENT || context == NONEMPTY_DOCUMENT) {
if (commentUnexpectedNames) {
// Write the name as a comment instead of literally
comment(deferredName);