From 3ceb0bbe14f02628fd6e655151e7ad97ab16b7d6 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sat, 29 Jul 2023 23:57:02 +0200 Subject: [PATCH] Minor follow-up changes --- Troubleshooting.md | 9 +++-- gson/src/main/java/com/google/gson/Gson.java | 8 +++- .../com/google/gson/stream/JsonReader.java | 4 +- .../com/google/gson/stream/JsonWriter.java | 4 +- .../google/gson/stream/JsonReaderTest.java | 24 +++++------ .../google/gson/stream/JsonWriterTest.java | 40 ++++++------------- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/Troubleshooting.md b/Troubleshooting.md index e6f21034fe..6bf2b8579a 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -147,9 +147,12 @@ To spot syntax errors in the JSON data easily you can open it in an editor with **Reason:** Due to legacy reasons Gson performs parsing by default in lenient mode -**Solution:** See [`Gson` class documentation](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#default-lenient) section "Lenient JSON handling" - -Note: Even in non-lenient mode Gson deviates slightly from the JSON specification, see [`JsonReader.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setStrictness(Strictness)) for more details. +**Solution:** If you are using Gson 2.11.0 or newer, call [`GsonBuilder.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#setStrictness(com.google.gson.Strictness)), +[`JsonReader.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setStrictness(com.google.gson.Strictness)) +and [`JsonWriter.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonWriter.html#setStrictness(com.google.gson.Strictness)) +with `Strictness.STRICT` to overwrite the default lenient behavior of `Gson` and make these classes strictly adhere to the JSON specification. +Otherwise if you are using an older Gson version, see the [`Gson` class documentation](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#default-lenient) +section "JSON Strictness handling" for alternative solutions. ## `IllegalStateException`: "Expected ... but was ..." diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 186c436f48..0219c1a186 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -136,6 +136,10 @@ * to make sure there is no trailing data * * + * Note that the {@code JsonReader} created this way is only 'legacy strict', it mostly adheres + * to the JSON specification but allows small deviations. See {@link JsonReader#setStrictness(Strictness)} + * for details. + * * @see TypeToken * * @author Inderjeet Singh @@ -915,7 +919,7 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce *
  • {@link GsonBuilder#serializeNulls()}
  • *
  • {@link GsonBuilder#setStrictness(Strictness)}. If no * {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness has been set} the created - * writer will have a strictness of {@link Strictness#LEGACY_STRICT}. Otherwise, this strictness of + * writer will have a strictness of {@link Strictness#LEGACY_STRICT}. Otherwise, the strictness of * the {@code Gson} instance will be used for the created writer.
  • *
  • {@link GsonBuilder#setPrettyPrinting()}
  • *
  • {@link GsonBuilder#setFormattingStyle(FormattingStyle)}
  • @@ -940,7 +944,7 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException { * */ diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index dfa4b6e8f4..0d106a1ccc 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -182,7 +182,7 @@ *

    Prefixing JSON files with ")]}'\n" makes them non-executable * by {@code