Moshi.Builder#setPrettyPrinting + failing test
This commit is contained in:
parent
b024b6a1cb
commit
176e9d0685
4 changed files with 77 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -136,6 +136,35 @@ public abstract class JsonAdapter<T> {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<T> indent(final String indent) {
|
||||
final JsonAdapter<T> delegate = this;
|
||||
return new JsonAdapter<T>() {
|
||||
@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
|
||||
|
|
|
@ -68,7 +68,7 @@ import okio.BufferedSink;
|
|||
* This code encodes the above structure: <pre> {@code
|
||||
* public void writeJsonStream(BufferedSink sink, List<Message> 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 <a
|
||||
|
|
|
@ -33,7 +33,7 @@ import java.util.Locale;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import org.junit.Assert;
|
||||
import okio.Buffer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.moshi.TestUtil.newReader;
|
||||
|
@ -546,6 +546,42 @@ public final class MoshiTest {
|
|||
.isEqualTo(new Pizza(18, true));
|
||||
}
|
||||
|
||||
@Test public void indent() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder().add(Pizza.class, new PizzaAdapter()).build();
|
||||
JsonAdapter<Pizza> 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<Pizza> 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())
|
||||
|
|
Loading…
Reference in a new issue