google / google/gson

JsonReader with strictness set to LENIENT,

Open
#2,769 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

# Gson version
2.11.0

# Java / Android version
any version

# Used tools
none

# Description
With a JsonReader with strictness = LENIENT, all values after a null are read as a single string begining by "null".

## Expected behavior
Values are correctly read.

# Reproduction steps
```java
public static void test(Stream stream) throws IOException {
Iterator iterator = stream.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);

System.out.println(TypeAdapters.STRING.read(reader));
System.out.println(TypeAdapters.STRING.read(reader));
}
```

With `stream = Stream.of("value1", "value2");` -> OK
```
value1
value2
```

With `stream = Stream.of("value1", null);`, -> OK
```
value1
null
```
(the printed null is really a null value, not "null" String -> OK)


With `stream = Stream.of(null, "value1");` -> KO
```
null"value1"
java.lang.IllegalStateException: Expected a string but was END_DOCUMENT at line 1 column 13 path $
```



With `stream = Stream.of(null, "value1", "value2");` -> KO (and so on)
```
null"value1""value2"
Exception in thread "main" java.lang.IllegalStateException: Expected a string but was END_DOCUMENT at line 1 column 21 path $
```

With `stream = Stream.of("value1", null, "value2");` -> KO
```
value1
null"value2"
```


With `stream = Stream.of("value1", null, "value2", "value3");` -> KO (and so on)
```
value1
null"value2""value3"
```

Same behavior happens with other json primitive type but not with json object or json array.
Save behavior happens in previous gson version with `#setLenient(true);`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.