@Override public void writeMap(Map<String, Object> value) throws IOException { writeToJson(value, jsonWriter); }
public static CustomTypeValue fromRawValue(Object value) { if (value instanceof Map || value instanceof List) { try { return new GraphQLJsonString(Utils.toJsonString(value)); } catch (Exception e) { throw new RuntimeException(e); } } else if (value instanceof Boolean) { return new GraphQLBoolean((Boolean) value); } else if (value instanceof Number) { return new GraphQLNumber((Number) value); } else { return new GraphQLString(value.toString()); } }
@Override public void writeToJson(@NotNull JsonWriter writer) throws IOException { checkNotNull(writer, "writer == null"); writer.name(JSON_KEY_TYPE).value(TYPE); if (!connectionParams.isEmpty()) { writer.name(JSON_KEY_PAYLOAD); Utils.writeToJson(connectionParams, writer); } } }
public static String toJsonString(@NotNull Object data) throws IOException { checkNotNull(data, "data == null"); Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); writeToJson(data, jsonWriter); jsonWriter.close(); return buffer.readUtf8(); }
@Override public void writeMap(@NotNull String fieldName, Map<String, Object> value) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (value == null) { jsonWriter.name(fieldName).nullValue(); } else { jsonWriter.name(fieldName); writeToJson(value, jsonWriter); } }
@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()); } }
@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); } }