/** * Throws a new IO exception with the given message and a context snippet * with this reader's content. */ final JsonEncodingException syntaxError(String message) throws JsonEncodingException { throw new JsonEncodingException(message + " at path " + getPath()); }
final void pushScope(int newTop) { if (stackSize == scopes.length) { if (stackSize == 256) { throw new JsonDataException("Nesting too deep at " + getPath()); } scopes = Arrays.copyOf(scopes, scopes.length * 2); pathNames = Arrays.copyOf(pathNames, pathNames.length * 2); pathIndices = Arrays.copyOf(pathIndices, pathIndices.length * 2); } scopes[stackSize++] = newTop; }
final JsonDataException typeMismatch(@Nullable Object value, Object expected) { if (value == null) { return new JsonDataException( "Expected " + expected + " but was null at path " + getPath()); } else { return new JsonDataException("Expected " + expected + " but was " + value + ", a " + value.getClass().getName() + ", at path " + getPath()); } }
@Override public Character fromJson(JsonReader reader) throws IOException { String value = reader.nextString(); if (value.length() > 1) { throw new JsonDataException( String.format(ERROR_FORMAT, "a char", '"' + value + '"', reader.getPath())); } return value.charAt(0); }
static int rangeCheckNextInt(JsonReader reader, String typeMessage, int min, int max) throws IOException { int value = reader.nextInt(); if (value < min || value > max) { throw new JsonDataException( String.format(ERROR_FORMAT, typeMessage, value, reader.getPath())); } return value; }
@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 {
@Override public Float fromJson(JsonReader reader) throws IOException { float value = (float) reader.nextDouble(); // Double check for infinity after float conversion; many doubles > Float.MAX if (!reader.isLenient() && Float.isInfinite(value)) { throw new JsonDataException("JSON forbids NaN and infinities: " + value + " at path " + reader.getPath()); } return value; }
@Override public T fromJson(JsonReader reader) throws IOException { int index = reader.selectString(options); if (index != -1) return constants[index]; // We can consume the string safely, we are terminating anyway. String path = reader.getPath(); String name = reader.nextString(); throw new JsonDataException("Expected one of " + Arrays.asList(nameStrings) + " but was " + name + " at path " + path); }
@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 @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 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 Map<K, V> fromJson(JsonReader reader) throws IOException { LinkedHashTreeMap<K, V> result = new LinkedHashTreeMap<>(); reader.beginObject(); while (reader.hasNext()) { reader.promoteNameToValue(); K name = keyAdapter.fromJson(reader); V value = valueAdapter.fromJson(reader); V replaced = result.put(name, value); if (replaced != null) { throw new JsonDataException("Map key '" + name + "' has multiple values at path " + reader.getPath() + ": " + replaced + " and " + value); } } reader.endObject(); return result; }
final JsonDataException typeMismatch(@Nullable Object value, Object expected) { if (value == null) { return new JsonDataException( "Expected " + expected + " but was null at path " + getPath()); } else { return new JsonDataException("Expected " + expected + " but was " + value + ", a " + value.getClass().getName() + ", at path " + getPath()); } }
final void pushScope(int newTop) { if (stackSize == scopes.length) { if (stackSize == 256) { throw new JsonDataException("Nesting too deep at " + getPath()); } scopes = Arrays.copyOf(scopes, scopes.length * 2); pathNames = Arrays.copyOf(pathNames, pathNames.length * 2); pathIndices = Arrays.copyOf(pathIndices, pathIndices.length * 2); } scopes[stackSize++] = newTop; }
@Override public Character fromJson(JsonReader reader) throws IOException { String value = reader.nextString(); if (value.length() > 1) { throw new JsonDataException( String.format(ERROR_FORMAT, "a char", '"' + value + '"', reader.getPath())); } return value.charAt(0); }
static int rangeCheckNextInt(JsonReader reader, String typeMessage, int min, int max) throws IOException { int value = reader.nextInt(); if (value < min || value > max) { throw new JsonDataException( String.format(ERROR_FORMAT, typeMessage, value, reader.getPath())); } return value; }
@Override public Float fromJson(JsonReader reader) throws IOException { float value = (float) reader.nextDouble(); // Double check for infinity after float conversion; many doubles > Float.MAX if (!reader.isLenient() && Float.isInfinite(value)) { throw new JsonDataException("JSON forbids NaN and infinities: " + value + " at path " + reader.getPath()); } return value; }
@Override public T fromJson(JsonReader reader) throws IOException { int index = reader.selectString(options); if (index != -1) return constants[index]; // We can consume the string safely, we are terminating anyway. String path = reader.getPath(); String name = reader.nextString(); throw new JsonDataException("Expected one of " + Arrays.asList(nameStrings) + " but was " + name + " at path " + path); }
@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 {