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);
}
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);
}
}
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);
}
}
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());
}
Aggregations