Rename toJsonObject() to toJsonValue(), fromJsonObject() to fromJsonValue().

Also rename the JsonReader and JsonWriter implementations.
This commit is contained in:
jwilson
2017-02-02 21:28:38 -05:00
parent 5b5a1a6b9b
commit e9d8538ec3
14 changed files with 75 additions and 79 deletions

View File

@@ -56,20 +56,17 @@ public abstract class JsonAdapter<T> {
}
/**
* Encodes {@code value} as a Java model comprised of maps, lists, strings, numbers, booleans
* and nulls.
* Encodes {@code value} as a Java value object comprised of maps, lists, strings, numbers,
* booleans, and nulls.
*
* <p>Values encoded using {@code value(double)} or {@code value(long)} are modeled with the
* corresponding boxed type. Values encoded using {@code value(Number)} are modeled as a
* {@link Long} for boxed integer types ({@link Byte}, {@link Short}, {@link Integer}, and {@link
* Long}), as a {@link Double} for boxed floating point types ({@link Float} and {@link Double}),
* and as a {@link BigDecimal} for all other types.
*
* <p>The returned model is equivalent to calling {@link #toJson} to encode {@code value}
* as a JSON string, and then parsing that string without any particular type.
*/
public final Object toJsonObject(T value) {
ObjectJsonWriter writer = new ObjectJsonWriter();
public final Object toJsonValue(T value) {
JsonValueWriter writer = new JsonValueWriter();
try {
toJson(writer, value);
return writer.root();
@@ -79,12 +76,11 @@ public abstract class JsonAdapter<T> {
}
/**
* Decodes a Java value from {@code object}, which must be a Java model comprised of maps, lists,
* strings, numbers, booleans and nulls. This is equivalent to encoding {@code object} to a JSON
* string, and then calling {@link #fromJson} to decode that string.
* Decodes a Java value object from {@code value}, which must be comprised of maps, lists,
* strings, numbers, booleans and nulls.
*/
public final T fromJsonObject(Object object) {
ObjectJsonReader reader = new ObjectJsonReader(object);
public final T fromJsonValue(Object value) {
JsonValueReader reader = new JsonValueReader(value);
try {
return fromJson(reader);
} catch (IOException e) {

View File

@@ -185,9 +185,9 @@ public abstract class JsonReader implements Closeable {
/** True to throw a {@link JsonDataException} on any attempt to call {@link #skipValue()}. */
boolean failOnUnknown;
/** Returns a new instance that reads a JSON-encoded stream from {@code source}. */
/** Returns a new instance that reads UTF-8 encoded JSON from {@code source}. */
public static JsonReader of(BufferedSource source) {
return new BufferedSourceJsonReader(source);
return new JsonUtf8Reader(source);
}
JsonReader() {
@@ -422,7 +422,7 @@ public abstract class JsonReader implements Closeable {
ByteString[] result = new ByteString[strings.length];
Buffer buffer = new Buffer();
for (int i = 0; i < strings.length; i++) {
BufferedSinkJsonWriter.string(buffer, strings[i]);
JsonUt8Writer.string(buffer, strings[i]);
buffer.readByte(); // Skip the leading double quote (but leave the trailing one).
result[i] = buffer.readByteString();
}

View File

@@ -27,7 +27,7 @@ import static com.squareup.moshi.JsonScope.NONEMPTY_ARRAY;
import static com.squareup.moshi.JsonScope.NONEMPTY_DOCUMENT;
import static com.squareup.moshi.JsonScope.NONEMPTY_OBJECT;
final class BufferedSinkJsonWriter extends JsonWriter {
final class JsonUt8Writer extends JsonWriter {
/*
* From RFC 7159, "All Unicode characters may be placed within the
@@ -62,7 +62,7 @@ final class BufferedSinkJsonWriter extends JsonWriter {
private String deferredName;
BufferedSinkJsonWriter(BufferedSink sink) {
JsonUt8Writer(BufferedSink sink) {
if (sink == null) {
throw new NullPointerException("sink == null");
}

View File

@@ -22,7 +22,7 @@ import okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
final class BufferedSourceJsonReader extends JsonReader {
final class JsonUtf8Reader extends JsonReader {
private static final long MIN_INCOMPLETE_INTEGER = Long.MIN_VALUE / 10;
private static final ByteString SINGLE_QUOTE_OR_SLASH = ByteString.encodeUtf8("'\\");
@@ -88,7 +88,7 @@ final class BufferedSourceJsonReader extends JsonReader {
*/
private String peekedString;
BufferedSourceJsonReader(BufferedSource source) {
JsonUtf8Reader(BufferedSource source) {
if (source == null) {
throw new NullPointerException("source == null");
}

View File

@@ -44,13 +44,13 @@ import java.util.Map;
* #endObject} will pop it.
* </ul>
*/
final class ObjectJsonReader extends JsonReader {
final class JsonValueReader extends JsonReader {
/** Sentinel object pushed on {@link #stack} when the reader is closed. */
private static final Object JSON_READER_CLOSED = new Object();
private final Object[] stack = new Object[32];
public ObjectJsonReader(Object root) {
public JsonValueReader(Object root) {
scopes[stackSize] = JsonScope.NONEMPTY_DOCUMENT;
stack[stackSize++] = root;
}

View File

@@ -29,11 +29,11 @@ import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;
/** Writes JSON by building a Java object comprising maps, lists, and JSON primitives. */
final class ObjectJsonWriter extends JsonWriter {
final class JsonValueWriter extends JsonWriter {
private final Object[] stack = new Object[32];
private String deferredName;
ObjectJsonWriter() {
JsonValueWriter() {
pushScope(EMPTY_DOCUMENT);
}
@@ -195,7 +195,7 @@ final class ObjectJsonWriter extends JsonWriter {
}
}
private ObjectJsonWriter add(Object newTop) {
private JsonValueWriter add(Object newTop) {
int scope = peekScope();
if (stackSize == 1) {

View File

@@ -136,9 +136,9 @@ public abstract class JsonWriter implements Closeable, Flushable {
boolean serializeNulls;
boolean promoteValueToName;
/** Returns a new instance that writes a JSON-encoded stream to {@code sink}. */
/** Returns a new instance that writes UTF-8 encoded JSON to {@code sink}. */
public static JsonWriter of(BufferedSink sink) {
return new BufferedSinkJsonWriter(sink);
return new JsonUt8Writer(sink);
}
JsonWriter() {

View File

@@ -25,7 +25,7 @@ abstract class JsonCodecFactory {
private static final JsonAdapter<Object> OBJECT_ADAPTER = MOSHI.adapter(Object.class);
static List<Object[]> factories() {
final JsonCodecFactory bufferedSink = new JsonCodecFactory() {
final JsonCodecFactory utf8 = new JsonCodecFactory() {
Buffer buffer;
@Override public JsonReader newReader(String json) {
@@ -35,7 +35,7 @@ abstract class JsonCodecFactory {
@Override JsonWriter newWriter() {
buffer = new Buffer();
return new BufferedSinkJsonWriter(buffer);
return new JsonUt8Writer(buffer);
}
@Override String json() {
@@ -49,17 +49,17 @@ abstract class JsonCodecFactory {
}
@Override public String toString() {
return "Buffer";
return "Utf8";
}
};
final JsonCodecFactory object = new JsonCodecFactory() {
ObjectJsonWriter writer;
final JsonCodecFactory value = new JsonCodecFactory() {
JsonValueWriter writer;
@Override public JsonReader newReader(String json) throws IOException {
Moshi moshi = new Moshi.Builder().build();
Object object = moshi.adapter(Object.class).lenient().fromJson(json);
return new ObjectJsonReader(object);
return new JsonValueReader(object);
}
// TODO(jwilson): fix precision checks and delete his method.
@@ -68,7 +68,7 @@ abstract class JsonCodecFactory {
}
@Override JsonWriter newWriter() {
writer = new ObjectJsonWriter();
writer = new JsonValueWriter();
return writer;
}
@@ -92,13 +92,13 @@ abstract class JsonCodecFactory {
}
@Override public String toString() {
return "Object";
return "Value";
}
};
return Arrays.asList(
new Object[] { bufferedSink },
new Object[] { object });
new Object[] { utf8 },
new Object[] { value });
}
abstract JsonReader newReader(String json) throws IOException;

View File

@@ -21,7 +21,7 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public final class BufferedSinkJsonWriterTest {
public final class JsonUt8WriterTest {
@Test public void prettyPrintObject() throws IOException {
Buffer buffer = new Buffer();
JsonWriter writer = JsonWriter.of(buffer);

View File

@@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public final class BufferedSourceJsonReaderTest {
public final class JsonUtf8ReaderTest {
@Test public void readingDoesNotBuffer() throws IOException {
Buffer buffer = new Buffer().writeUtf8("{}{}");

View File

@@ -28,14 +28,14 @@ import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
public final class ObjectJsonReaderTest {
public final class JsonValueReaderTest {
@Test public void array() throws Exception {
List<Object> root = new ArrayList<>();
root.add("s");
root.add(1.5d);
root.add(true);
root.add(null);
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY);
@@ -70,7 +70,7 @@ public final class ObjectJsonReaderTest {
root.put("b", 1.5d);
root.put("c", true);
root.put("d", null);
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_OBJECT);
@@ -110,7 +110,7 @@ public final class ObjectJsonReaderTest {
@Test public void nesting() throws Exception {
List<Map<String, List<Map<String, Double>>>> root
= singletonList(singletonMap("a", singletonList(singletonMap("b", 1.5d))));
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY);
@@ -162,7 +162,7 @@ public final class ObjectJsonReaderTest {
@Test public void promoteNameToValue() throws Exception {
Map<String, String> root = singletonMap("a", "b");
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
reader.beginObject();
reader.promoteNameToValue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.STRING);
@@ -176,7 +176,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void endArrayTooEarly() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList("s"));
JsonReader reader = new JsonValueReader(singletonList("s"));
reader.beginArray();
try {
@@ -189,7 +189,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void endObjectTooEarly() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonMap("a", "b"));
JsonReader reader = new JsonValueReader(singletonMap("a", "b"));
reader.beginObject();
try {
@@ -201,7 +201,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unsupportedType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("x")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("x")));
reader.beginArray();
try {
@@ -214,7 +214,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unsupportedKeyType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonMap(new StringBuilder("x"), "y"));
JsonReader reader = new JsonValueReader(singletonMap(new StringBuilder("x"), "y"));
reader.beginObject();
try {
@@ -227,7 +227,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void nullKey() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonMap(null, "y"));
JsonReader reader = new JsonValueReader(singletonMap(null, "y"));
reader.beginObject();
try {
@@ -239,7 +239,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedIntType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray();
try {
reader.nextInt();
@@ -251,7 +251,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedLongType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray();
try {
reader.nextLong();
@@ -263,7 +263,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedDoubleType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray();
try {
reader.nextDouble();
@@ -275,7 +275,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedStringType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("s")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("s")));
reader.beginArray();
try {
reader.nextString();
@@ -287,7 +287,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedBooleanType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("true")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("true")));
reader.beginArray();
try {
reader.nextBoolean();
@@ -299,7 +299,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void unexpectedNullType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("null")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("null")));
reader.beginArray();
try {
reader.nextNull();
@@ -311,7 +311,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void skipRoot() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("x")));
JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("x")));
reader.skipValue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}
@@ -321,7 +321,7 @@ public final class ObjectJsonReaderTest {
root.add("a");
root.add("b");
root.add("c");
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
reader.beginArray();
@@ -342,7 +342,7 @@ public final class ObjectJsonReaderTest {
root.put("a", "s");
root.put("b", 1.5d);
root.put("c", true);
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
reader.beginObject();
@@ -369,7 +369,7 @@ public final class ObjectJsonReaderTest {
root.put("a", "s");
root.put("b", 1.5d);
root.put("c", true);
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
reader.beginObject();
@@ -392,7 +392,7 @@ public final class ObjectJsonReaderTest {
}
@Test public void failOnUnknown() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList("a"));
JsonReader reader = new JsonValueReader(singletonList("a"));
reader.setFailOnUnknown(true);
reader.beginArray();
@@ -406,7 +406,7 @@ public final class ObjectJsonReaderTest {
@Test public void close() throws Exception {
try {
JsonReader reader = new ObjectJsonReader(singletonList("a"));
JsonReader reader = new JsonValueReader(singletonList("a"));
reader.beginArray();
reader.close();
reader.nextString();
@@ -415,7 +415,7 @@ public final class ObjectJsonReaderTest {
}
try {
JsonReader reader = new ObjectJsonReader(singletonList("a"));
JsonReader reader = new JsonValueReader(singletonList("a"));
reader.close();
reader.beginArray();
fail();
@@ -428,7 +428,7 @@ public final class ObjectJsonReaderTest {
for (int i = 0; i < 32; i++) {
root = singletonList(root);
}
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
for (int i = 0; i < 31; i++) {
reader.beginArray();
}
@@ -446,7 +446,7 @@ public final class ObjectJsonReaderTest {
for (int i = 0; i < 32; i++) {
root = singletonMap("a", root);
}
JsonReader reader = new ObjectJsonReader(root);
JsonReader reader = new JsonValueReader(root);
for (int i = 0; i < 31; i++) {
reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a");

View File

@@ -29,10 +29,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.fail;
public final class ObjectJsonWriterTest {
public final class JsonValueWriterTest {
@SuppressWarnings("unchecked")
@Test public void array() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value("s");
@@ -45,7 +45,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void object() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.setSerializeNulls(true);
writer.beginObject();
@@ -60,7 +60,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void repeatedNameThrows() throws IOException {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginObject();
writer.name("a").value(1L);
try {
@@ -73,7 +73,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void valueLongEmitsLong() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value(Long.MIN_VALUE);
writer.value(-1L);
@@ -92,7 +92,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void valueDoubleEmitsDouble() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.setLenient(true);
writer.beginArray();
writer.value(-2147483649.0d);
@@ -146,7 +146,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void primitiveIntegerTypesEmitLong() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value(new Byte(Byte.MIN_VALUE));
writer.value(new Short(Short.MIN_VALUE));
@@ -163,7 +163,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void primitiveFloatingPointTypesEmitDouble() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value(new Float(0.5f));
writer.value(new Double(0.5d));
@@ -176,7 +176,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void otherNumberTypesEmitBigDecimal() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value(new AtomicInteger(-2147483648));
writer.value(new AtomicLong(-9223372036854775808L));
@@ -223,7 +223,7 @@ public final class ObjectJsonWriterTest {
}
@Test public void valueCustomNumberTypeEmitsLongOrBigDecimal() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter();
JsonValueWriter writer = new JsonValueWriter();
writer.beginArray();
writer.value(stringNumber("-9223372036854775809"));
writer.value(stringNumber("-9223372036854775808"));

View File

@@ -137,8 +137,8 @@ public final class MapJsonAdapterTest {
jsonObject.put("7", null);
JsonAdapter<Map<Integer, Boolean>> jsonAdapter = mapAdapter(Integer.class, Boolean.class);
assertThat(jsonAdapter.serializeNulls().toJsonObject(map)).isEqualTo(jsonObject);
assertThat(jsonAdapter.fromJsonObject(jsonObject)).isEqualTo(map);
assertThat(jsonAdapter.serializeNulls().toJsonValue(map)).isEqualTo(jsonObject);
assertThat(jsonAdapter.fromJsonValue(jsonObject)).isEqualTo(map);
}
private <K, V> String toJson(Type keyType, Type valueType, Map<K, V> value) throws IOException {

View File

@@ -611,8 +611,8 @@ public final class MoshiTest {
pizzaObject.put("extraCheese", true);
JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class);
assertThat(jsonAdapter.toJsonObject(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonObject(pizzaObject)).isEqualTo(pizza);
assertThat(jsonAdapter.toJsonValue(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonValue(pizzaObject)).isEqualTo(pizza);
}
@Test public void customJsonAdapterToObjectAndFromObject() throws Exception {
@@ -627,8 +627,8 @@ public final class MoshiTest {
pizzaObject.put("extra cheese", true);
JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class);
assertThat(jsonAdapter.toJsonObject(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonObject(pizzaObject)).isEqualTo(pizza);
assertThat(jsonAdapter.toJsonValue(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonValue(pizzaObject)).isEqualTo(pizza);
}
@Test public void indent() throws Exception {