@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { if (value == null) { writer.nullValue(); } else { delegate.toJson(writer, value); } } @Override boolean isLenient() {
@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 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, @Nullable T value) throws IOException { if (value == null) { writer.nullValue(); } else { delegate.toJson(writer, value); } } @Override boolean isLenient() {
@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); } } }
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(); } } }
/** * Recursively writes the respective roots forming a json object that resembles the {@code path} * wrapping the type of the {@code adapter}. */ private static <T> void toJson(JsonAdapter<T> adapter, JsonWriter writer, T value, String[] path, int index) throws IOException { if (value != null || writer.getSerializeNulls()) { if (index == path.length) { adapter.toJson(writer, value); } else { writer.beginObject(); writer.name(path[index]); toJson(adapter, writer, value, path, ++index); writer.endObject(); } } else { // If we don't propagate the null value the writer will throw. writer.nullValue(); } } }