Java Code Examples for com.squareup.moshi.JsonReader#nextNull()
The following examples show how to use
com.squareup.moshi.JsonReader#nextNull() .
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: 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 2
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 3
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 4
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 5
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 6
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())); } }