mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #16 from square/jw/source
Enable JsonReader with Source.
This commit is contained in:
@@ -21,6 +21,7 @@ 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;
|
import okio.Source;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -263,13 +264,15 @@ public class JsonReader implements Closeable {
|
|||||||
private int[] pathIndices = new int[32];
|
private int[] pathIndices = new int[32];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance that reads a JSON-encoded stream from {@code in}.
|
* Creates a new instance that reads a JSON-encoded stream from {@code source}.
|
||||||
*/
|
*/
|
||||||
public JsonReader(Source source) {
|
public JsonReader(Source source) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
throw new NullPointerException("source == null");
|
throw new NullPointerException("source == null");
|
||||||
}
|
}
|
||||||
throw new UnsupportedOperationException("TODO");
|
BufferedSource bufferedSource = Okio.buffer(source);
|
||||||
|
this.source = bufferedSource;
|
||||||
|
this.buffer = bufferedSource.buffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -18,6 +18,8 @@ package com.squareup.moshi;
|
|||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import okio.Buffer;
|
||||||
|
import okio.ForwardingSource;
|
||||||
import okio.Source;
|
import okio.Source;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -66,6 +68,31 @@ public final class JsonReaderTest {
|
|||||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void readObjectBuffer() throws IOException {
|
||||||
|
Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}");
|
||||||
|
JsonReader reader = new JsonReader(buffer);
|
||||||
|
reader.beginObject();
|
||||||
|
assertEquals("a", reader.nextName());
|
||||||
|
assertEquals("android", reader.nextString());
|
||||||
|
assertEquals("b", reader.nextName());
|
||||||
|
assertEquals("banana", reader.nextString());
|
||||||
|
reader.endObject();
|
||||||
|
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void readObjectSource() throws IOException {
|
||||||
|
Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}");
|
||||||
|
Source source = new ForwardingSource(buffer) {}; // Mask the Buffer as an unbuffered Source.
|
||||||
|
JsonReader reader = new JsonReader(source);
|
||||||
|
reader.beginObject();
|
||||||
|
assertEquals("a", reader.nextName());
|
||||||
|
assertEquals("android", reader.nextString());
|
||||||
|
assertEquals("b", reader.nextName());
|
||||||
|
assertEquals("banana", reader.nextString());
|
||||||
|
reader.endObject();
|
||||||
|
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void readEmptyObject() throws IOException {
|
@Test public void readEmptyObject() throws IOException {
|
||||||
JsonReader reader = new JsonReader("{}");
|
JsonReader reader = new JsonReader("{}");
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
|
Reference in New Issue
Block a user