JsonReader.of() and JsonWriter.of() instead of constructors.

This opens the door to later implementations of these types that write
to something other than a stream. In particular, we could have a
JsonReader that reads from a DOM-like object, or a JsonWriter that
creates such an object.
This commit is contained in:
jwilson
2015-09-27 12:09:32 -04:00
parent 711b96f716
commit e181db0015
10 changed files with 86 additions and 78 deletions

View File

@@ -30,7 +30,7 @@ public abstract class JsonAdapter<T> {
public abstract T fromJson(JsonReader reader) throws IOException; public abstract T fromJson(JsonReader reader) throws IOException;
public final T fromJson(BufferedSource source) throws IOException { public final T fromJson(BufferedSource source) throws IOException {
return fromJson(new JsonReader(source)); return fromJson(JsonReader.of(source));
} }
public final T fromJson(String string) throws IOException { public final T fromJson(String string) throws IOException {
@@ -40,7 +40,7 @@ public abstract class JsonAdapter<T> {
public abstract void toJson(JsonWriter writer, T value) throws IOException; public abstract void toJson(JsonWriter writer, T value) throws IOException;
public final void toJson(BufferedSink sink, T value) throws IOException { public final void toJson(BufferedSink sink, T value) throws IOException {
JsonWriter writer = new JsonWriter(sink); JsonWriter writer = JsonWriter.of(sink);
toJson(writer, value); toJson(writer, value);
} }

View File

@@ -84,7 +84,7 @@ import okio.ByteString;
* This code implements the parser for the above structure: <pre> {@code * This code implements the parser for the above structure: <pre> {@code
* *
* public List<Message> readJsonStream(BufferedSource source) throws IOException { * public List<Message> readJsonStream(BufferedSource source) throws IOException {
* JsonReader reader = new JsonReader(source); * JsonReader reader = JsonReader.of(source);
* try { * try {
* return readMessagesArray(reader); * return readMessagesArray(reader);
* } finally { * } finally {
@@ -171,7 +171,7 @@ import okio.ByteString;
* <p>Each {@code JsonReader} may be used to read a single JSON stream. Instances * <p>Each {@code JsonReader} may be used to read a single JSON stream. Instances
* of this class are not thread safe. * of this class are not thread safe.
*/ */
public final class JsonReader implements Closeable { public class JsonReader implements Closeable {
private static final long MIN_INCOMPLETE_INTEGER = Long.MIN_VALUE / 10; private static final long MIN_INCOMPLETE_INTEGER = Long.MIN_VALUE / 10;
private static final ByteString SINGLE_QUOTE_OR_SLASH = ByteString.encodeUtf8("'\\"); private static final ByteString SINGLE_QUOTE_OR_SLASH = ByteString.encodeUtf8("'\\");
@@ -254,10 +254,7 @@ public final class JsonReader implements Closeable {
private String[] pathNames = new String[32]; private String[] pathNames = new String[32];
private int[] pathIndices = new int[32]; private int[] pathIndices = new int[32];
/** private JsonReader(BufferedSource source) {
* Creates a new instance that reads a JSON-encoded stream from {@code source}.
*/
public JsonReader(BufferedSource source) {
if (source == null) { if (source == null) {
throw new NullPointerException("source == null"); throw new NullPointerException("source == null");
} }
@@ -265,6 +262,13 @@ public final class JsonReader implements Closeable {
this.buffer = source.buffer(); this.buffer = source.buffer();
} }
/**
* Returns a new instance that reads a JSON-encoded stream from {@code source}.
*/
public static JsonReader of(BufferedSource source) {
return new JsonReader(source);
}
/** /**
* Configure this parser to be liberal in what it accepts. By default * Configure this parser to be liberal in what it accepts. By default
* this parser is strict and only accepts JSON as specified by <a * this parser is strict and only accepts JSON as specified by <a

View File

@@ -76,7 +76,7 @@ import static com.squareup.moshi.JsonScope.NONEMPTY_OBJECT;
* ]}</pre> * ]}</pre>
* This code encodes the above structure: <pre> {@code * This code encodes the above structure: <pre> {@code
* public void writeJsonStream(BufferedSink sink, List<Message> messages) throws IOException { * public void writeJsonStream(BufferedSink sink, List<Message> messages) throws IOException {
* JsonWriter writer = new JsonWriter(sink); * JsonWriter writer = JsonWriter.of(sink);
* writer.setIndentSpaces(4); * writer.setIndentSpaces(4);
* writeMessagesArray(writer, messages); * writeMessagesArray(writer, messages);
* writer.close(); * writer.close();
@@ -124,7 +124,7 @@ import static com.squareup.moshi.JsonScope.NONEMPTY_OBJECT;
* Instances of this class are not thread safe. Calls that would result in a * Instances of this class are not thread safe. Calls that would result in a
* malformed JSON string will fail with an {@link IllegalStateException}. * malformed JSON string will fail with an {@link IllegalStateException}.
*/ */
public final class JsonWriter implements Closeable, Flushable { public class JsonWriter implements Closeable, Flushable {
/* /*
* From RFC 4627, "All Unicode characters may be placed within the * From RFC 4627, "All Unicode characters may be placed within the
@@ -182,16 +182,20 @@ public final class JsonWriter implements Closeable, Flushable {
private boolean promoteNameToValue; private boolean promoteNameToValue;
/** private JsonWriter(BufferedSink sink) {
* Creates a new instance that writes a JSON-encoded stream to {@code sink}.
*/
public JsonWriter(BufferedSink sink) {
if (sink == null) { if (sink == null) {
throw new NullPointerException("sink == null"); throw new NullPointerException("sink == null");
} }
this.sink = sink; this.sink = sink;
} }
/**
* Returns a new instance that writes a JSON-encoded stream to {@code sink}.
*/
public static JsonWriter of(BufferedSink sink) {
return new JsonWriter(sink);
}
/** /**
* Sets the indentation string to be repeated for each level of indentation * Sets the indentation string to be repeated for each level of indentation
* in the encoded document. If {@code indent.isEmpty()} the encoded document * in the encoded document. If {@code indent.isEmpty()} the encoded document

View File

@@ -436,7 +436,7 @@ public final class ClassJsonAdapterTest {
// Wrap in an array to avoid top-level object warnings without going completely lenient. // Wrap in an array to avoid top-level object warnings without going completely lenient.
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setSerializeNulls(true); jsonWriter.setSerializeNulls(true);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonAdapter.toJson(jsonWriter, value); jsonAdapter.toJson(jsonWriter, value);

View File

@@ -39,12 +39,12 @@ public final class JsonReaderTest {
@Test public void readingDoesNotBuffer() throws IOException { @Test public void readingDoesNotBuffer() throws IOException {
Buffer buffer = new Buffer().writeUtf8("{}{}"); Buffer buffer = new Buffer().writeUtf8("{}{}");
JsonReader reader1 = new JsonReader(buffer); JsonReader reader1 = JsonReader.of(buffer);
reader1.beginObject(); reader1.beginObject();
reader1.endObject(); reader1.endObject();
assertThat(buffer.size()).isEqualTo(2); assertThat(buffer.size()).isEqualTo(2);
JsonReader reader2 = new JsonReader(buffer); JsonReader reader2 = JsonReader.of(buffer);
reader2.beginObject(); reader2.beginObject();
reader2.endObject(); reader2.endObject();
assertThat(buffer.size()).isEqualTo(0); assertThat(buffer.size()).isEqualTo(0);
@@ -80,7 +80,7 @@ public final class JsonReaderTest {
@Test public void readObjectBuffer() throws IOException { @Test public void readObjectBuffer() throws IOException {
Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}"); Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}");
JsonReader reader = new JsonReader(buffer); JsonReader reader = JsonReader.of(buffer);
reader.beginObject(); reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a"); assertThat(reader.nextName()).isEqualTo("a");
assertThat(reader.nextString()).isEqualTo("android"); assertThat(reader.nextString()).isEqualTo("android");
@@ -92,7 +92,7 @@ public final class JsonReaderTest {
@Test public void readObjectSource() throws IOException { @Test public void readObjectSource() throws IOException {
Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}"); Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}");
JsonReader reader = new JsonReader(buffer); JsonReader reader = JsonReader.of(buffer);
reader.beginObject(); reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a"); assertThat(reader.nextName()).isEqualTo("a");
assertThat(reader.nextString()).isEqualTo("android"); assertThat(reader.nextString()).isEqualTo("android");
@@ -245,7 +245,7 @@ public final class JsonReaderTest {
@Test public void nullSource() { @Test public void nullSource() {
try { try {
new JsonReader(null); JsonReader.of(null);
fail(); fail();
} catch (NullPointerException expected) { } catch (NullPointerException expected) {
} }

View File

@@ -24,7 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public final class JsonWriterPathTest { public final class JsonWriterPathTest {
@Test public void path() throws IOException { @Test public void path() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginObject(); writer.beginObject();
assertThat(writer.getPath()).isEqualTo("$."); assertThat(writer.getPath()).isEqualTo("$.");
@@ -63,7 +63,7 @@ public final class JsonWriterPathTest {
} }
@Test public void arrayOfObjects() throws IOException { @Test public void arrayOfObjects() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
writer.beginArray(); writer.beginArray();
assertThat(writer.getPath()).isEqualTo("$[0]"); assertThat(writer.getPath()).isEqualTo("$[0]");
writer.beginObject(); writer.beginObject();
@@ -83,7 +83,7 @@ public final class JsonWriterPathTest {
} }
@Test public void arrayOfArrays() throws IOException { @Test public void arrayOfArrays() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
writer.beginArray(); writer.beginArray();
assertThat(writer.getPath()).isEqualTo("$[0]"); assertThat(writer.getPath()).isEqualTo("$[0]");
writer.beginArray(); writer.beginArray();
@@ -103,7 +103,7 @@ public final class JsonWriterPathTest {
} }
@Test public void objectPath() throws IOException { @Test public void objectPath() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginObject(); writer.beginObject();
assertThat(writer.getPath()).isEqualTo("$."); assertThat(writer.getPath()).isEqualTo("$.");
@@ -122,7 +122,7 @@ public final class JsonWriterPathTest {
} }
@Test public void nestedObjects() throws IOException { @Test public void nestedObjects() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginObject(); writer.beginObject();
assertThat(writer.getPath()).isEqualTo("$."); assertThat(writer.getPath()).isEqualTo("$.");
@@ -147,7 +147,7 @@ public final class JsonWriterPathTest {
} }
@Test public void arrayPath() throws IOException { @Test public void arrayPath() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginArray(); writer.beginArray();
assertThat(writer.getPath()).isEqualTo("$[0]"); assertThat(writer.getPath()).isEqualTo("$[0]");
@@ -168,7 +168,7 @@ public final class JsonWriterPathTest {
} }
@Test public void nestedArrays() throws IOException { @Test public void nestedArrays() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginArray(); writer.beginArray();
assertThat(writer.getPath()).isEqualTo("$[0]"); assertThat(writer.getPath()).isEqualTo("$[0]");
@@ -189,7 +189,7 @@ public final class JsonWriterPathTest {
} }
@Test public void multipleTopLevelValuesInOneDocument() throws IOException { @Test public void multipleTopLevelValuesInOneDocument() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
writer.setLenient(true); writer.setLenient(true);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
@@ -200,7 +200,7 @@ public final class JsonWriterPathTest {
} }
@Test public void skipNulls() throws IOException { @Test public void skipNulls() throws IOException {
JsonWriter writer = new JsonWriter(new Buffer()); JsonWriter writer = JsonWriter.of(new Buffer());
writer.setSerializeNulls(false); writer.setSerializeNulls(false);
assertThat(writer.getPath()).isEqualTo("$"); assertThat(writer.getPath()).isEqualTo("$");
writer.beginObject(); writer.beginObject();

View File

@@ -27,7 +27,7 @@ import static org.junit.Assert.fail;
public final class JsonWriterTest { public final class JsonWriterTest {
@Test public void nullsValuesNotSerializedByDefault() throws IOException { @Test public void nullsValuesNotSerializedByDefault() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
jsonWriter.nullValue(); jsonWriter.nullValue();
@@ -38,7 +38,7 @@ public final class JsonWriterTest {
@Test public void nullsValuesSerializedWhenConfigured() throws IOException { @Test public void nullsValuesSerializedWhenConfigured() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setSerializeNulls(true); jsonWriter.setSerializeNulls(true);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
@@ -50,7 +50,7 @@ public final class JsonWriterTest {
@Test public void wrongTopLevelType() throws IOException { @Test public void wrongTopLevelType() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
try { try {
jsonWriter.value("a"); jsonWriter.value("a");
fail(); fail();
@@ -60,7 +60,7 @@ public final class JsonWriterTest {
@Test public void twoNames() throws IOException { @Test public void twoNames() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
try { try {
@@ -72,7 +72,7 @@ public final class JsonWriterTest {
@Test public void nameWithoutValue() throws IOException { @Test public void nameWithoutValue() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
try { try {
@@ -84,7 +84,7 @@ public final class JsonWriterTest {
@Test public void valueWithoutName() throws IOException { @Test public void valueWithoutName() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
try { try {
jsonWriter.value(true); jsonWriter.value(true);
@@ -95,7 +95,7 @@ public final class JsonWriterTest {
@Test public void multipleTopLevelValues() throws IOException { @Test public void multipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray().endArray(); jsonWriter.beginArray().endArray();
try { try {
jsonWriter.beginArray(); jsonWriter.beginArray();
@@ -106,7 +106,7 @@ public final class JsonWriterTest {
@Test public void badNestingObject() throws IOException { @Test public void badNestingObject() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.beginObject(); jsonWriter.beginObject();
try { try {
@@ -118,7 +118,7 @@ public final class JsonWriterTest {
@Test public void badNestingArray() throws IOException { @Test public void badNestingArray() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.beginArray(); jsonWriter.beginArray();
try { try {
@@ -130,7 +130,7 @@ public final class JsonWriterTest {
@Test public void nullName() throws IOException { @Test public void nullName() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
try { try {
jsonWriter.name(null); jsonWriter.name(null);
@@ -141,7 +141,7 @@ public final class JsonWriterTest {
@Test public void nullStringValue() throws IOException { @Test public void nullStringValue() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setSerializeNulls(true); jsonWriter.setSerializeNulls(true);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
@@ -152,7 +152,7 @@ public final class JsonWriterTest {
@Test public void nonFiniteDoubles() throws IOException { @Test public void nonFiniteDoubles() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
try { try {
jsonWriter.value(Double.NaN); jsonWriter.value(Double.NaN);
@@ -173,7 +173,7 @@ public final class JsonWriterTest {
@Test public void nonFiniteBoxedDoubles() throws IOException { @Test public void nonFiniteBoxedDoubles() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
try { try {
jsonWriter.value(new Double(Double.NaN)); jsonWriter.value(new Double(Double.NaN));
@@ -194,7 +194,7 @@ public final class JsonWriterTest {
@Test public void doubles() throws IOException { @Test public void doubles() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value(-0.0); jsonWriter.value(-0.0);
jsonWriter.value(1.0); jsonWriter.value(1.0);
@@ -220,7 +220,7 @@ public final class JsonWriterTest {
@Test public void longs() throws IOException { @Test public void longs() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value(0); jsonWriter.value(0);
jsonWriter.value(1); jsonWriter.value(1);
@@ -238,7 +238,7 @@ public final class JsonWriterTest {
@Test public void numbers() throws IOException { @Test public void numbers() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value(new BigInteger("0")); jsonWriter.value(new BigInteger("0"));
jsonWriter.value(new BigInteger("9223372036854775808")); jsonWriter.value(new BigInteger("9223372036854775808"));
@@ -254,7 +254,7 @@ public final class JsonWriterTest {
@Test public void booleans() throws IOException { @Test public void booleans() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value(true); jsonWriter.value(true);
jsonWriter.value(false); jsonWriter.value(false);
@@ -264,7 +264,7 @@ public final class JsonWriterTest {
@Test public void nulls() throws IOException { @Test public void nulls() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.nullValue(); jsonWriter.nullValue();
jsonWriter.endArray(); jsonWriter.endArray();
@@ -273,7 +273,7 @@ public final class JsonWriterTest {
@Test public void strings() throws IOException { @Test public void strings() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value("a"); jsonWriter.value("a");
jsonWriter.value("a\""); jsonWriter.value("a\"");
@@ -316,7 +316,7 @@ public final class JsonWriterTest {
@Test public void unicodeLineBreaksEscaped() throws IOException { @Test public void unicodeLineBreaksEscaped() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.value("\u2028 \u2029"); jsonWriter.value("\u2028 \u2029");
jsonWriter.endArray(); jsonWriter.endArray();
@@ -325,7 +325,7 @@ public final class JsonWriterTest {
@Test public void emptyArray() throws IOException { @Test public void emptyArray() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.endArray(); jsonWriter.endArray();
assertThat(buffer.readUtf8()).isEqualTo("[]"); assertThat(buffer.readUtf8()).isEqualTo("[]");
@@ -333,7 +333,7 @@ public final class JsonWriterTest {
@Test public void emptyObject() throws IOException { @Test public void emptyObject() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.endObject(); jsonWriter.endObject();
assertThat(buffer.readUtf8()).isEqualTo("{}"); assertThat(buffer.readUtf8()).isEqualTo("{}");
@@ -341,7 +341,7 @@ public final class JsonWriterTest {
@Test public void objectsInArrays() throws IOException { @Test public void objectsInArrays() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginArray(); jsonWriter.beginArray();
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a").value(5); jsonWriter.name("a").value(5);
@@ -358,7 +358,7 @@ public final class JsonWriterTest {
@Test public void arraysInObjects() throws IOException { @Test public void arraysInObjects() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a"); jsonWriter.name("a");
jsonWriter.beginArray(); jsonWriter.beginArray();
@@ -377,7 +377,7 @@ public final class JsonWriterTest {
@Test public void deepNestingArrays() throws IOException { @Test public void deepNestingArrays() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
jsonWriter.beginArray(); jsonWriter.beginArray();
} }
@@ -389,7 +389,7 @@ public final class JsonWriterTest {
@Test public void deepNestingObjects() throws IOException { @Test public void deepNestingObjects() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
jsonWriter.name("a"); jsonWriter.name("a");
@@ -407,7 +407,7 @@ public final class JsonWriterTest {
@Test public void repeatedName() throws IOException { @Test public void repeatedName() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("a").value(true); jsonWriter.name("a").value(true);
jsonWriter.name("a").value(false); jsonWriter.name("a").value(false);
@@ -418,7 +418,7 @@ public final class JsonWriterTest {
@Test public void prettyPrintObject() throws IOException { @Test public void prettyPrintObject() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setSerializeNulls(true); jsonWriter.setSerializeNulls(true);
jsonWriter.setIndent(" "); jsonWriter.setIndent(" ");
@@ -456,7 +456,7 @@ public final class JsonWriterTest {
@Test public void prettyPrintArray() throws IOException { @Test public void prettyPrintArray() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setIndent(" "); jsonWriter.setIndent(" ");
jsonWriter.beginArray(); jsonWriter.beginArray();
@@ -493,7 +493,7 @@ public final class JsonWriterTest {
@Test public void lenientWriterPermitsMultipleTopLevelValues() throws IOException { @Test public void lenientWriterPermitsMultipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.setLenient(true); writer.setLenient(true);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
@@ -505,7 +505,7 @@ public final class JsonWriterTest {
@Test public void strictWriterDoesNotPermitMultipleTopLevelValues() throws IOException { @Test public void strictWriterDoesNotPermitMultipleTopLevelValues() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
try { try {
@@ -517,7 +517,7 @@ public final class JsonWriterTest {
@Test public void closedWriterThrowsOnStructure() throws IOException { @Test public void closedWriterThrowsOnStructure() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -545,7 +545,7 @@ public final class JsonWriterTest {
@Test public void closedWriterThrowsOnName() throws IOException { @Test public void closedWriterThrowsOnName() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -558,7 +558,7 @@ public final class JsonWriterTest {
@Test public void closedWriterThrowsOnValue() throws IOException { @Test public void closedWriterThrowsOnValue() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -571,7 +571,7 @@ public final class JsonWriterTest {
@Test public void closedWriterThrowsOnFlush() throws IOException { @Test public void closedWriterThrowsOnFlush() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();
@@ -584,7 +584,7 @@ public final class JsonWriterTest {
@Test public void writerCloseIsIdempotent() throws IOException { @Test public void writerCloseIsIdempotent() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.endArray(); writer.endArray();
writer.close(); writer.close();

View File

@@ -75,7 +75,7 @@ public final class MapJsonAdapterTest {
JsonAdapter<?> jsonAdapter = mapAdapter(String.class, Boolean.class); JsonAdapter<?> jsonAdapter = mapAdapter(String.class, Boolean.class);
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setLenient(true); jsonWriter.setLenient(true);
jsonAdapter.toJson(jsonWriter, null); jsonAdapter.toJson(jsonWriter, null);
assertThat(buffer.readUtf8()).isEqualTo("null"); assertThat(buffer.readUtf8()).isEqualTo("null");
@@ -129,7 +129,7 @@ public final class MapJsonAdapterTest {
private <K, V> String toJson(Type keyType, Type valueType, Map<K, V> value) throws IOException { private <K, V> String toJson(Type keyType, Type valueType, Map<K, V> value) throws IOException {
JsonAdapter<Map<K, V>> jsonAdapter = mapAdapter(keyType, valueType); JsonAdapter<Map<K, V>> jsonAdapter = mapAdapter(keyType, valueType);
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter jsonWriter = new JsonWriter(buffer); JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setSerializeNulls(true); jsonWriter.setSerializeNulls(true);
jsonAdapter.toJson(jsonWriter, value); jsonAdapter.toJson(jsonWriter, value);
return buffer.readUtf8(); return buffer.readUtf8();

View File

@@ -160,12 +160,12 @@ public final class PromoteNameToValueTest {
} }
private JsonReader newReader(String s) { private JsonReader newReader(String s) {
return new JsonReader(new Buffer().writeUtf8(s)); return JsonReader.of(new Buffer().writeUtf8(s));
} }
@Test public void writerStringValue() throws Exception { @Test public void writerStringValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
writer.value("a"); writer.value("a");
@@ -179,7 +179,7 @@ public final class PromoteNameToValueTest {
@Test public void writerIntegerValue() throws Exception { @Test public void writerIntegerValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
writer.value(5); writer.value(5);
@@ -193,7 +193,7 @@ public final class PromoteNameToValueTest {
@Test public void writerDoubleValue() throws Exception { @Test public void writerDoubleValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
writer.value(5.5d); writer.value(5.5d);
@@ -207,7 +207,7 @@ public final class PromoteNameToValueTest {
@Test public void writerBooleanValue() throws Exception { @Test public void writerBooleanValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
try { try {
@@ -226,7 +226,7 @@ public final class PromoteNameToValueTest {
@Test public void writerLongValue() throws Exception { @Test public void writerLongValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
writer.value(5L); writer.value(5L);
@@ -240,7 +240,7 @@ public final class PromoteNameToValueTest {
@Test public void writerNullValue() throws Exception { @Test public void writerNullValue() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
try { try {
@@ -260,7 +260,7 @@ public final class PromoteNameToValueTest {
@Test public void writerMultipleValueObject() throws Exception { @Test public void writerMultipleValueObject() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.name("a"); writer.name("a");
writer.value(1); writer.value(1);
@@ -276,7 +276,7 @@ public final class PromoteNameToValueTest {
@Test public void writerEmptyValueObject() throws Exception { @Test public void writerEmptyValueObject() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();
assertThat(writer.getPath()).isEqualTo("$."); assertThat(writer.getPath()).isEqualTo("$.");
@@ -287,7 +287,7 @@ public final class PromoteNameToValueTest {
@Test public void writerUnusedPromotionDoesntPersist() throws Exception { @Test public void writerUnusedPromotionDoesntPersist() throws Exception {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = new JsonWriter(buffer); JsonWriter writer = JsonWriter.of(buffer);
writer.beginArray(); writer.beginArray();
writer.beginObject(); writer.beginObject();
writer.promoteNameToValue(); writer.promoteNameToValue();

View File

@@ -20,7 +20,7 @@ import okio.Buffer;
final class TestUtil { final class TestUtil {
static JsonReader newReader(String json) { static JsonReader newReader(String json) {
Buffer buffer = new Buffer().writeUtf8(json); Buffer buffer = new Buffer().writeUtf8(json);
return new JsonReader(buffer); return JsonReader.of(buffer);
} }
private TestUtil() { private TestUtil() {