@Override public @Nullable T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { return reader.nextNull(); } else { return delegate.fromJson(reader); } } @Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
@Override public @Nullable 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); } } @Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
@CheckReturnValue public final @Nullable T fromJson(String string) throws IOException { JsonReader reader = JsonReader.of(new Buffer().writeUtf8(string)); T result = fromJson(reader); if (!isLenient() && reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; }
@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; }
@Override public @Nullable Object fromJson(JsonReader reader) throws IOException { if (fromAdapter == null) { return delegate.fromJson(reader); } else if (!fromAdapter.nullable && reader.peek() == JsonReader.Token.NULL) { reader.nextNull(); return null; } else { try { return fromAdapter.fromJson(moshi, reader); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) throw (IOException) cause; throw new JsonDataException(cause + " at " + reader.getPath(), cause); } } }
@Override public Object fromJson(JsonReader reader) throws IOException { switch (reader.peek()) { case BEGIN_ARRAY: return listJsonAdapter.fromJson(reader); case BEGIN_OBJECT: return mapAdapter.fromJson(reader); case STRING: return stringAdapter.fromJson(reader); case NUMBER: return doubleAdapter.fromJson(reader); case BOOLEAN: return booleanAdapter.fromJson(reader); case NULL: return reader.nextNull(); default: throw new IllegalStateException( "Expected a value but was " + reader.peek() + " at path " + reader.getPath()); } }
@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); } } }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
@Override public @Nullable M fromJson(JsonReader in) throws IOException { if (in.peek() == JsonReader.Token.NULL) { in.nextNull(); return null;
private boolean nextNull() throws IOException { if(in.peek() == JsonReader.Token.NULL) { in.nextNull(); return true; } else return false; } }
@Override public @Nullable T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { return reader.nextNull(); } else { return delegate.fromJson(reader); } } @Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
@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); }
@Override public @Nullable 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); } } @Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
@CheckReturnValue public final @Nullable T fromJson(String string) throws IOException { JsonReader reader = JsonReader.of(new Buffer().writeUtf8(string)); T result = fromJson(reader); if (!isLenient() && reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; }
@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; }
@Override public @Nullable Object fromJson(JsonReader reader) throws IOException { if (fromAdapter == null) { return delegate.fromJson(reader); } else if (!fromAdapter.nullable && reader.peek() == JsonReader.Token.NULL) { reader.nextNull(); return null; } else { try { return fromAdapter.fromJson(moshi, reader); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) throw (IOException) cause; throw new JsonDataException(cause + " at " + reader.getPath(), cause); } } }
@Override public Object fromJson(JsonReader reader) throws IOException { switch (reader.peek()) { case BEGIN_ARRAY: return listJsonAdapter.fromJson(reader); case BEGIN_OBJECT: return mapAdapter.fromJson(reader); case STRING: return stringAdapter.fromJson(reader); case NUMBER: return doubleAdapter.fromJson(reader); case BOOLEAN: return booleanAdapter.fromJson(reader); case NULL: return reader.nextNull(); default: throw new IllegalStateException( "Expected a value but was " + reader.peek() + " at path " + reader.getPath()); } }
@Override public SafeCalendar fromJson(JsonReader reader) throws IOException { SafeCalendar calendar = new SafeCalendar(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); // May contain null values, so just skip them. if (reader.peek() == Token.NULL) { reader.skipValue(); continue; } if ("year".equals(name)) { calendar.set(Calendar.YEAR, reader.nextInt()); } else if ("month".equals(name)) { calendar.set(Calendar.MONTH, reader.nextInt() - 1); } else if ("day".equals(name)) { calendar.set(Calendar.DAY_OF_MONTH, reader.nextInt()); } else { throw new IOException("birthday should contain 'year', 'month' and/or 'day', found $" + name); } } reader.endObject(); return calendar; }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }