From 47cd73df897eafbbd1102c83f6ec9c16b103c736 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Sun, 12 Apr 2015 00:57:14 -0400 Subject: [PATCH] Make token a nested type of the reader. This is the only class which uses these tokens and it does not deserve a top-level type. --- .../java/com/squareup/moshi/JsonAdapter.java | 2 +- .../java/com/squareup/moshi/JsonReader.java | 99 +++++++++++++++---- .../java/com/squareup/moshi/JsonToken.java | 81 --------------- .../com/squareup/moshi/JsonReaderTest.java | 90 ++++++++--------- 4 files changed, 128 insertions(+), 144 deletions(-) delete mode 100644 moshi/src/main/java/com/squareup/moshi/JsonToken.java diff --git a/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java index 3bb89aa..488c183 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java @@ -46,7 +46,7 @@ public abstract class JsonAdapter { final JsonAdapter delegate = this; return new JsonAdapter() { @Override public T fromJson(JsonReader reader) throws IOException { - if (reader.peek() == JsonToken.NULL) { + if (reader.peek() == JsonReader.Token.NULL) { return reader.nextNull(); } else { return delegate.fromJson(reader); diff --git a/moshi/src/main/java/com/squareup/moshi/JsonReader.java b/moshi/src/main/java/com/squareup/moshi/JsonReader.java index 949a5e6..894951c 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonReader.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonReader.java @@ -411,7 +411,7 @@ public class JsonReader implements Closeable { /** * Returns the type of the next token without consuming it. */ - public JsonToken peek() throws IOException { + public Token peek() throws IOException { int p = peeked; if (p == PEEKED_NONE) { p = doPeek(); @@ -419,32 +419,32 @@ public class JsonReader implements Closeable { switch (p) { case PEEKED_BEGIN_OBJECT: - return JsonToken.BEGIN_OBJECT; + return Token.BEGIN_OBJECT; case PEEKED_END_OBJECT: - return JsonToken.END_OBJECT; + return Token.END_OBJECT; case PEEKED_BEGIN_ARRAY: - return JsonToken.BEGIN_ARRAY; + return Token.BEGIN_ARRAY; case PEEKED_END_ARRAY: - return JsonToken.END_ARRAY; + return Token.END_ARRAY; case PEEKED_SINGLE_QUOTED_NAME: case PEEKED_DOUBLE_QUOTED_NAME: case PEEKED_UNQUOTED_NAME: - return JsonToken.NAME; + return Token.NAME; case PEEKED_TRUE: case PEEKED_FALSE: - return JsonToken.BOOLEAN; + return Token.BOOLEAN; case PEEKED_NULL: - return JsonToken.NULL; + return Token.NULL; case PEEKED_SINGLE_QUOTED: case PEEKED_DOUBLE_QUOTED: case PEEKED_UNQUOTED: case PEEKED_BUFFERED: - return JsonToken.STRING; + return Token.STRING; case PEEKED_LONG: case PEEKED_NUMBER: - return JsonToken.NUMBER; + return Token.NUMBER; case PEEKED_EOF: - return JsonToken.END_DOCUMENT; + return Token.END_DOCUMENT; default: throw new AssertionError(); } @@ -755,7 +755,7 @@ public class JsonReader implements Closeable { } /** - * Returns the next token, a {@link JsonToken#NAME property name}, and + * Returns the next token, a {@link Token#NAME property name}, and * consumes it. * * @throws java.io.IOException if the next token in the stream is not a property @@ -783,7 +783,7 @@ public class JsonReader implements Closeable { } /** - * Returns the {@link JsonToken#STRING string} value of the next token, + * Returns the {@link Token#STRING string} value of the next token, * consuming it. If the next token is a number, this method will return its * string form. * @@ -819,7 +819,7 @@ public class JsonReader implements Closeable { } /** - * Returns the {@link JsonToken#BOOLEAN boolean} value of the next token, + * Returns the {@link Token#BOOLEAN boolean} value of the next token, * consuming it. * * @throws IllegalStateException if the next token is not a boolean or if @@ -866,7 +866,7 @@ public class JsonReader implements Closeable { } /** - * Returns the {@link JsonToken#NUMBER double} value of the next token, + * Returns the {@link Token#NUMBER double} value of the next token, * consuming it. If the next token is a string, this method will attempt to * parse it as a double using {@link Double#parseDouble(String)}. * @@ -912,7 +912,7 @@ public class JsonReader implements Closeable { } /** - * Returns the {@link JsonToken#NUMBER long} value of the next token, + * Returns the {@link Token#NUMBER long} value of the next token, * consuming it. If the next token is a string, this method will attempt to * parse it as a long. If the next token's numeric value cannot be exactly * represented by a Java {@code long}, this method throws. @@ -1029,7 +1029,7 @@ public class JsonReader implements Closeable { } /** - * Returns the {@link JsonToken#NUMBER int} value of the next token, + * Returns the {@link Token#NUMBER int} value of the next token, * consuming it. If the next token is a string, this method will attempt to * parse it as an int. If the next token's numeric value cannot be exactly * represented by a Java {@code int}, this method throws. @@ -1375,4 +1375,69 @@ public class JsonReader implements Closeable { private IOException syntaxError(String message) throws IOException { throw new IOException(message + " at path " + getPath()); } + + /** + * A structure, name, or value type in a JSON-encoded string. + */ + public enum Token { + + /** + * The opening of a JSON array. Written using {@link JsonWriter#beginArray} + * and read using {@link JsonReader#beginArray}. + */ + BEGIN_ARRAY, + + /** + * The closing of a JSON array. Written using {@link JsonWriter#endArray} + * and read using {@link JsonReader#endArray}. + */ + END_ARRAY, + + /** + * The opening of a JSON object. Written using {@link JsonWriter#beginObject} + * and read using {@link JsonReader#beginObject}. + */ + BEGIN_OBJECT, + + /** + * The closing of a JSON object. Written using {@link JsonWriter#endObject} + * and read using {@link JsonReader#endObject}. + */ + END_OBJECT, + + /** + * A JSON property name. Within objects, tokens alternate between names and + * their values. Written using {@link JsonWriter#name} and read using {@link + * JsonReader#nextName} + */ + NAME, + + /** + * A JSON string. + */ + STRING, + + /** + * A JSON number represented in this API by a Java {@code double}, {@code + * long}, or {@code int}. + */ + NUMBER, + + /** + * A JSON {@code true} or {@code false}. + */ + BOOLEAN, + + /** + * A JSON {@code null}. + */ + NULL, + + /** + * The end of the JSON stream. This sentinel value is returned by {@link + * JsonReader#peek()} to signal that the JSON-encoded value has no more + * tokens. + */ + END_DOCUMENT + } } diff --git a/moshi/src/main/java/com/squareup/moshi/JsonToken.java b/moshi/src/main/java/com/squareup/moshi/JsonToken.java deleted file mode 100644 index b5a22f9..0000000 --- a/moshi/src/main/java/com/squareup/moshi/JsonToken.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2010 Google 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; - -/** - * A structure, name or value type in a JSON-encoded string. - */ -public enum JsonToken { - - /** - * The opening of a JSON array. Written using {@link JsonWriter#beginArray} - * and read using {@link JsonReader#beginArray}. - */ - BEGIN_ARRAY, - - /** - * The closing of a JSON array. Written using {@link JsonWriter#endArray} - * and read using {@link JsonReader#endArray}. - */ - END_ARRAY, - - /** - * The opening of a JSON object. Written using {@link JsonWriter#beginObject} - * and read using {@link JsonReader#beginObject}. - */ - BEGIN_OBJECT, - - /** - * The closing of a JSON object. Written using {@link JsonWriter#endObject} - * and read using {@link JsonReader#endObject}. - */ - END_OBJECT, - - /** - * A JSON property name. Within objects, tokens alternate between names and - * their values. Written using {@link JsonWriter#name} and read using {@link - * JsonReader#nextName} - */ - NAME, - - /** - * A JSON string. - */ - STRING, - - /** - * A JSON number represented in this API by a Java {@code double}, {@code - * long}, or {@code int}. - */ - NUMBER, - - /** - * A JSON {@code true} or {@code false}. - */ - BOOLEAN, - - /** - * A JSON {@code null}. - */ - NULL, - - /** - * The end of the JSON stream. This sentinel value is returned by {@link - * JsonReader#peek()} to signal that the JSON-encoded value has no more - * tokens. - */ - END_DOCUMENT -} diff --git a/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java b/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java index 966270d..b0c3ea1 100644 --- a/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java +++ b/moshi/src/test/java/com/squareup/moshi/JsonReaderTest.java @@ -24,15 +24,15 @@ import okio.Source; import org.junit.Ignore; import org.junit.Test; -import static com.squareup.moshi.JsonToken.BEGIN_ARRAY; -import static com.squareup.moshi.JsonToken.BEGIN_OBJECT; -import static com.squareup.moshi.JsonToken.BOOLEAN; -import static com.squareup.moshi.JsonToken.END_ARRAY; -import static com.squareup.moshi.JsonToken.END_OBJECT; -import static com.squareup.moshi.JsonToken.NAME; -import static com.squareup.moshi.JsonToken.NULL; -import static com.squareup.moshi.JsonToken.NUMBER; -import static com.squareup.moshi.JsonToken.STRING; +import static com.squareup.moshi.JsonReader.Token.BEGIN_ARRAY; +import static com.squareup.moshi.JsonReader.Token.BEGIN_OBJECT; +import static com.squareup.moshi.JsonReader.Token.BOOLEAN; +import static com.squareup.moshi.JsonReader.Token.END_ARRAY; +import static com.squareup.moshi.JsonReader.Token.END_OBJECT; +import static com.squareup.moshi.JsonReader.Token.NAME; +import static com.squareup.moshi.JsonReader.Token.NULL; +import static com.squareup.moshi.JsonReader.Token.NUMBER; +import static com.squareup.moshi.JsonReader.Token.STRING; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -46,7 +46,7 @@ public final class JsonReaderTest { assertEquals(true, reader.nextBoolean()); assertEquals(true, reader.nextBoolean()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void readEmptyArray() throws IOException { @@ -54,7 +54,7 @@ public final class JsonReaderTest { reader.beginArray(); assertFalse(reader.hasNext()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void readObject() throws IOException { @@ -65,7 +65,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); assertEquals("banana", reader.nextString()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void readObjectBuffer() throws IOException { @@ -77,7 +77,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); assertEquals("banana", reader.nextString()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void readObjectSource() throws IOException { @@ -90,7 +90,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); assertEquals("banana", reader.nextString()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void readEmptyObject() throws IOException { @@ -98,7 +98,7 @@ public final class JsonReaderTest { reader.beginObject(); assertFalse(reader.hasNext()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipArray() throws IOException { @@ -109,7 +109,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); assertEquals(123, reader.nextInt()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipArrayAfterPeek() throws Exception { @@ -121,13 +121,13 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); assertEquals(123, reader.nextInt()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipTopLevelObject() throws Exception { JsonReader reader = new JsonReader("{\"a\": [\"one\", \"two\", \"three\"], \"b\": 123}"); reader.skipValue(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipObject() throws IOException { @@ -139,7 +139,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); reader.skipValue(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipObjectAfterPeek() throws Exception { @@ -156,7 +156,7 @@ public final class JsonReaderTest { assertEquals("three", reader.nextName()); reader.skipValue(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipInteger() throws IOException { @@ -167,7 +167,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); reader.skipValue(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipDouble() throws IOException { @@ -178,7 +178,7 @@ public final class JsonReaderTest { assertEquals("b", reader.nextName()); reader.skipValue(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void helloWorld() throws IOException { @@ -195,7 +195,7 @@ public final class JsonReaderTest { assertEquals("world", reader.nextString()); reader.endArray(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void nullString() { @@ -278,7 +278,7 @@ public final class JsonReaderTest { assertEquals("\u0019", reader.nextString()); assertEquals("\u20AC", reader.nextString()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void unescapingInvalidCharacters() throws IOException { @@ -344,7 +344,7 @@ public final class JsonReaderTest { assertEquals(3.141592653589793, reader.nextDouble(), 0d); assertEquals(2.718281828459045, reader.nextDouble(), 0d); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictNonFiniteDoubles() throws IOException { @@ -433,7 +433,7 @@ public final class JsonReaderTest { } assertEquals(Long.MAX_VALUE, reader.nextLong()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test @Ignore public void numberWithOctalPrefix() throws IOException { @@ -462,7 +462,7 @@ public final class JsonReaderTest { } assertEquals("01", reader.nextString()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void booleans() throws IOException { @@ -471,7 +471,7 @@ public final class JsonReaderTest { assertEquals(true, reader.nextBoolean()); assertEquals(false, reader.nextBoolean()); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void peekingUnquotedStringsPrefixedWithBooleans() throws IOException { @@ -529,7 +529,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("[" + s + "]"); reader.setLenient(true); reader.beginArray(); - assertEquals(JsonToken.STRING, reader.peek()); + assertEquals(JsonReader.Token.STRING, reader.peek()); assertEquals(s, reader.nextString()); reader.endArray(); } @@ -663,7 +663,7 @@ public final class JsonReaderTest { reader.nextNull(); reader.nextNull(); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void missingValue() throws IOException { @@ -775,7 +775,7 @@ public final class JsonReaderTest { } catch (IllegalStateException expected) { } reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); reader.close(); } @@ -929,7 +929,7 @@ public final class JsonReaderTest { reader = new JsonReader("a//"); reader.setLenient(true); assertEquals("a", reader.nextString()); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictCommentsWithSkipValue() throws IOException { @@ -979,7 +979,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("abc"); reader.setLenient(true); assertEquals("abc", reader.nextString()); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictUnquotedNamesWithSkipValue() throws IOException { @@ -1266,7 +1266,7 @@ public final class JsonReaderTest { assertEquals(true, reader.nextBoolean()); reader.beginObject(); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictMultipleTopLevelValuesWithSkipValue() throws IOException { @@ -1293,7 +1293,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("\"a\""); reader.setLenient(true); assertEquals("a", reader.nextString()); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictTopLevelValueType() { @@ -1436,10 +1436,10 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("[0." + repeat('9', 8192) + "]"); reader.setLenient(true); reader.beginArray(); - assertEquals(JsonToken.STRING, reader.peek()); + assertEquals(JsonReader.Token.STRING, reader.peek()); assertEquals(1d, reader.nextDouble(), 0d); reader.endArray(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void veryLongUnquotedLiteral() throws IOException { @@ -1463,7 +1463,7 @@ public final class JsonReaderTest { for (int i = 0; i < 40; i++) { reader.endArray(); } - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void deeplyNestedObjects() throws IOException { @@ -1485,7 +1485,7 @@ public final class JsonReaderTest { for (int i = 0; i < 40; i++) { reader.endObject(); } - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } // http://code.google.com/p/google-gson/issues/detail?id=409 @@ -1583,7 +1583,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader(repeat('x', 8192)); reader.setLenient(true); reader.skipValue(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void skipVeryLongQuotedString() throws IOException { @@ -1597,7 +1597,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("\"" + repeat('x', 8192) + "\""); reader.setLenient(true); reader.skipValue(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void stringAsNumberWithTruncatedExponent() throws IOException { @@ -1628,11 +1628,11 @@ public final class JsonReaderTest { reader.beginObject(); assertEquals(NAME, reader.peek()); assertEquals("", reader.nextName()); - assertEquals(JsonToken.BOOLEAN, reader.peek()); + assertEquals(JsonReader.Token.BOOLEAN, reader.peek()); assertEquals(true, reader.nextBoolean()); - assertEquals(JsonToken.END_OBJECT, reader.peek()); + assertEquals(JsonReader.Token.END_OBJECT, reader.peek()); reader.endObject(); - assertEquals(JsonToken.END_DOCUMENT, reader.peek()); + assertEquals(JsonReader.Token.END_DOCUMENT, reader.peek()); } @Test public void strictExtraCommasInMaps() throws IOException { @@ -1716,7 +1716,7 @@ public final class JsonReaderTest { JsonReader reader = new JsonReader("[\"string"); reader.setLenient(true); reader.beginArray(); - assertEquals(JsonToken.STRING, reader.peek()); + assertEquals(JsonReader.Token.STRING, reader.peek()); try { reader.nextString(); fail();