Merge pull request #156 from square/jwilson_0423_track_options_api

Track Okio snapshot API change to Options.
This commit is contained in:
Jake Wharton
2016-04-23 23:34:41 -04:00
5 changed files with 29 additions and 31 deletions

View File

@@ -552,7 +552,7 @@ final class BufferedSourceJsonReader extends JsonReader {
return result;
}
@Override int selectName(Selection selection) throws IOException {
@Override int selectName(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
@@ -561,10 +561,10 @@ final class BufferedSourceJsonReader extends JsonReader {
return -1;
}
int result = source.select(selection.doubleQuoteSuffix);
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathNames[stackSize - 1] = selection.strings[result];
pathNames[stackSize - 1] = options.strings[result];
}
return result;
}
@@ -596,7 +596,7 @@ final class BufferedSourceJsonReader extends JsonReader {
return result;
}
@Override int selectString(Selection selection) throws IOException {
@Override int selectString(Options options) throws IOException {
int p = peeked;
if (p == PEEKED_NONE) {
p = doPeek();
@@ -605,7 +605,7 @@ final class BufferedSourceJsonReader extends JsonReader {
return -1;
}
int result = source.select(selection.doubleQuoteSuffix);
int result = source.select(options.doubleQuoteSuffix);
if (result != -1) {
peeked = PEEKED_NONE;
pathIndices[stackSize - 1]++;

View File

@@ -116,13 +116,13 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
private final ClassFactory<T> classFactory;
private final Map<String, FieldBinding<?>> fieldsMap;
private final FieldBinding<?>[] fieldsArray;
private final JsonReader.Selection selection;
private final JsonReader.Options options;
ClassJsonAdapter(ClassFactory<T> classFactory, Map<String, FieldBinding<?>> fieldsMap) {
this.classFactory = classFactory;
this.fieldsMap = new LinkedHashMap<>(fieldsMap);
this.fieldsArray = fieldsMap.values().toArray(new FieldBinding[fieldsMap.size()]);
this.selection = JsonReader.Selection.of(
this.options = JsonReader.Options.of(
fieldsMap.keySet().toArray(new String[fieldsMap.size()]));
}
@@ -144,7 +144,7 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
try {
reader.beginObject();
while (reader.hasNext()) {
int index = reader.selectName(selection);
int index = reader.selectName(options);
FieldBinding<?> fieldBinding;
if (index != -1) {
fieldBinding = fieldsArray[index];

View File

@@ -17,8 +17,6 @@ package com.squareup.moshi;
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
@@ -277,10 +275,10 @@ public abstract class JsonReader implements Closeable {
public abstract String nextName() throws IOException;
/**
* If the next token is a {@linkplain Token#NAME property name} that's in {@code selection}, this
* If the next token is a {@linkplain Token#NAME property name} that's in {@code options}, this
* consumes it and returns its index. Otherwise this returns -1 and no name is consumed.
*/
abstract int selectName(Selection selection) throws IOException;
abstract int selectName(Options options) throws IOException;
/**
* Returns the {@linkplain Token#STRING string} value of the next token, consuming it. If the next
@@ -291,10 +289,10 @@ public abstract class JsonReader implements Closeable {
public abstract String nextString() throws IOException;
/**
* If the next token is a {@linkplain Token#STRING string} that's in {@code selection}, this
* If the next token is a {@linkplain Token#STRING string} that's in {@code options}, this
* consumes it and returns its index. Otherwise this returns -1 and no string is consumed.
*/
abstract int selectString(Selection selection) throws IOException;
abstract int selectString(Options options) throws IOException;
/**
* Returns the {@linkplain Token#BOOLEAN boolean} value of the next token, consuming it.
@@ -371,16 +369,16 @@ public abstract class JsonReader implements Closeable {
* unquoted or uses single quotes in the source JSON, it will not be selected. Client code that
* uses this class should fall back to another mechanism to accommodate this possibility.
*/
static final class Selection {
static final class Options {
final String[] strings;
final List<ByteString> doubleQuoteSuffix;
final okio.Options doubleQuoteSuffix;
public Selection(String[] strings, List<ByteString> doubleQuoteSuffix) {
private Options(String[] strings, okio.Options doubleQuoteSuffix) {
this.strings = strings;
this.doubleQuoteSuffix = doubleQuoteSuffix;
}
public static Selection of(String... strings) {
public static Options of(String... strings) {
try {
ByteString[] result = new ByteString[strings.length];
Buffer buffer = new Buffer();
@@ -389,7 +387,7 @@ public abstract class JsonReader implements Closeable {
buffer.readByte(); // Skip the leading double quote (but leave the trailing one).
result[i] = buffer.readByteString();
}
return new Selection(strings.clone(), Arrays.asList(result));
return new Options(strings.clone(), okio.Options.of(result));
} catch (IOException e) {
throw new AssertionError(e);
}

View File

@@ -217,7 +217,7 @@ final class StandardJsonAdapters {
private final Map<String, T> nameConstantMap;
private final String[] nameStrings;
private final T[] constants;
private final JsonReader.Selection selection;
private final JsonReader.Options options;
public EnumJsonAdapter(Class<T> enumType) {
this.enumType = enumType;
@@ -232,14 +232,14 @@ final class StandardJsonAdapters {
nameConstantMap.put(name, constant);
nameStrings[i] = name;
}
selection = JsonReader.Selection.of(nameStrings);
options = JsonReader.Options.of(nameStrings);
} catch (NoSuchFieldException e) {
throw new AssertionError("Missing field in " + enumType.getName(), e);
}
}
@Override public T fromJson(JsonReader reader) throws IOException {
int index = reader.selectString(selection);
int index = reader.selectString(options);
if (index != -1) return constants[index];
String name = reader.nextString();

View File

@@ -1772,7 +1772,7 @@ public final class BufferedSourceJsonReaderTest {
}
@Test public void selectName() throws IOException {
JsonReader.Selection abc = JsonReader.Selection.of("a", "b", "c");
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");
JsonReader reader = newReader("{\"a\": 5, \"b\": 5, \"c\": 5, \"d\": 5}");
reader.beginObject();
@@ -1807,7 +1807,7 @@ public final class BufferedSourceJsonReaderTest {
}
@Test public void selectString() throws IOException {
JsonReader.Selection abc = JsonReader.Selection.of("a", "b", "c");
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");
JsonReader reader = newReader("[\"a\", \"b\", \"c\", \"d\"]");
reader.beginArray();
@@ -1835,7 +1835,7 @@ public final class BufferedSourceJsonReaderTest {
/** Select doesn't match unquoted strings. */
@Test public void selectStringUnquoted() throws IOException {
JsonReader.Selection abc = JsonReader.Selection.of("a", "b", "c");
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");
JsonReader reader = newReader("[a]");
reader.setLenient(true);
@@ -1847,7 +1847,7 @@ public final class BufferedSourceJsonReaderTest {
/** Select doesn't match single quoted strings. */
@Test public void selectStringSingleQuoted() throws IOException {
JsonReader.Selection abc = JsonReader.Selection.of("a", "b", "c");
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");
JsonReader reader = newReader("['a']");
reader.setLenient(true);
@@ -1859,7 +1859,7 @@ public final class BufferedSourceJsonReaderTest {
/** Select doesn't match unnecessarily-escaped strings. */
@Test public void selectUnnecessaryEscaping() throws IOException {
JsonReader.Selection abc = JsonReader.Selection.of("a", "b", "c");
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");
JsonReader reader = newReader("[\"\\u0061\"]");
reader.beginArray();
@@ -1870,17 +1870,17 @@ public final class BufferedSourceJsonReaderTest {
/** Select does match necessarily escaping. The decoded value is used in the path. */
@Test public void selectNecessaryEscaping() throws IOException {
JsonReader.Selection selection = JsonReader.Selection.of("\n", "\u0000", "\"");
JsonReader.Options options = JsonReader.Options.of("\n", "\u0000", "\"");
JsonReader reader = newReader("{\"\\n\": 5,\"\\u0000\": 5, \"\\\"\": 5}");
reader.beginObject();
assertEquals(0, reader.selectName(selection));
assertEquals(0, reader.selectName(options));
assertEquals(5, reader.nextInt());
assertEquals("$.\n", reader.getPath());
assertEquals(1, reader.selectName(selection));
assertEquals(1, reader.selectName(options));
assertEquals(5, reader.nextInt());
assertEquals("$.\u0000", reader.getPath());
assertEquals(2, reader.selectName(selection));
assertEquals(2, reader.selectName(options));
assertEquals(5, reader.nextInt());
assertEquals("$.\"", reader.getPath());
reader.endObject();