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

The following examples show how to use com.squareup.moshi.JsonReader#nextString() . 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: 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 2
Source File: EnumJsonAdapter.java    From moshi with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: MoshiHelper.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
public static String nextNullableString(JsonReader reader) throws IOException {
    if (reader.peek() == JsonReader.Token.NULL) {
        reader.skipValue();
        return null;
    } else {
        return reader.nextString();
    }
}
 
Example 4
Source File: CustomAdapterWithDelegate.java    From moshi with Apache License 2.0 5 votes vote down vote up
@FromJson
@Nullable
Stage fromJson(JsonReader jsonReader, JsonAdapter<Stage> delegate) throws IOException {
  String value = jsonReader.nextString();

  Stage stage;
  if (value.startsWith("in-progress")) {
    stage = Stage.IN_PROGRESS;
  } else {
    stage = delegate.fromJsonValue(value);
  }
  return stage;
}
 
Example 5
Source File: Rfc3339DateJsonAdapter.java    From moshi with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: FallbackEnumJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 4 votes vote down vote up
@Override public T fromJson(JsonReader reader) throws IOException {
  int index = reader.selectString(options);
  if (index != -1) return constants[index];
  reader.nextString();
  return fallbackConstant;
}
 
Example 7
Source File: FallbackEnum.java    From moshi with Apache License 2.0 4 votes vote down vote up
@Override public T fromJson(JsonReader reader) throws IOException {
  int index = reader.selectString(options);
  if (index != -1) return constants[index];
  reader.nextString();
  return defaultValue;
}
 
Example 8
Source File: ByteStrings.java    From moshi with Apache License 2.0 4 votes vote down vote up
@Override public ByteString fromJson(JsonReader reader) throws IOException {
  String base64 = reader.nextString();
  return ByteString.decodeBase64(base64);
}