Merge pull request #35 from square/jw/expose-sink

Expose Sink as constructor type.
This commit is contained in:
Jesse Wilson
2015-04-12 15:36:32 -04:00
9 changed files with 224 additions and 185 deletions

View File

@@ -19,6 +19,8 @@ import java.io.IOException;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import okio.Buffer; import okio.Buffer;
import okio.BufferedSource;
import okio.Sink;
/** /**
* Converts Java values to JSON, and JSON values to Java. * Converts Java values to JSON, and JSON values to Java.
@@ -26,15 +28,25 @@ import okio.Buffer;
public abstract class JsonAdapter<T> { 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 {
return fromJson(new JsonReader(source));
}
public final T fromJson(String string) throws IOException { public final T fromJson(String string) throws IOException {
return fromJson(new JsonReader(string)); return fromJson(new Buffer().writeUtf8(string));
} }
public abstract void toJson(JsonWriter writer, T value) throws IOException; public abstract void toJson(JsonWriter writer, T value) throws IOException;
public final void toJson(Sink sink, T value) throws IOException {
JsonWriter writer = new JsonWriter(sink);
toJson(writer, value);
writer.flush();
}
public final String toJson(T value) throws IOException { public final String toJson(T value) throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
toJson(new JsonWriter(buffer), value); toJson(buffer, value);
return buffer.readUtf8(); return buffer.readUtf8();
} }

View File

@@ -21,8 +21,6 @@ import java.io.IOException;
import okio.Buffer; import okio.Buffer;
import okio.BufferedSource; import okio.BufferedSource;
import okio.ByteString; import okio.ByteString;
import okio.Okio;
import okio.Source;
/** /**
* Reads a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>) * Reads a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>)
@@ -264,20 +262,11 @@ public final class JsonReader implements Closeable {
/** /**
* Creates a new instance that reads a JSON-encoded stream from {@code source}. * Creates a new instance that reads a JSON-encoded stream from {@code source}.
*/ */
public JsonReader(Source source) { public JsonReader(BufferedSource source) {
if (source == null) { if (source == null) {
throw new NullPointerException("source == null"); throw new NullPointerException("source == null");
} }
BufferedSource bufferedSource = Okio.buffer(source); this.source = source;
this.source = bufferedSource;
this.buffer = bufferedSource.buffer();
}
/**
* Creates a new instance that reads a JSON-encoded value {@code s}.
*/
public JsonReader(String s) {
this.source = new Buffer().writeUtf8(s);
this.buffer = source.buffer(); this.buffer = source.buffer();
} }

View File

