diff --git a/moshi/src/main/java/com/squareup/moshi/JsonReader.java b/moshi/src/main/java/com/squareup/moshi/JsonReader.java index 76e0531..bcf1194 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonReader.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonReader.java @@ -27,6 +27,8 @@ import okio.Buffer; import okio.BufferedSource; import okio.ByteString; +import static java.util.Collections.unmodifiableList; + /** * Reads a JSON (RFC 7159) * encoded value as a stream of tokens. This stream includes both literal @@ -528,6 +530,11 @@ public abstract class JsonReader implements Closeable { this.doubleQuoteSuffix = doubleQuoteSuffix; } + /** Returns a copy of this {@link Options Option's} strings. */ + public List strings() { + return unmodifiableList(Arrays.asList(strings)); + } + @CheckReturnValue public static Options of(String... strings) { try { ByteString[] result = new ByteString[strings.length]; diff --git a/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java b/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java index c07a8e6..213ff5f 100644 --- a/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java +++ b/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Set; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -1157,6 +1158,20 @@ public final class JsonReaderTest { reader.endObject(); } + @Test public void optionsStrings() { + String[] options = new String[] { "a", "b", "c" }; + JsonReader.Options abc = JsonReader.Options.of("a", "b", "c"); + List strings = abc.strings(); + assertThat(options).containsExactlyElementsOf(strings); + try { + // Confirm it's unmodifiable and we can't mutate the original underlying array + strings.add("d"); + fail(); + } catch (UnsupportedOperationException expected) { + + } + } + /** Peek a value, then read it, recursively. */ private void readValue(JsonReader reader, boolean peekJsonFirst) throws IOException { JsonReader.Token token = reader.peek();