Java Code Examples for com.squareup.moshi.JsonReader#peek()
The following examples show how to use
com.squareup.moshi.JsonReader#peek() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumJsonAdapter.java From moshi with Apache License 2.0 | 6 votes |
@Override public @Nullable T fromJson(JsonReader reader) throws IOException { int index = reader.selectString(options); if (index != -1) return constants[index]; String path = reader.getPath(); if (!useFallbackValue) { String name = reader.nextString(); throw new JsonDataException("Expected one of " + Arrays.asList(nameStrings) + " but was " + name + " at path " + path); } if (reader.peek() != JsonReader.Token.STRING) { throw new JsonDataException( "Expected a string but was " + reader.peek() + " at path " + path); } reader.skipValue(); return fallbackValue; }
Example 2
Source File: HasMany.java From moshi-jsonapi with MIT License | 5 votes |
@Override public HasMany<T> fromJson(JsonReader reader) throws IOException { HasMany<T> relationship = new HasMany<>(); reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "data": if (reader.peek() == JsonReader.Token.NULL) { relationship.hasData = false; reader.nextNull(); } else { reader.beginArray(); while (reader.hasNext()) { relationship.add(resourceIdentifierJsonAdapter.fromJson(reader)); } reader.endArray(); } break; case "meta": relationship.setMeta(nextNullableObject(reader, jsonBufferJsonAdapter)); break; case "links": relationship.setLinks(nextNullableObject(reader, jsonBufferJsonAdapter)); break; default: reader.skipValue(); break; } } reader.endObject(); return relationship; }
Example 3
Source File: MoshiHelper.java From moshi-jsonapi with MIT License | 5 votes |
public static String nextNullableString(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { reader.skipValue(); return null; } else { return reader.nextString(); } }
Example 4
Source File: MoshiHelper.java From moshi-jsonapi with MIT License | 5 votes |
public static <T> T nextNullableObject(JsonReader reader, JsonAdapter<T> adapter) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { reader.skipValue(); return null; } else { return adapter.fromJson(reader); } }
Example 5
Source File: FallbackOnNullJsonAdapter.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Override public T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { reader.nextNull(); // We need to consume the value. return fallback; } return delegate.fromJson(reader); }
Example 6
Source File: ModelListAdapter.java From android with Apache License 2.0 | 5 votes |
@Override public List<T> fromJson(JsonReader reader) throws IOException { List<T> nations = new ArrayList<>(); reader.beginObject(); reader.nextName(); reader.beginArray(); while (reader.peek() == JsonReader.Token.BEGIN_OBJECT) { nations.add(moshi.adapter(type).fromJson(reader)); } reader.endArray(); reader.endObject(); return nations; }
Example 7
Source File: MultipleFormats.java From moshi with Apache License 2.0 | 5 votes |
@FromJson Card fromJson(JsonReader reader, @CardString JsonAdapter<Card> stringAdapter, JsonAdapter<Card> defaultAdapter) throws IOException { if (reader.peek() == JsonReader.Token.STRING) { return stringAdapter.fromJson(reader); } else { return defaultAdapter.fromJson(reader); } }
Example 8
Source File: Rfc3339DateJsonAdapter.java From moshi with Apache License 2.0 | 5 votes |
@Override public synchronized Date fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { return reader.nextNull(); } String string = reader.nextString(); return Iso8601Utils.parse(string); }
Example 9
Source File: NonNullJsonAdapter.java From moshi with Apache License 2.0 | 5 votes |
@Nullable @Override public T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { throw new JsonDataException("Unexpected null at " + reader.getPath()); } else { return delegate.fromJson(reader); } }
Example 10
Source File: NullSafeJsonAdapter.java From moshi with Apache License 2.0 | 5 votes |
@Override public @Nullable T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { return reader.nextNull(); } else { return delegate.fromJson(reader); } }
Example 11
Source File: MoshiHelper.java From moshi-jsonapi with MIT License | 4 votes |
public static void dump(JsonReader reader, JsonWriter writer) throws IOException { int nested = 0; boolean nullFlag = writer.getSerializeNulls(); writer.setSerializeNulls(true); try { while (reader.peek() != JsonReader.Token.END_DOCUMENT) { switch (reader.peek()) { case BEGIN_ARRAY: nested++; reader.beginArray(); writer.beginArray(); break; case END_ARRAY: reader.endArray(); writer.endArray(); if (0 == --nested) return; break; case BEGIN_OBJECT: nested++; reader.beginObject(); writer.beginObject(); break; case END_OBJECT: reader.endObject(); writer.endObject(); if (0 == --nested) return; break; case NAME: writer.name(reader.nextName()); break; case NUMBER: Double doubleValue = reader.nextDouble(); if (Math.floor(doubleValue) == doubleValue) { writer.value(doubleValue.longValue()); } else { writer.value(doubleValue); } break; case BOOLEAN: writer.value(reader.nextBoolean()); break; case STRING: writer.value(reader.nextString()); break; case NULL: reader.nextNull(); writer.nullValue(); break; } } } finally { writer.setSerializeNulls(nullFlag); } }
Example 12
Source File: WrappedJsonAdapter.java From moshi-lazy-adapters with Apache License 2.0 | 4 votes |
/** * Recursively goes through the json and finds the given root. Returns the object wrapped by the * provided {@code path}. */ private static <T> T fromJson(JsonAdapter<T> adapter, JsonReader reader, String[] path, int index, boolean failOnNotFound) throws IOException { if (index == path.length) { //noinspection unchecked This puts full responsibility on the caller. return adapter.fromJson(reader); } else { reader.beginObject(); Exception caughtException = null; try { String root = path[index]; while (reader.hasNext()) { if (reader.nextName().equals(root)) { if (reader.peek() == JsonReader.Token.NULL) { // Consumer expects a value, not a null. if (failOnNotFound) { throw new JsonDataException(String.format( "Wrapped Json expected at path: %s. Found null at %s", Arrays.asList(path), reader.getPath() )); } return reader.nextNull(); } return fromJson(adapter, reader, path, ++index, failOnNotFound); } else { reader.skipValue(); } } } catch (Exception e) { caughtException = e; } finally { // If the try block throw an exception, rethrow it up the stack. if (caughtException instanceof IOException) { //noinspection ThrowFromFinallyBlock throw (IOException) caughtException; } else if (caughtException instanceof JsonDataException) { //noinspection ThrowFromFinallyBlock throw (JsonDataException) caughtException; } else if (caughtException != null) { //noinspection ThrowFromFinallyBlock throw new AssertionError(caughtException); } // If the json has an additional key, that was not red, we ignore it. while (reader.hasNext()) { reader.skipValue(); } // End object, so that other adapters (if any) can proceed. reader.endObject(); } throw new JsonDataException(String.format( "Wrapped Json expected at path: %s. Actual: %s", Arrays.asList(path), reader.getPath())); } }