From 06412db7086ce0d87da4435401e891b39f872ba8 Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Fri, 16 Nov 2018 15:46:02 -0800 Subject: [PATCH] Add an example for readJsonString and value(BufferedSource). --- .../moshi/recipes/ReadJsonString.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/src/main/java/com/squareup/moshi/recipes/ReadJsonString.java diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonString.java b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonString.java new file mode 100644 index 0000000..a277d0e --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonString.java @@ -0,0 +1,64 @@ +package com.squareup.moshi.recipes; + +import com.squareup.moshi.FromJson; +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.JsonQualifier; +import com.squareup.moshi.JsonReader; +import com.squareup.moshi.JsonWriter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.ToJson; +import java.io.IOException; +import java.lang.annotation.Retention; +import okio.Buffer; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +final class ReadJsonString { + @Retention(RUNTIME) + @JsonQualifier + public @interface JsonString { + Object ADAPTER = new Object() { + @FromJson @JsonString String fromJson(JsonReader reader) throws IOException { + return reader.readJsonString().utf8(); + } + + @ToJson void toJson(JsonWriter writer, @JsonString String value) throws IOException { + writer.value(new Buffer().writeUtf8(value)); + } + }; + } + + static final class Location { + final String name; + final @JsonString String metadata; + + Location(String name, String metadata) { + this.name = name; + this.metadata = metadata; + } + } + + void run() throws IOException { + Moshi moshi = new Moshi.Builder() + .add(JsonString.ADAPTER) + .build(); + JsonAdapter locationAdapter = moshi.adapter(Location.class); + String json = "{\n" + + " \"name\": \"Niagara Falls\",\n" + + " \"metadata\": {\n" + + " \"latitude\": 43.0962,\n" + + " \"longitude\": -79.0377\n" + + " }\n" + + "}"; + Location location = locationAdapter.fromJson(json); + System.out.println(location.metadata); + System.out.println(locationAdapter.toJson(new Location("Niagara Falls", "{\n" + + " \"latitude\": 43.0962,\n" + + " \"longitude\": -79.0377\n" + + " }"))); + } + + public static void main(String[] args) throws IOException { + new ReadJsonString().run(); + } +}