Search in sources :

Example 1 with JsonWriter

use of com.apollographql.apollo.internal.json.JsonWriter in project apollo-android by apollographql.

the class IntegrationTest method operationJsonWriter.

@Test
public void operationJsonWriter() throws Exception {
    String json = Utils.readFileToString(getClass(), "/OperationJsonWriter.json");
    AllPlanetsQuery query = new AllPlanetsQuery();
    Response<AllPlanetsQuery.Data> response = new OperationResponseParser<>(query, query.responseFieldMapper(), new ScalarTypeAdapters(Collections.EMPTY_MAP)).parse(new Buffer().writeUtf8(json));
    Buffer buffer = new Buffer();
    OperationJsonWriter writer = new OperationJsonWriter(response.data(), new ScalarTypeAdapters(Collections.EMPTY_MAP));
    JsonWriter jsonWriter = JsonWriter.of(buffer);
    jsonWriter.setIndent("  ");
    writer.write(jsonWriter);
    assertThat(buffer.readUtf8()).isEqualTo(json);
}
Also used : Buffer(okio.Buffer) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) Test(org.junit.Test)

Example 2 with JsonWriter

use of com.apollographql.apollo.internal.json.JsonWriter in project apollo-android by apollographql.

the class RecordFieldJsonAdapter method toJson.

public String toJson(@Nonnull Map<String, Object> fields) {
    checkNotNull(fields, "fields == null");
    Buffer buffer = new Buffer();
    JsonWriter jsonWriter = JsonWriter.of(buffer);
    jsonWriter.setSerializeNulls(true);
    try {
        jsonWriter.beginObject();
        for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) {
            String key = fieldEntry.getKey();
            Object value = fieldEntry.getValue();
            jsonWriter.name(key);
            writeJsonValue(value, jsonWriter);
        }
        jsonWriter.endObject();
        jsonWriter.close();
        return buffer.readUtf8();
    } catch (IOException e) {
        // should never happen as we are working with mem buffer
        throw new RuntimeException(e);
    }
}
Also used : Buffer(okio.Buffer) IOException(java.io.IOException) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) Map(java.util.Map)

Example 3 with JsonWriter

use of com.apollographql.apollo.internal.json.JsonWriter in project apollo-android by apollographql.

the class OperationClientMessage method toJsonString.

public String toJsonString() {
    try {
        Buffer buffer = new Buffer();
        JsonWriter writer = JsonWriter.of(buffer);
        writer.beginObject();
        writeToJson(writer);
        writer.endObject();
        writer.close();
        return buffer.readUtf8();
    } catch (IOException e) {
        throw new RuntimeException("Failed to serialize to json", e);
    }
}
Also used : Buffer(okio.Buffer) IOException(java.io.IOException) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) InputFieldJsonWriter(com.apollographql.apollo.internal.json.InputFieldJsonWriter)

Example 4 with JsonWriter

use of com.apollographql.apollo.internal.json.JsonWriter in project apollo-android by apollographql.

the class ApolloServerInterceptor method httpRequestBody.

private RequestBody httpRequestBody(Operation operation) throws IOException {
    Buffer buffer = new Buffer();
    JsonWriter jsonWriter = JsonWriter.of(buffer);
    jsonWriter.setSerializeNulls(true);
    jsonWriter.beginObject();
    if (sendOperationIdentifiers) {
        jsonWriter.name("id").value(operation.operationId());
    } else {
        jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", ""));
    }
    jsonWriter.name("variables").beginObject();
    operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters));
    jsonWriter.endObject();
    jsonWriter.endObject();
    jsonWriter.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
Also used : Buffer(okio.Buffer) InputFieldJsonWriter(com.apollographql.apollo.internal.json.InputFieldJsonWriter) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) InputFieldJsonWriter(com.apollographql.apollo.internal.json.InputFieldJsonWriter)

Aggregations

JsonWriter (com.apollographql.apollo.internal.json.JsonWriter)4 Buffer (okio.Buffer)4 InputFieldJsonWriter (com.apollographql.apollo.internal.json.InputFieldJsonWriter)2 IOException (java.io.IOException)2 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)1 OperationJsonWriter (com.apollographql.apollo.response.OperationJsonWriter)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 Map (java.util.Map)1 Test (org.junit.Test)1