|
19 | 19 | import com.google.gson.internal.Excluder;
|
20 | 20 | import com.google.gson.stream.JsonReader;
|
21 | 21 | import com.google.gson.stream.JsonWriter;
|
| 22 | +import com.google.gson.stream.MalformedJsonException; |
22 | 23 | import java.io.IOException;
|
| 24 | +import java.io.StringReader; |
| 25 | +import java.io.StringWriter; |
23 | 26 | import java.lang.reflect.Field;
|
24 | 27 | import java.lang.reflect.Type;
|
25 | 28 | import java.text.DateFormat;
|
@@ -82,4 +85,71 @@ private static final class TestTypeAdapter extends TypeAdapter<Object> {
|
82 | 85 | }
|
83 | 86 | @Override public Object read(JsonReader in) throws IOException { return null; }
|
84 | 87 | }
|
| 88 | + |
| 89 | + public void testNewJsonWriter_Default() throws IOException { |
| 90 | + StringWriter writer = new StringWriter(); |
| 91 | + JsonWriter jsonWriter = new Gson().newJsonWriter(writer); |
| 92 | + jsonWriter.beginObject(); |
| 93 | + jsonWriter.name("test"); |
| 94 | + jsonWriter.nullValue(); |
| 95 | + jsonWriter.name("<test2"); |
| 96 | + jsonWriter.value(true); |
| 97 | + jsonWriter.endObject(); |
| 98 | + |
| 99 | + try { |
| 100 | + // Additional top-level value |
| 101 | + jsonWriter.value(1); |
| 102 | + fail(); |
| 103 | + } catch (IllegalStateException expected) { |
| 104 | + assertEquals("JSON must have only one top-level value.", expected.getMessage()); |
| 105 | + } |
| 106 | + |
| 107 | + jsonWriter.close(); |
| 108 | + assertEquals("{\"\\u003ctest2\":true}", writer.toString()); |
| 109 | + } |
| 110 | + |
| 111 | + public void testNewJsonWriter_Custom() throws IOException { |
| 112 | + StringWriter writer = new StringWriter(); |
| 113 | + JsonWriter jsonWriter = new GsonBuilder() |
| 114 | + .disableHtmlEscaping() |
| 115 | + .generateNonExecutableJson() |
| 116 | + .setPrettyPrinting() |
| 117 | + .serializeNulls() |
| 118 | + .setLenient() |
| 119 | + .create() |
| 120 | + .newJsonWriter(writer); |
| 121 | + jsonWriter.beginObject(); |
| 122 | + jsonWriter.name("test"); |
| 123 | + jsonWriter.nullValue(); |
| 124 | + jsonWriter.name("<test2"); |
| 125 | + jsonWriter.value(true); |
| 126 | + jsonWriter.endObject(); |
| 127 | + |
| 128 | + // Additional top-level value |
| 129 | + jsonWriter.value(1); |
| 130 | + |
| 131 | + jsonWriter.close(); |
| 132 | + assertEquals(")]}'\n{\n \"test\": null,\n \"<test2\": true\n}1", writer.toString()); |
| 133 | + } |
| 134 | + |
| 135 | + public void testNewJsonReader_Default() throws IOException { |
| 136 | + String json = "test"; // String without quotes |
| 137 | + JsonReader jsonReader = new Gson().newJsonReader(new StringReader(json)); |
| 138 | + try { |
| 139 | + jsonReader.nextString(); |
| 140 | + fail(); |
| 141 | + } catch (MalformedJsonException expected) { |
| 142 | + } |
| 143 | + jsonReader.close(); |
| 144 | + } |
| 145 | + |
| 146 | + public void testNewJsonReader_Custom() throws IOException { |
| 147 | + String json = "test"; // String without quotes |
| 148 | + JsonReader jsonReader = new GsonBuilder() |
| 149 | + .setLenient() |
| 150 | + .create() |
| 151 | + .newJsonReader(new StringReader(json)); |
| 152 | + assertEquals("test", jsonReader.nextString()); |
| 153 | + jsonReader.close(); |
| 154 | + } |
85 | 155 | }
|
0 commit comments