Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions gson/src/main/java/com/google/gson/Gson.java
Expand Up @@ -756,6 +756,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 @@ -765,12 +774,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
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();
}
}