Skip to content

JsonReader with strictness set to LENIENT, #2769

@d-william

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

public static void test(Stream<String> stream) throws IOException {
    Iterator<String> 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);.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions