From 176e9d068589d5df875f45680f854d2d13c18954 Mon Sep 17 00:00:00 2001 From: jmfayard Date: Sat, 7 Jan 2017 18:34:56 -0500 Subject: [PATCH] Moshi.Builder#setPrettyPrinting + failing test --- .../moshi/BufferedSinkJsonWriter.java | 4 ++ .../java/com/squareup/moshi/JsonAdapter.java | 29 ++++++++++++++ .../java/com/squareup/moshi/JsonWriter.java | 8 +++- .../java/com/squareup/moshi/MoshiTest.java | 38 ++++++++++++++++++- 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/moshi/src/main/java/com/squareup/moshi/BufferedSinkJsonWriter.java b/moshi/src/main/java/com/squareup/moshi/BufferedSinkJsonWriter.java index e2ac915..2927a19 100644 --- a/moshi/src/main/java/com/squareup/moshi/BufferedSinkJsonWriter.java +++ b/moshi/src/main/java/com/squareup/moshi/BufferedSinkJsonWriter.java @@ -105,6 +105,10 @@ final class BufferedSinkJsonWriter extends JsonWriter { } } + @Override public final String getIndent() { + return indent != null ? indent : ""; + } + @Override public final void setLenient(boolean lenient) { this.lenient = lenient; } diff --git a/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java b/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java index 8e2755f..ace81ba 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonAdapter.java @@ -136,6 +136,35 @@ public abstract class JsonAdapter { }; } + /** + * Return a JSON adapter equal to this, but using {@code indent} to control how the result is + * formatted. The {@code indent} string to be repeated for each level of indentation in the + * encoded document. If {@code indent.isEmpty()} the encoded document will be compact. Otherwise + * the encoded document will be more human-readable. + * + * @param indent a string containing only whitespace. + */ + public JsonAdapter indent(final String indent) { + final JsonAdapter delegate = this; + return new JsonAdapter() { + @Override public T fromJson(JsonReader reader) throws IOException { + return delegate.fromJson(reader); + } + @Override public void toJson(JsonWriter writer, T value) throws IOException { + String originalIndent = writer.getIndent(); + writer.setIndent(indent); + try { + delegate.toJson(writer, value); + } finally { + writer.setIndent(originalIndent); + } + } + @Override public String toString() { + return delegate + ".indent(\"" + indent + "\")"; + } + }; + } + public interface Factory { /** * Attempts to create an adapter for {@code type} annotated with {@code annotations}. This diff --git a/moshi/src/main/java/com/squareup/moshi/JsonWriter.java b/moshi/src/main/java/com/squareup/moshi/JsonWriter.java index d069477..f8d1e4e 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonWriter.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonWriter.java @@ -68,7 +68,7 @@ import okio.BufferedSink; * This code encodes the above structure:
   {@code
  *   public void writeJsonStream(BufferedSink sink, List messages) throws IOException {
  *     JsonWriter writer = JsonWriter.of(sink);
- *     writer.setIndentSpaces(4);
+ *     writer.setIndent("  ");
  *     writeMessagesArray(writer, messages);
  *     writer.close();
  *   }
@@ -137,6 +137,12 @@ public abstract class JsonWriter implements Closeable, Flushable {
    */
   public abstract void setIndent(String indent);
 
+  /**
+   * Returns a string containing only whitespace, used for each level of
+   * indentation. If empty, the encoded document will be compact.
+   */
+  public abstract String getIndent();
+
   /**
    * Configure this writer to relax its syntax rules. By default, this writer
    * only emits well-formed JSON as specified by  jsonAdapter = moshi.adapter(Pizza.class);
+
+    Pizza pizza = new Pizza(15, true);
+    assertThat(jsonAdapter.indent("  ").toJson(pizza)).isEqualTo(""
+        + "{\n"
+        + "  \"size\": 15,\n"
+        + "  \"extra cheese\": true\n"
+        + "}");
+  }
+
+  @Test public void unindent() throws Exception {
+    Moshi moshi = new Moshi.Builder().add(Pizza.class, new PizzaAdapter()).build();
+    JsonAdapter jsonAdapter = moshi.adapter(Pizza.class);
+
+    Buffer buffer = new Buffer();
+    JsonWriter writer = JsonWriter.of(buffer);
+    writer.setLenient(true);
+    writer.setIndent("  ");
+
+    Pizza pizza = new Pizza(15, true);
+
+    // Calling JsonAdapter.indent("") can remove indentation.
+    jsonAdapter.indent("").toJson(writer, pizza);
+    assertThat(buffer.readUtf8()).isEqualTo("{\"size\":15,\"extra cheese\":true}");
+
+    // Indentation changes only apply to their use.
+    jsonAdapter.toJson(writer, pizza);
+    assertThat(buffer.readUtf8()).isEqualTo(""
+        + "{\n"
+        + "  \"size\": 15,\n"
+        + "  \"extra cheese\": true\n"
+        + "}");
+  }
+
   @Test public void composingJsonAdapterFactory() throws Exception {
     Moshi moshi = new Moshi.Builder()
         .add(new MealDealAdapterFactory())