Skip to content
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

bug fix issue #2769 #2779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ private String nextUnquotedValue() throws IOException {
while (true) {
for (; pos + i < limit; i++) {
switch (buffer[pos + i]) {
case '"':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for consistency it would also be necessary to change JsonReader#skipUnquotedValue so that reading a value and skipping a value is consistent, and adding a test for this.

Not sure if JsonReader#isLiteral would have to be changed as well, maybe not.

case '/':
case '\\':
case ';':
Expand Down
25 changes: 25 additions & 0 deletions gson/src/test/java/com/google/gson/stream/JsonReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import static org.junit.Assert.assertThrows;

import com.google.gson.Strictness;
import com.google.gson.internal.bind.TypeAdapters;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Stream;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -2170,6 +2174,27 @@ private static void assertDocument(String document, Object... expectations) thro
}
}

@Test
public void testJsonReaderWithStrictnessSetToLenientAndNullValue() throws IOException {
Iterator<String> iterator = Stream.of(null, "value1", "value2").iterator();
StringWriter str = new StringWriter();

try (JsonWriter writer = new JsonWriter(str)) {
writer.setStrictness(Strictness.LENIENT);
while (iterator.hasNext()) {
TypeAdapters.STRING.write(writer, iterator.next());
}
writer.flush();
}

JsonReader reader = new JsonReader(new StringReader(str.toString()));
reader.setStrictness(Strictness.LENIENT);

assertThat(TypeAdapters.STRING.read(reader)).isEqualTo("null");
assertThat(TypeAdapters.STRING.read(reader)).isEqualTo("value1");
assertThat(TypeAdapters.STRING.read(reader)).isEqualTo("value2");
}

/** Returns a reader that returns one character at a time. */
private static Reader reader(String s) {
/* if (true) */ return new StringReader(s);
Expand Down
Loading