@@ -19,6 +19,7 @@ import java.io.Closeable;
import java.io.Flushable; import java.io.Flushable;
import java.io.IOException; import java.io.IOException;
import okio.BufferedSink; import okio.BufferedSink;
import okio.Okio;
import okio.Sink; import okio.Sink;
import static com.squareup.moshi.JsonScope.DANGLING_NAME; import static com.squareup.moshi.JsonScope.DANGLING_NAME;
@@ -180,11 +181,11 @@ public final class JsonWriter implements Closeable, Flushable {
/** /**
* Creates a new instance that writes a JSON-encoded stream to {@code sink}. * Creates a new instance that writes a JSON-encoded stream to {@code sink}.
*/ */
public JsonWriter(BufferedSink sink) { public JsonWriter(Sink sink) {
if (sink == null) { if (sink == null) {
throw new NullPointerException("sink == null"); throw new NullPointerException("sink == null");
} }
this.sink = sink; this.sink = Okio.buffer(sink);
} }
/** /**

View File

@@ -24,6 +24,7 @@ import javax.crypto.KeyGenerator;
import okio.Buffer; import okio.Buffer;
import org.junit.Test; import org.junit.Test;
import static com.squareup.moshi.TestUtil.newReader;
import static com.squareup.moshi.Util.NO_ANNOTATIONS; import static com.squareup.moshi.Util.NO_ANNOTATIONS;
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;
@@ -411,7 +412,7 @@ public final class ClassJsonAdapterTest {
JsonAdapter<T> jsonAdapter = (JsonAdapter<T>) ClassJsonAdapter.FACTORY.create( JsonAdapter<T> jsonAdapter = (JsonAdapter<T>) ClassJsonAdapter.FACTORY.create(
type, NO_ANNOTATIONS, moshi); type, NO_ANNOTATIONS, moshi);
// 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.
JsonReader jsonReader = new JsonReader("[" + json + "]"); JsonReader jsonReader = newReader("[" + json + "]");
jsonReader.beginArray(); jsonReader.beginArray();
T result = jsonAdapter.fromJson(jsonReader); T result = jsonAdapter.fromJson(jsonReader);
jsonReader.endArray(); jsonReader.endArray();

View File

@@ -18,11 +18,12 @@ package com.squareup.moshi;
import java.io.IOException; import java.io.IOException;
import org.junit.Test; import org.junit.Test;
import static com.squareup.moshi.TestUtil.newReader;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public final class JsonReaderPathTest { public final class JsonReaderPathTest {
@Test public void path() throws IOException { @Test public void path() throws IOException {
JsonReader reader = new JsonReader("{\"a\":[2,true,false,null,\"b\",{\"c\":\"d\"},[3]]}"); JsonReader reader = newReader("{\"a\":[2,true,false,null,\"b\",{\"c\":\"d\"},[3]]}");
assertEquals("$", reader.getPath()); assertEquals("$", reader.getPath());
reader.beginObject(); reader.beginObject();
assertEquals("$.", reader.getPath()); assertEquals("$.", reader.getPath());
@@ -61,7 +62,7 @@ public final class JsonReaderPathTest {
} }
@Test public void arrayOfObjects() throws IOException { @Test public void arrayOfObjects() throws IOException {
JsonReader reader = new JsonReader("[{},{},{}]"); JsonReader reader = newReader("[{},{},{}]");
reader.beginArray(); reader.beginArray();
assertEquals("$[0]", reader.getPath()); assertEquals("$[0]", reader.getPath());
reader.beginObject(); reader.beginObject();
@@ -81,7 +82,7 @@ public final class JsonReaderPathTest {
} }
@Test public void arrayOfArrays() throws IOException { @Test public void arrayOfArrays() throws IOException {
JsonReader reader = new JsonReader("[[],[],[]]"); JsonReader reader = newReader("[[],[],[]]");
reader.beginArray(); reader.beginArray();
assertEquals("$[0]", reader.getPath()); assertEquals("$[0]", reader.getPath());
reader.beginArray(); reader.beginArray();
@@ -101,7 +102,7 @@ public final class JsonReaderPathTest {
} }
@Test public void objectPath() throws IOException { @Test public void objectPath() throws IOException {
JsonReader reader = new JsonReader("{\"a\":1,\"b\":2}"); JsonReader reader = newReader("{\"a\":1,\"b\":2}");
assertEquals("$", reader.getPath()); assertEquals("$", reader.getPath());
reader.peek(); reader.peek();
@@ -141,7 +142,7 @@ public final class JsonReaderPathTest {
} }
@Test public void arrayPath() throws IOException { @Test public void arrayPath() throws IOException {
JsonReader reader = new JsonReader("[1,2]"); JsonReader reader = newReader("[1,2]");
assertEquals("$", reader.getPath()); assertEquals("$", reader.getPath());
reader.peek(); reader.peek();
@@ -171,7 +172,7 @@ public final class JsonReaderPathTest {
} }
@Test public void multipleTopLevelValuesInOneDocument() throws IOException { @Test public void multipleTopLevelValuesInOneDocument() throws IOException {
JsonReader reader = new JsonReader("[][]"); JsonReader reader = newReader("[][]");
reader.setLenient(true); reader.setLenient(true);
reader.beginArray(); reader.beginArray();
reader.endArray(); reader.endArray();
@@ -182,7 +183,7 @@ public final class JsonReaderPathTest {
} }
@Test public void skipArrayElements() throws IOException { @Test public void skipArrayElements() throws IOException {
JsonReader reader = new JsonReader("[1,2,3]"); JsonReader reader = newReader("[1,2,3]");
reader.beginArray(); reader.beginArray();
reader.skipValue(); reader.skipValue();
reader.skipValue(); reader.skipValue();
@@ -190,14 +191,14 @@ public final class JsonReaderPathTest {
} }
@Test public void skipObjectNames() throws IOException { @Test public void skipObjectNames() throws IOException {
JsonReader reader = new JsonReader("{\"a\":1}"); JsonReader reader = newReader("{\"a\":1}");
reader.beginObject(); reader.beginObject();
reader.skipValue(); reader.skipValue();
assertEquals("$.null", reader.getPath()); assertEquals("$.null", reader.getPath());
} }
@Test public void skipObjectValues() throws IOException { @Test public void skipObjectValues() throws IOException {
JsonReader reader = new JsonReader("{\"a\":1,\"b\":2}"); JsonReader reader = newReader("{\"a\":1,\"b\":2}");
reader.beginObject(); reader.beginObject();
reader.nextName(); reader.nextName();
reader.skipValue(); reader.skipValue();
@@ -207,7 +208,7 @@ public final class JsonReaderPathTest {
} }
@Test public void skipNestedStructures() throws IOException { @Test public void skipNestedStructures() throws IOException {
JsonReader reader = new JsonReader("[[1,2,3],4]"); JsonReader reader = newReader("[[1,2,3],4]");
reader.beginArray(); reader.beginArray();
reader.skipValue(); reader.skipValue();
assertEquals("$[1]", reader.getPath()); assertEquals("$[1]", reader.getPath());

File diff suppressed because it is too large Load Diff

View File

@@ -25,6 +25,7 @@ import okio.Buffer;
import org.assertj.core.data.MapEntry; import org.assertj.core.data.MapEntry;
import org.junit.Test; import org.junit.Test;
import static com.squareup.moshi.TestUtil.newReader;
import static com.squareup.moshi.Util.NO_ANNOTATIONS; import static com.squareup.moshi.Util.NO_ANNOTATIONS;
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;
@@ -77,7 +78,7 @@ public final class MapJsonAdapterTest {
jsonAdapter.toJson(jsonWriter, null); jsonAdapter.toJson(jsonWriter, null);
assertThat(buffer.readUtf8()).isEqualTo("null"); assertThat(buffer.readUtf8()).isEqualTo("null");
JsonReader jsonReader = new JsonReader("null"); JsonReader jsonReader = newReader("null");
jsonReader.setLenient(true); jsonReader.setLenient(true);
assertThat(jsonAdapter.fromJson(jsonReader)).isEqualTo(null); assertThat(jsonAdapter.fromJson(jsonReader)).isEqualTo(null);
} }

View File

@@ -29,6 +29,7 @@ import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.junit.Test; import org.junit.Test;
import static com.squareup.moshi.TestUtil.newReader;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
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;
@@ -264,7 +265,7 @@ public final class MoshiTest {
// Non-lenient adapter won't allow values outside of range. // Non-lenient adapter won't allow values outside of range.
adapter = moshi.adapter(double.class); adapter = moshi.adapter(double.class);
JsonReader reader = new JsonReader("[1E309]"); JsonReader reader = newReader("[1E309]");
reader.beginArray(); reader.beginArray();
try { try {
adapter.fromJson(reader); adapter.fromJson(reader);
@@ -273,7 +274,7 @@ public final class MoshiTest {
assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[0]"); assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[0]");
} }
reader = new JsonReader("[-1E309]"); reader = newReader("[-1E309]");
reader.beginArray(); reader.beginArray();
try { try {
adapter.fromJson(reader); adapter.fromJson(reader);
@@ -332,7 +333,7 @@ public final class MoshiTest {
// Non-lenient adapter won't allow values outside of range. // Non-lenient adapter won't allow values outside of range.
adapter = moshi.adapter(float.class); adapter = moshi.adapter(float.class);
JsonReader reader = new JsonReader("[1E39]"); JsonReader reader = newReader("[1E39]");
reader.beginArray(); reader.beginArray();
try { try {
adapter.fromJson(reader); adapter.fromJson(reader);
@@ -341,7 +342,7 @@ public final class MoshiTest {
assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[1]"); assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[1]");
} }
reader = new JsonReader("[-1E39]"); reader = newReader("[-1E39]");
reader.beginArray(); reader.beginArray();
try { try {
adapter.fromJson(reader); adapter.fromJson(reader);

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015 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 okio.Buffer;
final class TestUtil {
static JsonReader newReader(String json) {
Buffer buffer = new Buffer().writeUtf8(json);
return new JsonReader(buffer);
}
private TestUtil() {
throw new AssertionError("No instances.");
}
}