Skip to content

Commit

Permalink
Fix Gson.newJsonWriter ignoring lenient and HTML-safe setting (goog…
Browse files Browse the repository at this point in the history
…le#1989)

* Improve Gson newJsonWriter and newJsonReader documentation

* Consider lenient and HTML-safe setting for Gson.newJsonWriter

* Remove empty line between imports
  • Loading branch information
Marcono1234 authored and tibor-universe committed Nov 17, 2021
1 parent 9ee4f3a commit 08bee30
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,15 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce

/**
* Returns a new JSON writer configured for the settings on this Gson instance.
*
* <p>The following settings are considered:
* <ul>
* <li>{@link GsonBuilder#disableHtmlEscaping()}</li>
* <li>{@link GsonBuilder#generateNonExecutableJson()}</li>
* <li>{@link GsonBuilder#serializeNulls()}</li>
* <li>{@link GsonBuilder#setLenient()}</li>
* <li>{@link GsonBuilder#setPrettyPrinting()}</li>
* </ul>
*/
public JsonWriter newJsonWriter(Writer writer) throws IOException {
if (generateNonExecutableJson) {
Expand All @@ -792,12 +801,19 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException {
if (prettyPrinting) {
jsonWriter.setIndent(" ");
}
jsonWriter.setHtmlSafe(htmlSafe);
jsonWriter.setLenient(lenient);
jsonWriter.setSerializeNulls(serializeNulls);
return jsonWriter;
}

/**
* Returns a new JSON reader configured for the settings on this Gson instance.
*
* <p>The following settings are considered:
* <ul>
* <li>{@link GsonBuilder#setLenient()}</li>
* </ul>
*/
public JsonReader newJsonReader(Reader reader) {
JsonReader jsonReader = new JsonReader(reader);
Expand Down
70 changes: 70 additions & 0 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
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;
Expand Down Expand Up @@ -82,4 +85,71 @@ private static final class TestTypeAdapter extends TypeAdapter<Object> {
}
@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("<test2");
jsonWriter.value(true);
jsonWriter.endObject();

try {
// Additional top-level value
jsonWriter.value(1);
fail();
} catch (IllegalStateException expected) {
assertEquals("JSON must have only one top-level value.", expected.getMessage());
}

jsonWriter.close();
assertEquals("{\"\\u003ctest2\":true}", writer.toString());
}

public void testNewJsonWriter_Custom() throws IOException {
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new GsonBuilder()
.disableHtmlEscaping()
.generateNonExecutableJson()
.setPrettyPrinting()
.serializeNulls()
.setLenient()
.create()
.newJsonWriter(writer);
jsonWriter.beginObject();
jsonWriter.name("test");
jsonWriter.nullValue();
jsonWriter.name("<test2");
jsonWriter.value(true);
jsonWriter.endObject();

// Additional top-level value
jsonWriter.value(1);

jsonWriter.close();
assertEquals(")]}'\n{\n \"test\": null,\n \"<test2\": true\n}1", writer.toString());
}

public void testNewJsonReader_Default() throws IOException {
String json = "test"; // String without quotes
JsonReader jsonReader = new Gson().newJsonReader(new StringReader(json));
try {
jsonReader.nextString();
fail();
} catch (MalformedJsonException expected) {
}
jsonReader.close();
}

public void testNewJsonReader_Custom() throws IOException {
String json = "test"; // String without quotes
JsonReader jsonReader = new GsonBuilder()
.setLenient()
.create()
.newJsonReader(new StringReader(json));
assertEquals("test", jsonReader.nextString());
jsonReader.close();
}
}

0 comments on commit 08bee30

Please sign in to comment.