@Before public void setUpAdapter() { customTypeAdapter = new CustomTypeAdapter<TestCustomScalar>() { @Override public TestCustomScalar decode(CustomTypeValue value) { String valueStr = value.value.toString(); return new TestCustomScalar(valueStr.substring(1, valueStr.length())); } @Override public CustomTypeValue encode(TestCustomScalar value) { return new CustomTypeValue.GraphQLString("#" + value.fieldOne); } }; recordFieldAdapter = RecordFieldJsonAdapter.create(); }
public static RecordFieldJsonAdapter create() { return new RecordFieldJsonAdapter(); }
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); } }
public Map<String, Object> from(String jsonFieldSource) throws IOException { final BufferedSource bufferSource = Okio.buffer(Okio.source(new ByteArrayInputStream(jsonFieldSource.getBytes()))); return from(bufferSource); }
Record record = recordBuilder.build(); String json = recordFieldAdapter.toJson(record.fields()); Map<String, Object> deserializedMap = recordFieldAdapter.from(json); assertThat(deserializedMap.get("bigDecimal")).isEqualTo(expectedBigDecimal); assertThat(deserializedMap.get("string")).isEqualTo(expectedStringValue);
@Nonnull public Set<String> merge(@Nonnull final Record apolloRecord, @Nonnull final CacheHeaders cacheHeaders) { if (cacheHeaders.hasHeader(DO_NOT_STORE)) { return Collections.emptySet(); } //noinspection ResultOfMethodCallIgnored Optional<NormalizedCache> normalizedCacheOptional = nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@Nonnull NormalizedCache cache) { cache.merge(apolloRecord, cacheHeaders); } }); Optional<Record> optionalOldRecord = selectRecordForKey(apolloRecord.key()); Set<String> changedKeys; if (!optionalOldRecord.isPresent()) { createRecord(apolloRecord.key(), recordFieldAdapter.toJson(apolloRecord.fields())); changedKeys = Collections.emptySet(); } else { Record oldRecord = optionalOldRecord.get(); changedKeys = oldRecord.mergeWith(apolloRecord); if (!changedKeys.isEmpty()) { updateRecord(oldRecord.key(), recordFieldAdapter.toJson(oldRecord.fields())); } } return changedKeys; }
public Map<String, Object> from(String jsonFieldSource) throws IOException { final BufferedSource bufferSource = Okio.buffer(Okio.source(new ByteArrayInputStream(jsonFieldSource.getBytes(Charset.defaultCharset())))); return fromBufferSource(bufferSource); }
public Map<String, Object> from(String jsonFieldSource) throws IOException { final BufferedSource bufferSource = Okio.buffer(Okio.source(new ByteArrayInputStream(jsonFieldSource.getBytes()))); return from(bufferSource); }
@Before public void createFieldAdapter() { basicFieldAdapter = RecordFieldJsonAdapter.create(); }
jsonWriter.beginArray(); for (Object item : (List) value) { writeJsonValue(item, jsonWriter); for (Map.Entry<String, Object> fieldEntry : fields.entrySet()) { jsonWriter.name(fieldEntry.getKey()); writeJsonValue(fieldEntry.getValue(), jsonWriter);
Record cursorToRecord(Cursor cursor) throws IOException { String key = cursor.getString(1); String jsonOfFields = cursor.getString(2); return Record.builder(key).addFields(recordFieldAdapter.from(jsonOfFields)).build(); }
public static RecordFieldJsonAdapter create() { return new RecordFieldJsonAdapter(); }
Optional<CacheKeyResolver> cacheKeyResolver = this.cacheKeyResolver; if (cacheFactory.isPresent() && cacheKeyResolver.isPresent()) { final NormalizedCache normalizedCache = cacheFactory.get().createChain(RecordFieldJsonAdapter.create()); apolloStore = new RealApolloStore(normalizedCache, cacheKeyResolver.get(), scalarTypeAdapters, dispatcher, apolloLogger);
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); } }
public static RecordFieldJsonAdapter create() { return new RecordFieldJsonAdapter(); }
Optional<CacheKeyResolver> cacheKeyResolver = this.cacheKeyResolver; if (cacheFactory.isPresent() && cacheKeyResolver.isPresent()) { final NormalizedCache normalizedCache = cacheFactory.get().createChain(RecordFieldJsonAdapter.create()); apolloStore = new RealAppSyncStore(normalizedCache, cacheKeyResolver.get(), scalarTypeAdapters, dispatcher, apolloLogger);
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); } }
Optional<CacheKeyResolver> cacheKeyResolver = this.cacheKeyResolver; if (cacheFactory.isPresent() && cacheKeyResolver.isPresent()) { final NormalizedCache normalizedCache = cacheFactory.get().createChain(RecordFieldJsonAdapter.create()); apolloStore = new RealAppSyncStore(normalizedCache, cacheKeyResolver.get(), scalarTypeAdapters, dispatcher, apolloLogger);
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()); } } }
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()); } } }