/** * Generates a dummy package.json file to silence npm warnings */ private void writePackageFile(File apolloPackageFile) { try { JsonWriter writer = JsonWriter.of(Okio.buffer(Okio.sink(apolloPackageFile))); writer.beginObject(); writer.name("name").value("apollo-android"); writer.name("version").value("0.0.1"); writer.name("description").value("Generates Java code based on a GraphQL schema and query documents. Uses " + "apollo-codegen under the hood."); writer.name("name").value("apollo-android"); writer.name("repository"); writer.beginObject(); writer.name("type").value("git"); writer.name("url").value("git+https://github.com/apollostack/apollo-android.git"); writer.endObject(); writer.name("author").value("Apollo"); writer.name("license").value("MIT"); writer.endObject(); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void toJson(JsonWriter writer, SortedSet<T> set) throws IOException { writer.beginArray(); for (T element : set) { elementAdapter.toJson(writer, element); } writer.endArray(); } }
@Override public void toJson(JsonWriter out, @Nullable M message) throws IOException { if (message == null) { out.nullValue(); return; } out.beginObject(); for (int i = 0; i < fieldBindings.length; i++) { FieldBinding<M, B> fieldBinding = fieldBindings[i]; out.name(fieldBinding.name); Object value = fieldBinding.get(message); ((JsonAdapter) jsonAdapters[i]).toJson(out, value); } out.endObject(); }
@Override public void toJson(JsonWriter writer, T value) throws IOException { try { writer.beginObject(); for (FieldBinding<?> fieldBinding : fieldsArray) { writer.name(fieldBinding.name); fieldBinding.write(writer, value); } writer.endObject(); } catch (IllegalAccessException e) { throw new AssertionError(); } }
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { boolean serializeNulls = writer.getSerializeNulls(); writer.setSerializeNulls(true); try { delegate.toJson(writer, value); } finally { writer.setSerializeNulls(serializeNulls); } } @Override boolean isLenient() {
@Override public void moshi(com.squareup.moshi.JsonWriter writer, Users obj) throws IOException { writer.beginObject(); if (obj.users != null) { writer.name("users"); writer.beginArray(); for (User user : obj.users) { moshiUser(writer, user); } writer.endArray(); } writer.endObject(); }
@Override public void toJson(JsonWriter writer, String value) throws IOException { writer.value(value); }
@Override public void toJson(JsonWriter writer, Object value) throws IOException { Class<?> type = value.getClass(); int labelIndex = subtypes.indexOf(type); if (labelIndex == -1) { throw new IllegalArgumentException("Expected one of " + subtypes + " but found " + value + ", a " + value.getClass() + ". Register this subtype."); } JsonAdapter<Object> adapter = jsonAdapters.get(labelIndex); writer.beginObject(); writer.name(labelKey).value(labels.get(labelIndex)); int flattenToken = writer.beginFlatten(); adapter.toJson(writer, value); writer.endFlatten(flattenToken); writer.endObject(); }
private static void writeValueOrNull(JsonWriter writer, SafeCalendar calendar, String name, int field) throws IOException { writer.name(name); if (calendar.isSet(field)) { // Calendar and the XING API understand month differently. int value = calendar.get(field); writer.value(field == Calendar.MONTH ? value + 1 : value); } else { writer.nullValue(); } } }
@Override public void toJson(JsonWriter writer, SafeCalendar value) throws IOException { writer.setSerializeNulls(true); writer.beginObject(); writeValueOrNull(writer, value, "year", Calendar.YEAR); writeValueOrNull(writer, value, "month", Calendar.MONTH); writeValueOrNull(writer, value, "day", Calendar.DAY_OF_MONTH); writer.endObject(); writer.setSerializeNulls(false); }
@Override public void toJson(JsonWriter writer, Map<K, V> map) throws IOException { writer.beginObject(); for (Map.Entry<K, V> entry : map.entrySet()) { if (entry.getKey() == null) { throw new JsonDataException("Map key is null at " + writer.getPath()); } writer.promoteValueToName(); keyAdapter.toJson(writer, entry.getKey()); valueAdapter.toJson(writer, entry.getValue()); } writer.endObject(); }
@Override public void toJson(JsonWriter writer, Object value) throws IOException { Class<?> valueClass = value.getClass(); if (valueClass == Object.class) { // Don't recurse infinitely when the runtime type is also Object.class. writer.beginObject(); writer.endObject(); } else { moshi.adapter(toJsonType(valueClass), Util.NO_ANNOTATIONS).toJson(writer, value); } }
@Test public void maintainsPreviousSerializationValue() throws Exception { JsonAdapter<Data1> adapter = moshi.adapter(Data1.class); Data1 data1 = new Data1(); JsonWriter writer1 = JsonWriter.of(new Buffer()); writer1.setSerializeNulls(true); adapter.toJson(writer1, data1); assertThat(writer1.getSerializeNulls()).isTrue(); JsonWriter writer2 = JsonWriter.of(new Buffer()); writer2.setSerializeNulls(false); adapter.toJson(writer2, data1); assertThat(writer2.getSerializeNulls()).isFalse(); }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); JsonWriter writer = JsonWriter.of(buffer); adapter.toJson(writer, value); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
private <C extends Collection<String>> String toJson(Type collectionType, C collection) throws IOException { JsonAdapter<C> jsonAdapter = validCsAdapter(collectionType); Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.setSerializeNulls(true); //noinspection ConstantConditions At this point the adapter should not be null. jsonAdapter.lenient().toJson(jsonWriter, collection); return buffer.readUtf8(); }
@Override public void toJson(JsonWriter writer, @Nullable Object value) throws IOException { if (toAdapter == null) { delegate.toJson(writer, value); } else if (!toAdapter.nullable && value == null) { writer.nullValue(); } else { try { toAdapter.toJson(moshi, writer, value); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof IOException) throw (IOException) cause; throw new JsonDataException(cause + " at " + writer.getPath(), cause); } } }
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { if (value == null) { writer.nullValue(); } else { delegate.toJson(writer, value); } } @Override boolean isLenient() {
private void moshiUser(com.squareup.moshi.JsonWriter writer, User user) throws IOException { writer.beginObject(); writer.name("_id"); writer.value(user._id); writer.name("index"); writer.value(user.index); writer.name("guid"); writer.value(user.guid); writer.name("isActive"); writer.value(user.isActive); writer.name("balance"); writer.value(user.balance); writer.name("picture"); writer.value(user.picture); writer.name("age"); writer.value(user.age); writer.name("eyeColor"); writer.value(user.eyeColor); writer.name("name"); writer.value(user.name);
@Override public void toJson(JsonWriter writer, Float value) throws IOException { // Manual null pointer check for the float.class adapter. if (value == null) { throw new NullPointerException(); } // Use the Number overload so we write out float precision instead of double precision. writer.value(value); }
@Override public void toJson(JsonWriter writer, Object value) throws IOException { Class<?> type = value.getClass(); int labelIndex = subtypes.indexOf(type); if (labelIndex == -1) { throw new IllegalArgumentException("Expected one of " + subtypes + " but found " + value + ", a " + value.getClass() + ". Register this subtype."); } JsonAdapter<Object> adapter = jsonAdapters.get(labelIndex); writer.beginObject(); writer.name(labelKey).value(labels.get(labelIndex)); int flattenToken = writer.beginFlatten(); adapter.toJson(writer, value); writer.endFlatten(flattenToken); writer.endObject(); }