mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Track Okio snapshot API change to Options.
Also rename our own Selection class to Options since it's semantically the same thing. https://github.com/square/okio/pull/215
This commit is contained in:
@@ -552,7 +552,7 @@ final class BufferedSourceJsonReader extends JsonReader {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override int selectName(Selection selection) throws IOException {
|
@Override int selectName(Options options) throws IOException {
|
||||||
int p = peeked;
|
int p = peeked;
|
||||||
if (p == PEEKED_NONE) {
|
if (p == PEEKED_NONE) {
|
||||||
p = doPeek();
|
p = doPeek();
|
||||||
@@ -561,10 +561,10 @@ final class BufferedSourceJsonReader extends JsonReader {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = source.select(selection.doubleQuoteSuffix);
|
int result = source.select(options.doubleQuoteSuffix);
|
||||||
if (result != -1) {
|
if (result != -1) {
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathNames[stackSize - 1] = selection.strings[result];
|
pathNames[stackSize - 1] = options.strings[result];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -596,7 +596,7 @@ final class BufferedSourceJsonReader extends JsonReader {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override int selectString(Selection selection) throws IOException {
|
@Override int selectString(Options options) throws IOException {
|
||||||
int p = peeked;
|
int p = peeked;
|
||||||
if (p == PEEKED_NONE) {
|
if (p == PEEKED_NONE) {
|
||||||
p = doPeek();
|
p = doPeek();
|
||||||
@@ -605,7 +605,7 @@ final class BufferedSourceJsonReader extends JsonReader {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = source.select(selection.doubleQuoteSuffix);
|
int result = source.select(options.doubleQuoteSuffix);
|
||||||
if (result != -1) {
|
if (result != -1) {
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
|
@@ -116,13 +116,13 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
|||||||
private final ClassFactory<T> classFactory;
|
private final ClassFactory<T> classFactory;
|
||||||
private final Map<String, FieldBinding<?>> fieldsMap;
|
private final Map<String, FieldBinding<?>> fieldsMap;
|
||||||
private final FieldBinding<?>[] fieldsArray;
|
private final FieldBinding<?>[] fieldsArray;
|
||||||
private final JsonReader.Selection selection;
|
private final JsonReader.Options options;
|
||||||
|
|
||||||
ClassJsonAdapter(ClassFactory<T> classFactory, Map<String, FieldBinding<?>> fieldsMap) {
|
ClassJsonAdapter(ClassFactory<T> classFactory, Map<String, FieldBinding<?>> fieldsMap) {
|
||||||
this.classFactory = classFactory;
|
this.classFactory = classFactory;
|
||||||
this.fieldsMap = new LinkedHashMap<>(fieldsMap);
|
this.fieldsMap = new LinkedHashMap<>(fieldsMap);
|
||||||
this.fieldsArray = fieldsMap.values().toArray(new FieldBinding[fieldsMap.size()]);
|
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()]));
|
fieldsMap.keySet().toArray(new String[fieldsMap.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
|||||||
try {
|
try {
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
int index = reader.selectName(selection);
|
int index = reader.selectName(options);
|
||||||
FieldBinding<?> fieldBinding;
|
FieldBinding<?> fieldBinding;
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
fieldBinding = fieldsArray[index];
|
fieldBinding = fieldsArray[index];
|
||||||
|
@@ -17,8 +17,6 @@ package com.squareup.moshi;
|
|||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import okio.Buffer;
|
import okio.Buffer;
|
||||||
import okio.BufferedSource;
|
import okio.BufferedSource;
|
||||||
import okio.ByteString;
|
import okio.ByteString;
|
||||||
@@ -277,10 +275,10 @@ public abstract class JsonReader implements Closeable {
|
|||||||
public abstract String nextName() throws IOException;
|
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.
|
* 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
|
* 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;
|
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.
|
* 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.
|
* 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
|
* 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.
|
* 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 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.strings = strings;
|
||||||
this.doubleQuoteSuffix = doubleQuoteSuffix;
|
this.doubleQuoteSuffix = doubleQuoteSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Selection of(String... strings) {
|
public static Options of(String... strings) {
|
||||||
try {
|
try {
|
||||||
ByteString[] result = new ByteString[strings.length];
|
ByteString[] result = new ByteString[strings.length];
|
||||||
Buffer buffer = new Buffer();
|
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).
|
buffer.readByte(); // Skip the leading double quote (but leave the trailing one).
|
||||||
result[i] = buffer.readByteString();
|
result[i] = buffer.readByteString();
|
||||||
}
|
}
|
||||||
return new Selection(strings.clone(), Arrays.asList(result));
|
return new Options(strings.clone(), okio.Options.of(result));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
|
@@ -217,7 +217,7 @@ final class StandardJsonAdapters {
|
|||||||
private final Map<String, T> nameConstantMap;
|
private final Map<String, T> nameConstantMap;
|
||||||
private final String[] nameStrings;
|
private final String[] nameStrings;
|
||||||
private final T[] constants;
|
private final T[] constants;
|
||||||
private final JsonReader.Selection selection;
|
private final JsonReader.Options options;
|
||||||
|
|
||||||
public EnumJsonAdapter(Class<T> enumType) {
|
public EnumJsonAdapter(Class<T> enumType) {
|
||||||
this.enumType = enumType;
|
this.enumType = enumType;
|
||||||
@@ -232,14 +232,14 @@ final class StandardJsonAdapters {
|
|||||||
nameConstantMap.put(name, constant);
|
nameConstantMap.put(name, constant);
|
||||||
nameStrings[i] = name;
|
nameStrings[i] = name;
|
||||||
}
|
}
|
||||||
selection = JsonReader.Selection.of(nameStrings);
|
options = JsonReader.Options.of(nameStrings);
|
||||||
} catch (NoSuchFieldException e) {
|
} catch (NoSuchFieldException e) {
|
||||||
throw new AssertionError("Missing field in " + enumType.getName(), e);
|
throw new AssertionError("Missing field in " + enumType.getName(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public T fromJson(JsonReader reader) throws IOException {
|
@Override public T fromJson(JsonReader reader) throws IOException {
|
||||||
int index = reader.selectString(selection);
|
int index = reader.selectString(options);
|
||||||
if (index != -1) return constants[index];
|
if (index != -1) return constants[index];
|
||||||
|
|
||||||
String name = reader.nextString();
|
String name = reader.nextString();
|
||||||
|
@@ -1772,7 +1772,7 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test public void selectName() throws IOException {
|
@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}");
|
JsonReader reader = newReader("{\"a\": 5, \"b\": 5, \"c\": 5, \"d\": 5}");
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
@@ -1807,7 +1807,7 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test public void selectString() throws IOException {
|
@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\"]");
|
JsonReader reader = newReader("[\"a\", \"b\", \"c\", \"d\"]");
|
||||||
reader.beginArray();
|
reader.beginArray();
|
||||||
@@ -1835,7 +1835,7 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
|
|
||||||
/** Select doesn't match unquoted strings. */
|
/** Select doesn't match unquoted strings. */
|
||||||
@Test public void selectStringUnquoted() throws IOException {
|
@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]");
|
JsonReader reader = newReader("[a]");
|
||||||
reader.setLenient(true);
|
reader.setLenient(true);
|
||||||
@@ -1847,7 +1847,7 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
|
|
||||||
/** Select doesn't match single quoted strings. */
|
/** Select doesn't match single quoted strings. */
|
||||||
@Test public void selectStringSingleQuoted() throws IOException {
|
@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']");
|
JsonReader reader = newReader("['a']");
|
||||||
reader.setLenient(true);
|
reader.setLenient(true);
|
||||||
@@ -1859,7 +1859,7 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
|
|
||||||
/** Select doesn't match unnecessarily-escaped strings. */
|
/** Select doesn't match unnecessarily-escaped strings. */
|
||||||
@Test public void selectUnnecessaryEscaping() throws IOException {
|
@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\"]");
|
JsonReader reader = newReader("[\"\\u0061\"]");
|
||||||
reader.beginArray();
|
reader.beginArray();
|
||||||
@@ -1870,17 +1870,17 @@ public final class BufferedSourceJsonReaderTest {
|
|||||||
|
|
||||||
/** Select does match necessarily escaping. The decoded value is used in the path. */
|
/** Select does match necessarily escaping. The decoded value is used in the path. */
|
||||||
@Test public void selectNecessaryEscaping() throws IOException {
|
@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}");
|
JsonReader reader = newReader("{\"\\n\": 5,\"\\u0000\": 5, \"\\\"\": 5}");
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
assertEquals(0, reader.selectName(selection));
|
assertEquals(0, reader.selectName(options));
|
||||||
assertEquals(5, reader.nextInt());
|
assertEquals(5, reader.nextInt());
|
||||||
assertEquals("$.\n", reader.getPath());
|
assertEquals("$.\n", reader.getPath());
|
||||||
assertEquals(1, reader.selectName(selection));
|
assertEquals(1, reader.selectName(options));
|
||||||
assertEquals(5, reader.nextInt());
|
assertEquals(5, reader.nextInt());
|
||||||
assertEquals("$.\u0000", reader.getPath());
|
assertEquals("$.\u0000", reader.getPath());
|
||||||
assertEquals(2, reader.selectName(selection));
|
assertEquals(2, reader.selectName(options));
|
||||||
assertEquals(5, reader.nextInt());
|
assertEquals(5, reader.nextInt());
|
||||||
assertEquals("$.\"", reader.getPath());
|
assertEquals("$.\"", reader.getPath());
|
||||||
reader.endObject();
|
reader.endObject();
|
||||||
|
Reference in New Issue
Block a user