Java Code Examples for com.squareup.moshi.JsonReader#hasNext()

The following examples show how to use com.squareup.moshi.JsonReader#hasNext() . 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 vote down vote up
@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 vote down vote up
@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: MoshiConverterFactoryTest.java    From jus with Apache License 2.0 6 votes vote down vote up
@FromJson
public AnInterface read(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();

    String name = null;
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
            case "name":
                name = jsonReader.nextString();
                break;
        }
    }

    jsonReader.endObject();
    return new AnImplementation(name);
}
 
Example 4
Source File: HasMany.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@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 5
Source File: Error.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@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 6
Source File: CustomAdapterFactory.java    From moshi with Apache License 2.0 5 votes vote down vote up
@Override public SortedSet<T> fromJson(JsonReader reader) throws IOException {
  TreeSet<T> result = new TreeSet<>();
  reader.beginArray();
  while (reader.hasNext()) {
    result.add(elementAdapter.fromJson(reader));
  }
  reader.endArray();
  return result;
}
 
Example 7
Source File: WrappedJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 4 votes vote down vote up
/**
 * 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()));
  }
}