@SuppressWarnings("unchecked") // We require that field's values are of type T. void write(JsonWriter writer, Object value) throws IllegalAccessException, IOException { T fieldValue = (T) field.get(value); adapter.toJson(writer, fieldValue); } }
public void run() throws Exception { Map<String, String> requestBody = new LinkedHashMap<>(); requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/"); RequestBody jsonRequestBody = RequestBody.create( MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody)); Request request = new Request.Builder() .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY) .post(jsonRequestBody) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { delegate.toJson(writer, value); } @Override boolean isLenient() {
@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()); } }
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { if (value == null) { writer.nullValue(); } else { delegate.toJson(writer, value); } } @Override boolean isLenient() {
@CheckReturnValue public final String toJson(@Nullable T value) { Buffer buffer = new Buffer(); try { toJson(buffer, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to a Buffer. } return buffer.readUtf8(); }
@Override public void toJson(Moshi moshi, JsonWriter writer, @Nullable Object value) throws IOException, InvocationTargetException { Object intermediate = invoke(value); delegate.toJson(writer, intermediate); } };
@Override public void toJson(JsonWriter writer, Object value) throws IOException { delegate.toJson(writer, new Envelope<>(value)); } }
@Override public void toJson(JsonWriter writer, Object value) throws IOException { writer.beginArray(); for (int i = 0, size = Array.getLength(value); i < size; i++) { elementAdapter.toJson(writer, Array.get(value, i)); } writer.endArray(); }
@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, @Nullable T value) throws IOException { if (value == null) { throw new JsonDataException("Unexpected null at " + writer.getPath()); } else { delegate.toJson(writer, value); } } @Override boolean isLenient() {
@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 writer, C value) throws IOException { writer.beginArray(); for (T element : value) { elementAdapter.toJson(writer, element); } writer.endArray(); }
@ToJson void toJson(JsonWriter writer, Card value, @CardString JsonAdapter<Card> stringAdapter) throws IOException { stringAdapter.toJson(writer, value); }
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException { boolean lenient = writer.isLenient(); writer.setLenient(true); try { delegate.toJson(writer, value); } finally { writer.setLenient(lenient); } } @Override boolean isLenient() {
@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 toJson(JsonWriter writer, @Nullable T value) throws IOException { String originalIndent = writer.getIndent(); writer.setIndent(indent); try { delegate.toJson(writer, value); } finally { writer.setIndent(originalIndent); } } @Override boolean isLenient() {
@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); } }
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(new MultipleFormatsCardAdapter()) .add(new CardStringAdapter()) .build(); JsonAdapter<Card> cardAdapter = moshi.adapter(Card.class); // Decode cards from one format or the other. System.out.println(cardAdapter.fromJson("\"5D\"")); System.out.println(cardAdapter.fromJson("{\"suit\": \"SPADES\", \"rank\": 5}")); // Cards are always encoded as strings. System.out.println(cardAdapter.toJson(new Card('5', Suit.CLUBS))); }
public void run() throws Exception { BlackjackHand blackjackHand = new BlackjackHand( new Card('6', SPADES), Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS))); Moshi moshi = new Moshi.Builder().build(); JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class); String json = jsonAdapter.toJson(blackjackHand); System.out.println(json); }