@Override public void writeToJson(@NotNull JsonWriter writer) throws IOException { checkNotNull(writer, "writer == null"); writer.name(JSON_KEY_TYPE).value(TYPE); } }
@Override public void writeObject(@NotNull String fieldName, InputFieldMarshaller marshaller) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (marshaller != null) { jsonWriter.name(fieldName).beginObject(); marshaller.marshal(this); jsonWriter.endObject(); } else { jsonWriter.name(fieldName).nullValue(); } }
@Override public void willResolve(ResponseField field, Operation.Variables variables) { try { jsonWriter.name(field.responseName()); if (field.type() == ResponseField.Type.LIST) { jsonWriter.beginArray(); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void writeToJson(@NotNull JsonWriter writer) throws IOException { checkNotNull(writer, "writer == null"); writer.name(JSON_KEY_ID).value(subscriptionId); writer.name(JSON_KEY_TYPE).value(TYPE); writer.name(JSON_KEY_PAYLOAD).beginObject(); writer.name(JSON_KEY_QUERY).value(subscription.queryDocument().replaceAll("\\n", "")); writer.name(JSON_KEY_VARIABLES).beginObject(); subscription.variables().marshaller().marshal(new InputFieldJsonWriter(writer, scalarTypeAdapters)); writer.endObject(); writer.name(JSON_KEY_OPERATION_NAME).value(subscription.name().name()); writer.endObject(); } }
@Override public void writeList(@NotNull String fieldName, ListWriter listWriter) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (listWriter != null) { jsonWriter.name(fieldName).beginArray(); listWriter.write(new JsonListItemWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endArray(); } else { jsonWriter.name(fieldName).nullValue(); } }
public String toJson(@NotNull 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); } }
static ByteString httpRequestBody(Operation operation, ScalarTypeAdapters scalarTypeAdapters, boolean writeQueryDocument) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.setSerializeNulls(true); jsonWriter.beginObject(); jsonWriter.name("operationName").value(operation.name().name()); jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.name("extensions") .beginObject() .name("persistedQuery") .beginObject() .name("version").value(1) .name("sha256Hash").value(operation.operationId()) .endObject() .endObject(); if (writeQueryDocument) { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.endObject(); jsonWriter.close(); return buffer.readByteString(); } }
@SuppressWarnings("unchecked") public static void writeToJson(Object value, JsonWriter jsonWriter) throws IOException { if (value == null) { jsonWriter.nullValue(); } else if (value instanceof Map) { jsonWriter.beginObject(); for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) { String key = entry.getKey().toString(); jsonWriter.name(key); writeToJson(entry.getValue(), jsonWriter); } jsonWriter.endObject(); } else if (value instanceof List) { jsonWriter.beginArray(); for (Object item : (List) value) { writeToJson(item, jsonWriter); } jsonWriter.endArray(); } else if (value instanceof Boolean) { jsonWriter.value((Boolean) value); } else if (value instanceof Number) { jsonWriter.value((Number) value); } else { jsonWriter.value(value.toString()); } }
public void write(@NotNull JsonWriter jsonWriter) throws IOException { checkNotNull(jsonWriter, "jsonWriter == null"); RealResponseWriter realResponseWriter = new RealResponseWriter(Operation.EMPTY_VARIABLES, scalarTypeAdapters); operationData.marshaller().marshal(realResponseWriter); jsonWriter.beginObject(); jsonWriter.name("data"); jsonWriter.beginObject(); try { realResponseWriter.resolveFields(new JsonResponseResolver(jsonWriter)); } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw e; } } jsonWriter.endObject(); jsonWriter.endObject(); }
jsonWriter.beginObject(); for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) { jsonWriter.name(fieldEntry.getKey()); writeJsonValue(fieldEntry.getValue(), jsonWriter);
@SuppressWarnings("unchecked") @Override public void writeCustom(@NotNull String fieldName, ScalarType scalarType, Object value) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (value != null) { CustomTypeAdapter customTypeAdapter = scalarTypeAdapters.adapterFor(scalarType); CustomTypeValue customTypeValue = customTypeAdapter.encode(value); if (customTypeValue instanceof CustomTypeValue.GraphQLString) { writeString(fieldName, ((CustomTypeValue.GraphQLString) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLBoolean) { writeBoolean(fieldName, ((CustomTypeValue.GraphQLBoolean) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLNumber) { writeNumber(fieldName, ((CustomTypeValue.GraphQLNumber) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLJsonString) { writeString(fieldName, ((CustomTypeValue.GraphQLJsonString) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLJson) { writeMap(fieldName, ((CustomTypeValue.GraphQLJson) customTypeValue).value); } else { throw new IllegalArgumentException("Unsupported custom value type: " + customTypeValue); } } else { jsonWriter.name(fieldName).nullValue(); } }