mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Merge pull request #35 from square/jw/expose-sink
Expose Sink as constructor type.
This commit is contained in:
@@ -19,6 +19,8 @@ import java.io.IOException;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Type;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
import okio.Sink;
|
||||
|
||||
/**
|
||||
* Converts Java values to JSON, and JSON values to Java.
|
||||
@@ -26,15 +28,25 @@ import okio.Buffer;
|
||||
public abstract class JsonAdapter<T> {
|
||||
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 {
|
||||
return fromJson(new JsonReader(string));
|
||||
return fromJson(new Buffer().writeUtf8(string));
|
||||
}
|
||||
|
||||
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 {
|
||||
Buffer buffer = new Buffer();
|
||||
toJson(new JsonWriter(buffer), value);
|
||||
toJson(buffer, value);
|
||||
return buffer.readUtf8();
|
||||
}
|
||||
|
||||
|
@@ -21,8 +21,6 @@ import java.io.IOException;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
import okio.ByteString;
|
||||
import okio.Okio;
|
||||
import okio.Source;
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*/
|
||||
public JsonReader(Source source) {
|
||||
public JsonReader(BufferedSource source) {
|
||||
if (source == null) {
|
||||
throw new NullPointerException("source == null");
|
||||
}
|
||||
BufferedSource bufferedSource = Okio.buffer(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.source = source;
|
||||
this.buffer = source.buffer();
|
||||
}
|
||||
|
||||
|
@@ -19,6 +19,7 @@ import java.io.Closeable;
|
||||
import java.io.Flushable;
|
||||
import java.io.IOException;
|
||||
import okio.BufferedSink;
|
||||
import okio.Okio;
|
||||
import okio.Sink;
|
||||
|
||||
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}.
|
||||
*/
|
||||
public JsonWriter(BufferedSink sink) {
|
||||
public JsonWriter(Sink sink) {
|
||||
if (sink == null) {
|
||||
throw new NullPointerException("sink == null");
|
||||
}
|
||||
this.sink = sink;
|
||||
this.sink = Okio.buffer(sink);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -24,6 +24,7 @@ import javax.crypto.KeyGenerator;
|
||||
import okio.Buffer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
import static com.squareup.moshi.Util.NO_ANNOTATIONS;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -411,7 +412,7 @@ public final class ClassJsonAdapterTest {
|
||||
JsonAdapter<T> jsonAdapter = (JsonAdapter<T>) ClassJsonAdapter.FACTORY.create(
|
||||
type, NO_ANNOTATIONS, moshi);
|
||||
// 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();
|
||||
T result = jsonAdapter.fromJson(jsonReader);
|
||||
jsonReader.endArray();
|
||||
|
@@ -18,11 +18,12 @@ package com.squareup.moshi;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class JsonReaderPathTest {
|
||||
@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());
|
||||
reader.beginObject();
|
||||
assertEquals("$.", reader.getPath());
|
||||
@@ -61,7 +62,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void arrayOfObjects() throws IOException {
|
||||
JsonReader reader = new JsonReader("[{},{},{}]");
|
||||
JsonReader reader = newReader("[{},{},{}]");
|
||||
reader.beginArray();
|
||||
assertEquals("$[0]", reader.getPath());
|
||||
reader.beginObject();
|
||||
@@ -81,7 +82,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void arrayOfArrays() throws IOException {
|
||||
JsonReader reader = new JsonReader("[[],[],[]]");
|
||||
JsonReader reader = newReader("[[],[],[]]");
|
||||
reader.beginArray();
|
||||
assertEquals("$[0]", reader.getPath());
|
||||
reader.beginArray();
|
||||
@@ -101,7 +102,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void objectPath() throws IOException {
|
||||
JsonReader reader = new JsonReader("{\"a\":1,\"b\":2}");
|
||||
JsonReader reader = newReader("{\"a\":1,\"b\":2}");
|
||||
assertEquals("$", reader.getPath());
|
||||
|
||||
reader.peek();
|
||||
@@ -141,7 +142,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void arrayPath() throws IOException {
|
||||
JsonReader reader = new JsonReader("[1,2]");
|
||||
JsonReader reader = newReader("[1,2]");
|
||||
assertEquals("$", reader.getPath());
|
||||
|
||||
reader.peek();
|
||||
@@ -171,7 +172,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void multipleTopLevelValuesInOneDocument() throws IOException {
|
||||
JsonReader reader = new JsonReader("[][]");
|
||||
JsonReader reader = newReader("[][]");
|
||||
reader.setLenient(true);
|
||||
reader.beginArray();
|
||||
reader.endArray();
|
||||
@@ -182,7 +183,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void skipArrayElements() throws IOException {
|
||||
JsonReader reader = new JsonReader("[1,2,3]");
|
||||
JsonReader reader = newReader("[1,2,3]");
|
||||
reader.beginArray();
|
||||
reader.skipValue();
|
||||
reader.skipValue();
|
||||
@@ -190,14 +191,14 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void skipObjectNames() throws IOException {
|
||||
JsonReader reader = new JsonReader("{\"a\":1}");
|
||||
JsonReader reader = newReader("{\"a\":1}");
|
||||
reader.beginObject();
|
||||
reader.skipValue();
|
||||
assertEquals("$.null", reader.getPath());
|
||||
}
|
||||
|
||||
@Test public void skipObjectValues() throws IOException {
|
||||
JsonReader reader = new JsonReader("{\"a\":1,\"b\":2}");
|
||||
JsonReader reader = newReader("{\"a\":1,\"b\":2}");
|
||||
reader.beginObject();
|
||||
reader.nextName();
|
||||
reader.skipValue();
|
||||
@@ -207,7 +208,7 @@ public final class JsonReaderPathTest {
|
||||
}
|
||||
|
||||
@Test public void skipNestedStructures() throws IOException {
|
||||
JsonReader reader = new JsonReader("[[1,2,3],4]");
|
||||
JsonReader reader = newReader("[[1,2,3],4]");
|
||||
reader.beginArray();
|
||||
reader.skipValue();
|
||||
assertEquals("$[1]", reader.getPath());
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -25,6 +25,7 @@ import okio.Buffer;
|
||||
import org.assertj.core.data.MapEntry;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
import static com.squareup.moshi.Util.NO_ANNOTATIONS;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -77,7 +78,7 @@ public final class MapJsonAdapterTest {
|
||||
jsonAdapter.toJson(jsonWriter, null);
|
||||
assertThat(buffer.readUtf8()).isEqualTo("null");
|
||||
|
||||
JsonReader jsonReader = new JsonReader("null");
|
||||
JsonReader jsonReader = newReader("null");
|
||||
jsonReader.setLenient(true);
|
||||
assertThat(jsonAdapter.fromJson(jsonReader)).isEqualTo(null);
|
||||
}
|
||||
|
@@ -29,6 +29,7 @@ import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -264,7 +265,7 @@ public final class MoshiTest {
|
||||
|
||||
// Non-lenient adapter won't allow values outside of range.
|
||||
adapter = moshi.adapter(double.class);
|
||||
JsonReader reader = new JsonReader("[1E309]");
|
||||
JsonReader reader = newReader("[1E309]");
|
||||
reader.beginArray();
|
||||
try {
|
||||
adapter.fromJson(reader);
|
||||
@@ -273,7 +274,7 @@ public final class MoshiTest {
|
||||
assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[0]");
|
||||
}
|
||||
|
||||
reader = new JsonReader("[-1E309]");
|
||||
reader = newReader("[-1E309]");
|
||||
reader.beginArray();
|
||||
try {
|
||||
adapter.fromJson(reader);
|
||||
@@ -332,7 +333,7 @@ public final class MoshiTest {
|
||||
|
||||
// Non-lenient adapter won't allow values outside of range.
|
||||
adapter = moshi.adapter(float.class);
|
||||
JsonReader reader = new JsonReader("[1E39]");
|
||||
JsonReader reader = newReader("[1E39]");
|
||||
reader.beginArray();
|
||||
try {
|
||||
adapter.fromJson(reader);
|
||||
@@ -341,7 +342,7 @@ public final class MoshiTest {
|
||||
assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[1]");
|
||||
}
|
||||
|
||||
reader = new JsonReader("[-1E39]");
|
||||
reader = newReader("[-1E39]");
|
||||
reader.beginArray();
|
||||
try {
|
||||
adapter.fromJson(reader);
|
||||
|
29
moshi/src/test/java/com/squareup/moshi/TestUtil.java
Normal file
29
moshi/src/test/java/com/squareup/moshi/TestUtil.java
Normal 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.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user