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()); } }
final void pushScope(int newTop) { if (stackSize == scopes.length) { throw new JsonDataException("Nesting too deep at " + getPath() + ": circular reference?"); } scopes[stackSize++] = newTop; }
@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(); } }
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); } }
@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(); } }
private static void writeJsonValue(Object value, JsonWriter jsonWriter) throws IOException { if (value == null) { jsonWriter.nullValue(); } else if (value instanceof String) { jsonWriter.value((String) value); } else if (value instanceof Boolean) { jsonWriter.value((boolean) value); } else if (value instanceof Number) { jsonWriter.value((Number) value); } else if (value instanceof CacheReference) { jsonWriter.value(((CacheReference) value).serialize()); } else if (value instanceof List) { jsonWriter.beginArray(); for (Object item : (List) value) { writeJsonValue(item, jsonWriter); } jsonWriter.endArray(); } else { throw new RuntimeException("Unsupported record value type: " + value.getClass()); } } }
@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller == null) { jsonWriter.nullValue(); } else { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } }
@Before public void setUp() throws IOException { jsonBuffer = new Buffer(); jsonWriter = JsonWriter.of(jsonBuffer); jsonWriter.setSerializeNulls(true); jsonWriter.beginObject(); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(Collections.<ScalarType, CustomTypeAdapter>emptyMap())); }
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(); }
@NotNull @Override public String build(@NotNull ResponseField field, @NotNull Operation.Variables variables) { checkNotNull(field, "field == null"); checkNotNull(variables, "variables == null"); if (field.arguments().isEmpty()) { return field.fieldName(); } Object resolvedArguments = resolveArguments(field.arguments(), variables); try { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.setSerializeNulls(true); Utils.writeToJson(resolvedArguments, jsonWriter); jsonWriter.close(); return String.format("%s(%s)", field.fieldName(), buffer.readUtf8()); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void didResolveScalar(Object value) { try { if (value instanceof Number) { jsonWriter.value((Number) value); } else if (value instanceof Boolean) { jsonWriter.value((boolean) value); } else { jsonWriter.value(value.toString()); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller != null) { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } } }
@Override public void writeList(ListWriter listWriter) throws IOException { if (listWriter == null) { jsonWriter.nullValue(); } else { jsonWriter.beginArray(); listWriter.write(new JsonListItemWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endArray(); } } }