Java Code Examples for com.squareup.moshi.JsonWriter#endObject()

The following examples show how to use com.squareup.moshi.JsonWriter#endObject() . 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 6 votes vote down vote up
@Override
public void toJson(JsonWriter writer, HasMany<T> value) throws IOException {
    writer.beginObject();
    writer.name("data");
    if (!value.hasData) {
        writeNull(writer, true);
    } else {
        writer.beginArray();
        for (ResourceIdentifier resource : value.linkedResources) {
            resourceIdentifierJsonAdapter.toJson(writer, resource);
        }
        writer.endArray();
    }
    writeNullable(writer, jsonBufferJsonAdapter, "meta", value.getMeta());
    writeNullable(writer, jsonBufferJsonAdapter, "links", value.getLinks());
    writer.endObject();
}
 
Example 2
Source File: WrappedJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 6 votes vote down vote up
/**
 * Recursively writes the respective roots forming a json object that resembles the {@code path}
 * wrapping the type of the {@code adapter}.
 */
private static <T> void toJson(JsonAdapter<T> adapter, JsonWriter writer, T value,
    String[] path, int index) throws IOException {
  if (value != null || writer.getSerializeNulls()) {
    if (index == path.length) {
      adapter.toJson(writer, value);
    } else {
      writer.beginObject();
      writer.name(path[index]);
      toJson(adapter, writer, value, path, ++index);
      writer.endObject();
    }
  } else {
    // If we don't propagate the null value the writer will throw.
    writer.nullValue();
  }
}
 
Example 3
Source File: PolymorphicJsonAdapterFactory.java    From moshi with Apache License 2.0 6 votes vote down vote up
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
  Class<?> type = value.getClass();
  int labelIndex = subtypes.indexOf(type);
  if (labelIndex == -1) {
    throw new IllegalArgumentException("Expected one of "
        + subtypes
        + " but found "
        + value
        + ", a "
        + value.getClass()
        + ". Register this subtype.");
  }
  JsonAdapter<Object> adapter = jsonAdapters.get(labelIndex);
  writer.beginObject();
  writer.name(labelKey).value(labels.get(labelIndex));
  int flattenToken = writer.beginFlatten();
  adapter.toJson(writer, value);
  writer.endFlatten(flattenToken);
  writer.endObject();
}
 
Example 4
Source File: BoundParameterQuery.java    From influxdb-java with MIT License 6 votes vote down vote up
private String createJsonObject(final Map<String, Object> parameterMap) throws IOException {
  Buffer b = new Buffer();
  JsonWriter writer = JsonWriter.of(b);
  writer.beginObject();
  for (Entry<String, Object> pair : parameterMap.entrySet()) {
    String name = pair.getKey();
    Object value = pair.getValue();
    if (value instanceof Number) {
      Number number = (Number) value;
      writer.name(name).value(number);
    } else if (value instanceof String) {
      writer.name(name).value((String) value);
    } else if (value instanceof Boolean) {
      writer.name(name).value((Boolean) value);
    } else {
      writer.name(name).value(String.valueOf(value));
    }
  }
  writer.endObject();
  return b.readString(Charset.forName("utf-8"));
}
 
Example 5
Source File: ResourceIdentifier.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@Override
public void toJson(JsonWriter writer, ResourceIdentifier value) throws IOException {
    writer.beginObject();
    writer.name("type").value(value.getType());
    writer.name("id").value(value.getId());
    writeNullable(writer, jsonBufferJsonAdapter, "meta", value.getMeta());
    writer.endObject();
}
 
Example 6
Source File: HasOne.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@Override
public void toJson(JsonWriter writer, HasOne<T> value) throws IOException {
    writer.beginObject();
    writeNullable(writer, resourceIdentifierJsonAdapter, "data", value.linkedResource, true);
    writeNullable(writer, jsonBufferJsonAdapter, "meta", value.getMeta());
    writeNullable(writer, jsonBufferJsonAdapter, "links", value.getLinks());
    writer.endObject();
}
 
Example 7
Source File: Error.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@Override
public void toJson(JsonWriter writer, Error value) throws IOException {
    writer.beginObject();
    writer.name("id").value(value.getId());
    writer.name("status").value(value.getStatus());
    writer.name("code").value(value.getCode());
    writer.name("title").value(value.getTitle());
    writer.name("detail").value(value.getDetail());
    writeNullable(writer, jsonBufferJsonAdapter, "source", value.getSource());
    writeNullable(writer, jsonBufferJsonAdapter, "meta", value.getMeta());
    writeNullable(writer, jsonBufferJsonAdapter, "links", value.getLinks());
    writer.endObject();
}
 
Example 8
Source File: ModelListAdapter.java    From android with Apache License 2.0 5 votes vote down vote up
@Override public void toJson(JsonWriter writer, List<T> value) throws IOException {
  writer.beginObject();
  writer.name(type.getSimpleName().toLowerCase());
  writer.beginArray();
  JsonAdapter<T> adapter = moshi.adapter(type);
  for (T n : value) {
    adapter.toJson(writer, n);
  }
  writer.endArray();
  writer.endObject();
}
 
Example 9
Source File: MoshiHelper.java    From moshi-jsonapi with MIT License 4 votes vote down vote up
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 10
Source File: MoshiConverterFactoryTest.java    From jus with Apache License 2.0 4 votes vote down vote up
@ToJson
public void write(JsonWriter jsonWriter, AnInterface anInterface) throws IOException {
    jsonWriter.beginObject();
    jsonWriter.name("name").value(anInterface.getName());
    jsonWriter.endObject();
}