Merge pull request #228 from square/jwilson.0122.json_writer_tests

Prepare tests for a 2nd implementation of JsonWriter.
This commit is contained in:
Jake Wharton
2017-01-23 17:32:44 -05:00
committed by GitHub
5 changed files with 375 additions and 329 deletions

View File

@@ -18,68 +18,83 @@ package com.squareup.moshi;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import okio.Buffer; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@RunWith(Parameterized.class)
public final class BufferedSinkJsonWriterTest { public final class BufferedSinkJsonWriterTest {
@Parameter public JsonWriterFactory factory;
@Parameters(name = "{0}")
public static List<Object[]> parameters() {
return JsonWriterFactory.factories();
}
@Test public void nullsValuesNotSerializedByDefault() throws IOException { @Test public void nullsValuesNotSerializedByDefault() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a"); writer.nullValue();
jsonWriter.nullValue(); writer.endObject();
jsonWriter.endObject(); writer.close();
jsonWriter.close(); assertThat(factory.json()).isEqualTo("{}");
assertThat(buffer.readUtf8()).isEqualTo("{}");
} }
@Test public void nullsValuesSerializedWhenConfigured() throws IOException { @Test public void nullsValuesSerializedWhenConfigured() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.setSerializeNulls(true);
jsonWriter.setSerializeNulls(true); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a"); writer.nullValue();
jsonWriter.nullValue(); writer.endObject();
jsonWriter.endObject(); writer.close();
jsonWriter.close(); assertThat(factory.json()).isEqualTo("{\"a\":null}");
assertThat(buffer.readUtf8()).isEqualTo("{\"a\":null}");
} }
@Test public void topLevelValueTypes() throws IOException { @Test public void topLevelBoolean() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
writer.value(true);
writer.close();
assertThat(factory.json()).isEqualTo("true");
}
JsonWriter writer1 = JsonWriter.of(buffer); @Test public void topLevelNull() throws IOException {
writer1.value(true); JsonWriter writer = factory.newWriter();
writer1.close(); writer.nullValue();
assertThat(buffer.readUtf8()).isEqualTo("true"); writer.close();
assertThat(factory.json()).isEqualTo("null");
}
JsonWriter writer2 = JsonWriter.of(buffer); @Test public void topLevelInt() throws IOException {
writer2.nullValue(); JsonWriter writer = factory.newWriter();
writer2.close(); writer.value(123);
assertThat(buffer.readUtf8()).isEqualTo("null"); writer.close();
assertThat(factory.json()).isEqualTo("123");
}
JsonWriter writer3 = JsonWriter.of(buffer); @Test public void topLevelDouble() throws IOException {
writer3.value(123); JsonWriter writer = factory.newWriter();
writer3.close(); writer.value(123.4);
assertThat(buffer.readUtf8()).isEqualTo("123"); writer.close();
assertThat(factory.json()).isEqualTo("123.4");
}
JsonWriter writer4 = JsonWriter.of(buffer); @Test public void topLevelString() throws IOException {
writer4.value(123.4); JsonWriter writer = factory.newWriter();
writer4.close(); writer.value("a");
assertThat(buffer.readUtf8()).isEqualTo("123.4"); writer.close();
assertThat(factory.json()).isEqualTo("\"a\"");
JsonWriter writer5 = JsonWriter.of(buffer);
writer5.value("a");
writer5.close();
assertThat(buffer.readUtf8()).isEqualTo("\"a\"");
} }
@Test public void invalidTopLevelTypes() throws IOException { @Test public void invalidTopLevelTypes() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.name("hello"); writer.name("hello");
try { try {
writer.value("world"); writer.value("world");
@@ -89,155 +104,144 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void twoNames() throws IOException { @Test public void twoNames() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a");
try { try {
jsonWriter.name("a"); writer.name("a");
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void nameWithoutValue() throws IOException { @Test public void nameWithoutValue() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a");
try { try {
jsonWriter.endObject(); writer.endObject();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void valueWithoutName() throws IOException { @Test public void valueWithoutName() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject();
try { try {
jsonWriter.value(true); writer.value(true);
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void multipleTopLevelValues() throws IOException { @Test public void multipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray().endArray();
jsonWriter.beginArray().endArray();
try { try {
jsonWriter.beginArray(); writer.beginArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void badNestingObject() throws IOException { @Test public void badNestingObject() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.beginObject();
jsonWriter.beginObject();
try { try {
jsonWriter.endArray(); writer.endArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void badNestingArray() throws IOException { @Test public void badNestingArray() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.beginArray();
jsonWriter.beginArray();
try { try {
jsonWriter.endObject(); writer.endObject();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
} }
@Test public void nullName() throws IOException { @Test public void nullName() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject();
try { try {
jsonWriter.name(null); writer.name(null);
fail(); fail();
} catch (NullPointerException expected) { } catch (NullPointerException expected) {
} }
} }
@Test public void nullStringValue() throws IOException { @Test public void nullStringValue() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.setSerializeNulls(true);
jsonWriter.setSerializeNulls(true); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a"); writer.value((String) null);
jsonWriter.value((String) null); writer.endObject();
jsonWriter.endObject(); assertThat(factory.json()).isEqualTo("{\"a\":null}");
assertThat(buffer.readUtf8()).isEqualTo("{\"a\":null}");
} }
@Test public void nonFiniteDoubles() throws IOException { @Test public void nonFiniteDoubles() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray();
try { try {
jsonWriter.value(Double.NaN); writer.value(Double.NaN);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
try { try {
jsonWriter.value(Double.NEGATIVE_INFINITY); writer.value(Double.NEGATIVE_INFINITY);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
try { try {
jsonWriter.value(Double.POSITIVE_INFINITY); writer.value(Double.POSITIVE_INFINITY);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
} }
@Test public void nonFiniteBoxedDoubles() throws IOException { @Test public void nonFiniteBoxedDoubles() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray();
try { try {
jsonWriter.value(new Double(Double.NaN)); writer.value(new Double(Double.NaN));
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
try { try {
jsonWriter.value(new Double(Double.NEGATIVE_INFINITY)); writer.value(new Double(Double.NEGATIVE_INFINITY));
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
try { try {
jsonWriter.value(new Double(Double.POSITIVE_INFINITY)); writer.value(new Double(Double.POSITIVE_INFINITY));
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
} }
@Test public void doubles() throws IOException { @Test public void doubles() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value(-0.0);
jsonWriter.value(-0.0); writer.value(1.0);
jsonWriter.value(1.0); writer.value(Double.MAX_VALUE);
jsonWriter.value(Double.MAX_VALUE); writer.value(Double.MIN_VALUE);
jsonWriter.value(Double.MIN_VALUE); writer.value(0.0);
jsonWriter.value(0.0); writer.value(-0.5);
jsonWriter.value(-0.5); writer.value(2.2250738585072014E-308);
jsonWriter.value(2.2250738585072014E-308); writer.value(Math.PI);
jsonWriter.value(Math.PI); writer.value(Math.E);
jsonWriter.value(Math.E); writer.endArray();
jsonWriter.endArray(); writer.close();
jsonWriter.close(); assertThat(factory.json()).isEqualTo("[-0.0,"
assertThat(buffer.readUtf8()).isEqualTo("[-0.0,"
+ "1.0," + "1.0,"
+ "1.7976931348623157E308," + "1.7976931348623157E308,"
+ "4.9E-324," + "4.9E-324,"
@@ -249,17 +253,16 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void longs() throws IOException { @Test public void longs() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value(0);
jsonWriter.value(0); writer.value(1);
jsonWriter.value(1); writer.value(-1);
jsonWriter.value(-1); writer.value(Long.MIN_VALUE);
jsonWriter.value(Long.MIN_VALUE); writer.value(Long.MAX_VALUE);
jsonWriter.value(Long.MAX_VALUE); writer.endArray();
jsonWriter.endArray(); writer.close();
jsonWriter.close(); assertThat(factory.json()).isEqualTo("[0,"
assertThat(buffer.readUtf8()).isEqualTo("[0,"
+ "1," + "1,"
+ "-1," + "-1,"
+ "-9223372036854775808," + "-9223372036854775808,"
@@ -267,75 +270,70 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void numbers() throws IOException { @Test public void numbers() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value(new BigInteger("0"));
jsonWriter.value(new BigInteger("0")); writer.value(new BigInteger("9223372036854775808"));
jsonWriter.value(new BigInteger("9223372036854775808")); writer.value(new BigInteger("-9223372036854775809"));
jsonWriter.value(new BigInteger("-9223372036854775809")); writer.value(new BigDecimal("3.141592653589793238462643383"));
jsonWriter.value(new BigDecimal("3.141592653589793238462643383")); writer.endArray();
jsonWriter.endArray(); writer.close();
jsonWriter.close(); assertThat(factory.json()).isEqualTo("[0,"
assertThat(buffer.readUtf8()).isEqualTo("[0,"
+ "9223372036854775808," + "9223372036854775808,"
+ "-9223372036854775809," + "-9223372036854775809,"
+ "3.141592653589793238462643383]"); + "3.141592653589793238462643383]");
} }
@Test public void booleans() throws IOException { @Test public void booleans() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value(true);
jsonWriter.value(true); writer.value(false);
jsonWriter.value(false); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[true,false]");
assertThat(buffer.readUtf8()).isEqualTo("[true,false]");
} }
@Test public void boxedBooleans() throws IOException { @Test public void boxedBooleans() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value((Boolean) true);
jsonWriter.value((Boolean) true); writer.value((Boolean) false);
jsonWriter.value((Boolean) false); writer.value((Boolean) null);
jsonWriter.value((Boolean) null); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[true,false,null]");
assertThat(buffer.readUtf8()).isEqualTo("[true,false,null]");
} }
@Test public void nulls() throws IOException { @Test public void nulls() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.nullValue();
jsonWriter.nullValue(); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[null]");
assertThat(buffer.readUtf8()).isEqualTo("[null]");
} }
@Test public void strings() throws IOException { @Test public void strings() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value("a");
jsonWriter.value("a"); writer.value("a\"");
jsonWriter.value("a\""); writer.value("\"");
jsonWriter.value("\""); writer.value(":");
jsonWriter.value(":"); writer.value(",");
jsonWriter.value(","); writer.value("\b");
jsonWriter.value("\b"); writer.value("\f");
jsonWriter.value("\f"); writer.value("\n");
jsonWriter.value("\n"); writer.value("\r");
jsonWriter.value("\r"); writer.value("\t");
jsonWriter.value("\t"); writer.value(" ");
jsonWriter.value(" "); writer.value("\\");
jsonWriter.value("\\"); writer.value("{");
jsonWriter.value("{"); writer.value("}");
jsonWriter.value("}"); writer.value("[");
jsonWriter.value("["); writer.value("]");
jsonWriter.value("]"); writer.value("\0");
jsonWriter.value("\0"); writer.value("\u0019");
jsonWriter.value("\u0019"); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[\"a\","
assertThat(buffer.readUtf8()).isEqualTo("[\"a\","
+ "\"a\\\"\"," + "\"a\\\"\","
+ "\"\\\"\"," + "\"\\\"\","
+ "\":\"," + "\":\","
@@ -356,87 +354,80 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void unicodeLineBreaksEscaped() throws IOException { @Test public void unicodeLineBreaksEscaped() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.value("\u2028 \u2029");
jsonWriter.value("\u2028 \u2029"); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[\"\\u2028 \\u2029\"]");
assertThat(buffer.readUtf8()).isEqualTo("[\"\\u2028 \\u2029\"]");
} }
@Test public void emptyArray() throws IOException { @Test public void emptyArray() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[]");
assertThat(buffer.readUtf8()).isEqualTo("[]");
} }
@Test public void emptyObject() throws IOException { @Test public void emptyObject() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.endObject();
jsonWriter.endObject(); assertThat(factory.json()).isEqualTo("{}");
assertThat(buffer.readUtf8()).isEqualTo("{}");
} }
@Test public void objectsInArrays() throws IOException { @Test public void objectsInArrays() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginArray();
jsonWriter.beginArray(); writer.beginObject();
jsonWriter.beginObject(); writer.name("a").value(5);
jsonWriter.name("a").value(5); writer.name("b").value(false);
jsonWriter.name("b").value(false); writer.endObject();
jsonWriter.endObject(); writer.beginObject();
jsonWriter.beginObject(); writer.name("c").value(6);
jsonWriter.name("c").value(6); writer.name("d").value(true);
jsonWriter.name("d").value(true); writer.endObject();
jsonWriter.endObject(); writer.endArray();
jsonWriter.endArray(); assertThat(factory.json()).isEqualTo("[{\"a\":5,\"b\":false},"
assertThat(buffer.readUtf8()).isEqualTo("[{\"a\":5,\"b\":false},"
+ "{\"c\":6,\"d\":true}]"); + "{\"c\":6,\"d\":true}]");
} }
@Test public void arraysInObjects() throws IOException { @Test public void arraysInObjects() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.name("a");
jsonWriter.name("a"); writer.beginArray();
jsonWriter.beginArray(); writer.value(5);
jsonWriter.value(5); writer.value(false);
jsonWriter.value(false); writer.endArray();
jsonWriter.endArray(); writer.name("b");
jsonWriter.name("b"); writer.beginArray();
jsonWriter.beginArray(); writer.value(6);
jsonWriter.value(6); writer.value(true);
jsonWriter.value(true); writer.endArray();
jsonWriter.endArray(); writer.endObject();
jsonWriter.endObject(); assertThat(factory.json()).isEqualTo("{\"a\":[5,false],"
assertThat(buffer.readUtf8()).isEqualTo("{\"a\":[5,false],"
+ "\"b\":[6,true]}"); + "\"b\":[6,true]}");
} }
@Test public void deepNestingArrays() throws IOException { @Test public void deepNestingArrays() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.beginArray(); writer.beginArray();
} }
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.endArray(); writer.endArray();
} }
assertThat(buffer.readUtf8()) assertThat(factory.json())
.isEqualTo("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"); .isEqualTo("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]");
} }
@Test public void tooDeepNestingArrays() throws IOException { @Test public void tooDeepNestingArrays() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.beginArray(); writer.beginArray();
} }
try { try {
jsonWriter.beginArray(); writer.beginArray();
fail(); fail();
} catch (JsonDataException expected) { } catch (JsonDataException expected) {
assertThat(expected).hasMessage("Nesting too deep at $[0][0][0][0][0][0][0][0][0][0][0][0][0]" assertThat(expected).hasMessage("Nesting too deep at $[0][0][0][0][0][0][0][0][0][0][0][0][0]"
@@ -445,31 +436,29 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void deepNestingObjects() throws IOException { @Test public void deepNestingObjects() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.beginObject(); writer.beginObject();
jsonWriter.name("a"); writer.name("a");
} }
jsonWriter.value(true); writer.value(true);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.endObject(); writer.endObject();
} }
assertThat(buffer.readUtf8()).isEqualTo("" assertThat(factory.json()).isEqualTo(""
+ "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":" + "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":"
+ "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":" + "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":"
+ "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":true}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"); + "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":true}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}");
} }
@Test public void tooDeepNestingObjects() throws IOException { @Test public void tooDeepNestingObjects() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
jsonWriter.beginObject(); writer.beginObject();
jsonWriter.name("a"); writer.name("a");
} }
try { try {
jsonWriter.beginObject(); writer.beginObject();
fail(); fail();
} catch (JsonDataException expected) { } catch (JsonDataException expected) {
assertThat(expected).hasMessage("Nesting too deep at $.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a." assertThat(expected).hasMessage("Nesting too deep at $.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a."
@@ -478,36 +467,34 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void repeatedName() throws IOException { @Test public void repeatedName() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.beginObject();
jsonWriter.beginObject(); writer.name("a").value(true);
jsonWriter.name("a").value(true); writer.name("a").value(false);
jsonWriter.name("a").value(false); writer.endObject();
jsonWriter.endObject();
// JsonWriter doesn't attempt to detect duplicate names // JsonWriter doesn't attempt to detect duplicate names
assertThat(buffer.readUtf8()).isEqualTo("{\"a\":true,\"a\":false}"); assertThat(factory.json()).isEqualTo("{\"a\":true,\"a\":false}");
} }
@Test public void prettyPrintObject() throws IOException { @Test public void prettyPrintObject() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.setSerializeNulls(true);
jsonWriter.setSerializeNulls(true); writer.setIndent(" ");
jsonWriter.setIndent(" ");
jsonWriter.beginObject(); writer.beginObject();
jsonWriter.name("a").value(true); writer.name("a").value(true);
jsonWriter.name("b").value(false); writer.name("b").value(false);
jsonWriter.name("c").value(5.0); writer.name("c").value(5.0);
jsonWriter.name("e").nullValue(); writer.name("e").nullValue();
jsonWriter.name("f").beginArray(); writer.name("f").beginArray();
jsonWriter.value(6.0); writer.value(6.0);
jsonWriter.value(7.0); writer.value(7.0);
jsonWriter.endArray(); writer.endArray();
jsonWriter.name("g").beginObject(); writer.name("g").beginObject();
jsonWriter.name("h").value(8.0); writer.name("h").value(8.0);
jsonWriter.name("i").value(9.0); writer.name("i").value(9.0);
jsonWriter.endObject(); writer.endObject();
jsonWriter.endObject(); writer.endObject();
String expected = "{\n" String expected = "{\n"
+ " \"a\": true,\n" + " \"a\": true,\n"
@@ -523,28 +510,27 @@ public final class BufferedSinkJsonWriterTest {
+ " \"i\": 9.0\n" + " \"i\": 9.0\n"
+ " }\n" + " }\n"
+ "}"; + "}";
assertThat(buffer.readUtf8()).isEqualTo(expected); assertThat(factory.json()).isEqualTo(expected);
} }
@Test public void prettyPrintArray() throws IOException { @Test public void prettyPrintArray() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter jsonWriter = JsonWriter.of(buffer); writer.setIndent(" ");
jsonWriter.setIndent(" ");
jsonWriter.beginArray(); writer.beginArray();
jsonWriter.value(true); writer.value(true);
jsonWriter.value(false); writer.value(false);
jsonWriter.value(5.0); writer.value(5.0);
jsonWriter.nullValue(); writer.nullValue();
jsonWriter.beginObject(); writer.beginObject();
jsonWriter.name("a").value(6.0); writer.name("a").value(6.0);
jsonWriter.name("b").value(7.0); writer.name("b").value(7.0);
jsonWriter.endObject(); writer.endObject();
jsonWriter.beginArray(); writer.beginArray();
jsonWriter.value(8.0); writer.value(8.0);
jsonWriter.value(9.0); writer.value(9.0);
jsonWriter.endArray(); writer.endArray();
jsonWriter.endArray(); writer.endArray();
String expected = "[\n" String expected = "[\n"
+ " true,\n" + " true,\n"
@@ -560,24 +546,22 @@ public final class BufferedSinkJsonWriterTest {
+ " 9.0\n" + " 9.0\n"
+ " ]\n" + " ]\n"
+ "]"; + "]";
assertThat(buffer.readUtf8()).isEqualTo(expected); assertThat(factory.json()).isEqualTo(expected);
} }
@Test public void lenientWriterPermitsMultipleTopLevelValues() throws IOException { @Test public void lenientWriterPermitsMultipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.setLenient(true); writer.setLenient(true);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
assertThat(buffer.readUtf8()).isEqualTo("[][]"); assertThat(factory.json()).isEqualTo("[][]");
} }
@Test public void strictWriterDoesNotPermitMultipleTopLevelValues() throws IOException { @Test public void strictWriterDoesNotPermitMultipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
try { try {
@@ -588,8 +572,7 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void closedWriterThrowsOnStructure() throws IOException { @Test public void closedWriterThrowsOnStructure() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -616,8 +599,7 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void closedWriterThrowsOnName() throws IOException { @Test public void closedWriterThrowsOnName() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -629,8 +611,7 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void closedWriterThrowsOnValue() throws IOException { @Test public void closedWriterThrowsOnValue() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -642,8 +623,7 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void closedWriterThrowsOnFlush() throws IOException { @Test public void closedWriterThrowsOnFlush() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -655,8 +635,7 @@ public final class BufferedSinkJsonWriterTest {
} }
@Test public void writerCloseIsIdempotent() throws IOException { @Test public void writerCloseIsIdempotent() throws IOException {
Buffer buffer = new Buffer(); JsonWriter writer = factory.newWriter();
JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();

View File

@@ -21,34 +21,51 @@ import java.util.List;
import okio.Buffer; import okio.Buffer;
abstract class JsonReaderFactory { abstract class JsonReaderFactory {
public static final JsonReaderFactory BUFFERED_SOURCE = new JsonReaderFactory() {
@Override public JsonReader newReader(String json) {
Buffer buffer = new Buffer().writeUtf8(json);
return JsonReader.of(buffer);
}
@Override public String toString() {
return "BufferedSourceJsonReader";
}
};
public static final JsonReaderFactory JSON_OBJECT = new JsonReaderFactory() {
@Override public JsonReader newReader(String json) throws IOException {
Moshi moshi = new Moshi.Builder().build();
Object object = moshi.adapter(Object.class).lenient().fromJson(json);
return new ObjectJsonReader(object);
}
@Override public String toString() {
return "ObjectJsonReader";
}
};
static List<Object[]> factories() { static List<Object[]> factories() {
JsonReaderFactory bufferedSource = new JsonReaderFactory() {
@Override public JsonReader newReader(String json) {
Buffer buffer = new Buffer().writeUtf8(json);
return JsonReader.of(buffer);
}
@Override public boolean supportsMultipleTopLevelValuesInOneDocument() {
return true;
}
@Override public String toString() {
return "BufferedSourceJsonReader";
}
};
JsonReaderFactory jsonObject = new JsonReaderFactory() {
@Override public JsonReader newReader(String json) throws IOException {
Moshi moshi = new Moshi.Builder().build();
Object object = moshi.adapter(Object.class).lenient().fromJson(json);
return new ObjectJsonReader(object);
}
// TODO(jwilson): fix precision checks and delete his method.
@Override public boolean implementsStrictPrecision() {
return false;
}
@Override public String toString() {
return "ObjectJsonReader";
}
};
return Arrays.asList( return Arrays.asList(
new Object[] { BUFFERED_SOURCE }, new Object[] { bufferedSource },
new Object[] { JSON_OBJECT }); new Object[] { jsonObject });
} }
abstract JsonReader newReader(String json) throws IOException; abstract JsonReader newReader(String json) throws IOException;
public boolean supportsMultipleTopLevelValuesInOneDocument() {
return false;
}
public boolean implementsStrictPrecision() {
return true;
}
} }

View File

@@ -185,7 +185,7 @@ public final class JsonReaderPathTest {
} }
@Test public void multipleTopLevelValuesInOneDocument() throws IOException { @Test public void multipleTopLevelValuesInOneDocument() throws IOException {
assumeTrue(factory != JsonReaderFactory.JSON_OBJECT); assumeTrue(factory.supportsMultipleTopLevelValuesInOneDocument());
JsonReader reader = factory.newReader("[][]"); JsonReader reader = factory.newReader("[][]");
reader.setLenient(true); reader.setLenient(true);

View File

@@ -355,7 +355,8 @@ public final class JsonReaderTest {
} }
@Test public void longs() throws IOException { @Test public void longs() throws IOException {
assumeTrue(factory != JsonReaderFactory.JSON_OBJECT); // TODO(jwilson): fix precision checks. assumeTrue(factory.implementsStrictPrecision());
String json = "[0,0,0," String json = "[0,0,0,"
+ "1,1,1," + "1,1,1,"
+ "-1,-1,-1," + "-1,-1,-1,"
@@ -458,7 +459,7 @@ public final class JsonReaderTest {
} }
@Test public void integerMismatchWithDoubleDoesNotAdvance() throws IOException { @Test public void integerMismatchWithDoubleDoesNotAdvance() throws IOException {
assumeTrue(factory != JsonReaderFactory.JSON_OBJECT); // TODO(jwilson): fix precision checks. assumeTrue(factory.implementsStrictPrecision());
JsonReader reader = newReader("[1.5]"); JsonReader reader = newReader("[1.5]");
reader.beginArray(); reader.beginArray();
@@ -472,7 +473,7 @@ public final class JsonReaderTest {
} }
@Test public void integerMismatchWithLongDoesNotAdvance() throws IOException { @Test public void integerMismatchWithLongDoesNotAdvance() throws IOException {
assumeTrue(factory != JsonReaderFactory.JSON_OBJECT); // TODO(jwilson): fix precision checks. assumeTrue(factory.implementsStrictPrecision());
JsonReader reader = newReader("[9223372036854775807]"); JsonReader reader = newReader("[9223372036854775807]");
reader.beginArray(); reader.beginArray();
@@ -486,7 +487,7 @@ public final class JsonReaderTest {
} }
@Test public void longMismatchWithDoubleDoesNotAdvance() throws IOException { @Test public void longMismatchWithDoubleDoesNotAdvance() throws IOException {
assumeTrue(factory != JsonReaderFactory.JSON_OBJECT); // TODO(jwilson): fix precision checks. assumeTrue(factory.implementsStrictPrecision());
JsonReader reader = newReader("[1.5]"); JsonReader reader = newReader("[1.5]");
reader.beginArray(); reader.beginArray();

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
import java.util.Arrays;
import java.util.List;
import okio.Buffer;
abstract class JsonWriterFactory {
static List<Object[]> factories() {
final JsonWriterFactory bufferedSink = new JsonWriterFactory() {
Buffer buffer;
@Override JsonWriter newWriter() {
buffer = new Buffer();
return new BufferedSinkJsonWriter(buffer);
}
@Override String json() {
String result = buffer.readUtf8();
buffer = null;
return result;
}
@Override public String toString() {
return "BufferedSinkJsonWriter";
}
};
return Arrays.<Object[]>asList(
new Object[] { bufferedSink });
}
abstract JsonWriter newWriter();
abstract String json();
}