Fix Gson.newJsonWriter ignoring lenient and HTML-safe setting (#1989)

* Improve Gson newJsonWriter and newJsonReader documentation

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

* Remove empty line between imports
This commit is contained in:
Marcono1234 2021-11-01 23:11:10 +01:00 committed by GitHub
parent e0de45ff69
commit deaa3a6cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 0 deletions

View File

@ -778,6 +778,15 @@ public final class Gson {
/**
* 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) {
@ -787,12 +796,19 @@ public final class Gson {
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);

View File

@ -19,7 +19,10 @@ package com.google.gson;
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 +85,71 @@ public final class GsonTest extends TestCase {
}
@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();
}
}