From c50e5b01691dc09ec8b9bffea2c2e150b97f1818 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 11 Oct 2021 15:07:43 +0200 Subject: [PATCH 1/3] Improve Gson newJsonWriter and newJsonReader documentation --- gson/src/main/java/com/google/gson/Gson.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 1511bbb178..4af5940d9a 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -756,6 +756,13 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce /** * Returns a new JSON writer configured for the settings on this Gson instance. + * + *

The following settings are considered: + *

*/ public JsonWriter newJsonWriter(Writer writer) throws IOException { if (generateNonExecutableJson) { @@ -771,6 +778,11 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException { /** * Returns a new JSON reader configured for the settings on this Gson instance. + * + *

The following settings are considered: + *

*/ public JsonReader newJsonReader(Reader reader) { JsonReader jsonReader = new JsonReader(reader); From 6ae97a458d560ebf054d713cd790ec19e16dcfd9 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 11 Oct 2021 15:28:02 +0200 Subject: [PATCH 2/3] Consider lenient and HTML-safe setting for Gson.newJsonWriter --- gson/src/main/java/com/google/gson/Gson.java | 4 ++ .../test/java/com/google/gson/GsonTest.java | 71 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 4af5940d9a..8e6df5b36a 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -759,8 +759,10 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce * *

The following settings are considered: *

*/ @@ -772,6 +774,8 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException { if (prettyPrinting) { jsonWriter.setIndent(" "); } + jsonWriter.setHtmlSafe(htmlSafe); + jsonWriter.setLenient(lenient); jsonWriter.setSerializeNulls(serializeNulls); return jsonWriter; } diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java index d537f7ad5b..3ac24e754d 100644 --- a/gson/src/test/java/com/google/gson/GsonTest.java +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -19,7 +19,11 @@ import com.google.gson.internal.Excluder; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; +import com.google.gson.stream.MalformedJsonException; + import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.text.DateFormat; @@ -82,4 +86,71 @@ private static final class TestTypeAdapter extends TypeAdapter { } @Override public Object read(JsonReader in) throws IOException { return null; } } + + public void testNewJsonWriter_Default() throws IOException { + StringWriter writer = new StringWriter(); + JsonWriter jsonWriter = new Gson().newJsonWriter(writer); + jsonWriter.beginObject(); + jsonWriter.name("test"); + jsonWriter.nullValue(); + jsonWriter.name(" Date: Mon, 25 Oct 2021 00:58:47 +0200 Subject: [PATCH 3/3] Remove empty line between imports --- gson/src/test/java/com/google/gson/GsonTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java index 3ac24e754d..5276371e88 100644 --- a/gson/src/test/java/com/google/gson/GsonTest.java +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -20,7 +20,6 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.google.gson.stream.MalformedJsonException; - import java.io.IOException; import java.io.StringReader; import java.io.StringWriter;