Java Code Examples for com.squareup.moshi.JsonReader#skipValue()
The following examples show how to use
com.squareup.moshi.JsonReader#skipValue() .
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: ResourceIdentifier.java From moshi-jsonapi with MIT License | 6 votes |
@Override public ResourceIdentifier fromJson(JsonReader reader) throws IOException { ResourceIdentifier object = new ResourceIdentifier(); reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "id": object.setId(nextNullableString(reader)); break; case "type": object.setType(nextNullableString(reader)); break; case "meta": object.setMeta(nextNullableObject(reader, jsonBufferJsonAdapter)); break; default: reader.skipValue(); break; } } reader.endObject(); return object; }
Example 2
Source File: HasOne.java From moshi-jsonapi with MIT License | 6 votes |
@Override public HasOne<T> fromJson(JsonReader reader) throws IOException { HasOne<T> relationship = new HasOne<>(); reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "data": relationship.set(nextNullableObject(reader, resourceIdentifierJsonAdapter)); 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: DefaultOnDataMismatchAdapter.java From moshi with Apache License 2.0 | 6 votes |
@Override public T fromJson(JsonReader reader) throws IOException { // Use a peeked reader to leave the reader in a known state even if there's an exception. JsonReader peeked = reader.peekJson(); T result; try { // Attempt to decode to the target type with the peeked reader. result = delegate.fromJson(peeked); } catch (JsonDataException e) { result = defaultValue; } finally { peeked.close(); } // Skip the value back on the reader, no matter the state of the peeked reader. reader.skipValue(); return result; }
Example 4
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 5
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 6
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 7
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 8
Source File: Error.java From moshi-jsonapi with MIT License | 5 votes |
@Override public Error fromJson(JsonReader reader) throws IOException { Error err = new Error(); reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "id": err.setId(nextNullableString(reader)); break; case "status": err.setStatus(nextNullableString(reader)); break; case "code": err.setCode(nextNullableString(reader)); break; case "title": err.setTitle(nextNullableString(reader)); break; case "detail": err.setDetail(nextNullableString(reader)); break; case "source": err.setSource(nextNullableObject(reader, jsonBufferJsonAdapter)); break; case "meta": err.setMeta(nextNullableObject(reader, jsonBufferJsonAdapter)); break; case "links": err.setLinks(nextNullableObject(reader, jsonBufferJsonAdapter)); break; default: reader.skipValue(); break; } } reader.endObject(); return err; }
Example 9
Source File: TransientJsonAdapter.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Override public T fromJson(JsonReader reader) throws IOException { if (deserialize) { return delegate.fromJson(reader); } else { reader.skipValue(); return null; } }
Example 10
Source File: PolymorphicJsonAdapterFactory.java From moshi with Apache License 2.0 | 5 votes |
@Override public Object fromJson(JsonReader reader) throws IOException { JsonReader peeked = reader.peekJson(); peeked.setFailOnUnknown(false); int labelIndex; try { labelIndex = labelIndex(peeked); } finally { peeked.close(); } if (labelIndex == -1) { reader.skipValue(); return defaultValue; } return jsonAdapters.get(labelIndex).fromJson(reader); }
Example 11
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())); } }