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 * Encodes {@code value} as a Java value object comprised of maps, lists, strings, numbers,
* and nulls. * booleans, and nulls.
* *
* <p>Values encoded using {@code value(double)} or {@code value(long)} are modeled with the * <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 * 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 * {@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}), * Long}), as a {@link Double} for boxed floating point types ({@link Float} and {@link Double}),
* and as a {@link BigDecimal} for all other types. * 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) { public final Object toJsonValue(T value) {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
try { try {
toJson(writer, value); toJson(writer, value);
return writer.root(); 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, * Decodes a Java value object from {@code value}, which must be comprised of maps, lists,
* strings, numbers, booleans and nulls. This is equivalent to encoding {@code object} to a JSON * strings, numbers, booleans and nulls.
* string, and then calling {@link #fromJson} to decode that string.
*/ */
public final T fromJsonObject(Object object) { public final T fromJsonValue(Object value) {
ObjectJsonReader reader = new ObjectJsonReader(object); JsonValueReader reader = new JsonValueReader(value);
try { try {
return fromJson(reader); return fromJson(reader);
} catch (IOException e) { } 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()}. */ /** True to throw a {@link JsonDataException} on any attempt to call {@link #skipValue()}. */
boolean failOnUnknown; 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) { public static JsonReader of(BufferedSource source) {
return new BufferedSourceJsonReader(source); return new JsonUtf8Reader(source);
} }
JsonReader() { JsonReader() {
@@ -422,7 +422,7 @@ public abstract class JsonReader implements Closeable {
ByteString[] result = new ByteString[strings.length]; ByteString[] result = new ByteString[strings.length];
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
for (int i = 0; i < strings.length; i++) { 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). buffer.readByte(); // Skip the leading double quote (but leave the trailing one).
result[i] = buffer.readByteString(); 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_DOCUMENT;
import static com.squareup.moshi.JsonScope.NONEMPTY_OBJECT; 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 * From RFC 7159, "All Unicode characters may be placed within the
@@ -62,7 +62,7 @@ final class BufferedSinkJsonWriter extends JsonWriter {
private String deferredName; private String deferredName;
BufferedSinkJsonWriter(BufferedSink sink) { JsonUt8Writer(BufferedSink sink) {
if (sink == null) { if (sink == null) {
throw new NullPointerException("sink == null"); throw new NullPointerException("sink == null");
} }

View File

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

View File

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

View File

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

View File

@@ -136,9 +136,9 @@ public abstract class JsonWriter implements Closeable, Flushable {
boolean serializeNulls; boolean serializeNulls;
boolean promoteValueToName; 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) { public static JsonWriter of(BufferedSink sink) {
return new BufferedSinkJsonWriter(sink); return new JsonUt8Writer(sink);
} }
JsonWriter() { JsonWriter() {

View File

@@ -25,7 +25,7 @@ abstract class JsonCodecFactory {
private static final JsonAdapter<Object> OBJECT_ADAPTER = MOSHI.adapter(Object.class); private static final JsonAdapter<Object> OBJECT_ADAPTER = MOSHI.adapter(Object.class);
static List<Object[]> factories() { static List<Object[]> factories() {
final JsonCodecFactory bufferedSink = new JsonCodecFactory() { final JsonCodecFactory utf8 = new JsonCodecFactory() {
Buffer buffer; Buffer buffer;
@Override public JsonReader newReader(String json) { @Override public JsonReader newReader(String json) {
@@ -35,7 +35,7 @@ abstract class JsonCodecFactory {
@Override JsonWriter newWriter() { @Override JsonWriter newWriter() {
buffer = new Buffer(); buffer = new Buffer();
return new BufferedSinkJsonWriter(buffer); return new JsonUt8Writer(buffer);
} }
@Override String json() { @Override String json() {
@@ -49,17 +49,17 @@ abstract class JsonCodecFactory {
} }
@Override public String toString() { @Override public String toString() {
return "Buffer"; return "Utf8";
} }
}; };
final JsonCodecFactory object = new JsonCodecFactory() { final JsonCodecFactory value = new JsonCodecFactory() {
ObjectJsonWriter writer; JsonValueWriter writer;
@Override public JsonReader newReader(String json) throws IOException { @Override public JsonReader newReader(String json) throws IOException {
Moshi moshi = new Moshi.Builder().build(); Moshi moshi = new Moshi.Builder().build();
Object object = moshi.adapter(Object.class).lenient().fromJson(json); 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. // TODO(jwilson): fix precision checks and delete his method.
@@ -68,7 +68,7 @@ abstract class JsonCodecFactory {
} }
@Override JsonWriter newWriter() { @Override JsonWriter newWriter() {
writer = new ObjectJsonWriter(); writer = new JsonValueWriter();
return writer; return writer;
} }
@@ -92,13 +92,13 @@ abstract class JsonCodecFactory {
} }
@Override public String toString() { @Override public String toString() {
return "Object"; return "Value";
} }
}; };
return Arrays.asList( return Arrays.asList(
new Object[] { bufferedSink }, new Object[] { utf8 },
new Object[] { object }); new Object[] { value });
} }
abstract JsonReader newReader(String json) throws IOException; 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; import static org.assertj.core.api.Assertions.assertThat;
public final class BufferedSinkJsonWriterTest { public final class JsonUt8WriterTest {
@Test public void prettyPrintObject() throws IOException { @Test public void prettyPrintObject() throws IOException {
Buffer buffer = new Buffer(); Buffer buffer = new Buffer();
JsonWriter writer = JsonWriter.of(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.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public final class BufferedSourceJsonReaderTest { public final class JsonUtf8ReaderTest {
@Test public void readingDoesNotBuffer() throws IOException { @Test public void readingDoesNotBuffer() throws IOException {
Buffer buffer = new Buffer().writeUtf8("{}{}"); 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.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public final class ObjectJsonReaderTest { public final class JsonValueReaderTest {
@Test public void array() throws Exception { @Test public void array() throws Exception {
List<Object> root = new ArrayList<>(); List<Object> root = new ArrayList<>();
root.add("s"); root.add("s");
root.add(1.5d); root.add(1.5d);
root.add(true); root.add(true);
root.add(null); root.add(null);
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue(); assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY); assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY);
@@ -70,7 +70,7 @@ public final class ObjectJsonReaderTest {
root.put("b", 1.5d); root.put("b", 1.5d);
root.put("c", true); root.put("c", true);
root.put("d", null); root.put("d", null);
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue(); assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_OBJECT); assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_OBJECT);
@@ -110,7 +110,7 @@ public final class ObjectJsonReaderTest {
@Test public void nesting() throws Exception { @Test public void nesting() throws Exception {
List<Map<String, List<Map<String, Double>>>> root List<Map<String, List<Map<String, Double>>>> root
= singletonList(singletonMap("a", singletonList(singletonMap("b", 1.5d)))); = singletonList(singletonMap("a", singletonList(singletonMap("b", 1.5d))));
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
assertThat(reader.hasNext()).isTrue(); assertThat(reader.hasNext()).isTrue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY); assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_ARRAY);
@@ -162,7 +162,7 @@ public final class ObjectJsonReaderTest {
@Test public void promoteNameToValue() throws Exception { @Test public void promoteNameToValue() throws Exception {
Map<String, String> root = singletonMap("a", "b"); Map<String, String> root = singletonMap("a", "b");
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
reader.beginObject(); reader.beginObject();
reader.promoteNameToValue(); reader.promoteNameToValue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.STRING); assertThat(reader.peek()).isEqualTo(JsonReader.Token.STRING);
@@ -176,7 +176,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void endArrayTooEarly() throws Exception { @Test public void endArrayTooEarly() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList("s")); JsonReader reader = new JsonValueReader(singletonList("s"));
reader.beginArray(); reader.beginArray();
try { try {
@@ -189,7 +189,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void endObjectTooEarly() throws Exception { @Test public void endObjectTooEarly() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonMap("a", "b")); JsonReader reader = new JsonValueReader(singletonMap("a", "b"));
reader.beginObject(); reader.beginObject();
try { try {
@@ -201,7 +201,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unsupportedType() throws Exception { @Test public void unsupportedType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("x"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("x")));
reader.beginArray(); reader.beginArray();
try { try {
@@ -214,7 +214,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unsupportedKeyType() throws Exception { @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(); reader.beginObject();
try { try {
@@ -227,7 +227,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void nullKey() throws Exception { @Test public void nullKey() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonMap(null, "y")); JsonReader reader = new JsonValueReader(singletonMap(null, "y"));
reader.beginObject(); reader.beginObject();
try { try {
@@ -239,7 +239,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedIntType() throws Exception { @Test public void unexpectedIntType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextInt(); reader.nextInt();
@@ -251,7 +251,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedLongType() throws Exception { @Test public void unexpectedLongType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextLong(); reader.nextLong();
@@ -263,7 +263,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedDoubleType() throws Exception { @Test public void unexpectedDoubleType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("1"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("1")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextDouble(); reader.nextDouble();
@@ -275,7 +275,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedStringType() throws Exception { @Test public void unexpectedStringType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("s"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("s")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextString(); reader.nextString();
@@ -287,7 +287,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedBooleanType() throws Exception { @Test public void unexpectedBooleanType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("true"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("true")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextBoolean(); reader.nextBoolean();
@@ -299,7 +299,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void unexpectedNullType() throws Exception { @Test public void unexpectedNullType() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("null"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("null")));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextNull(); reader.nextNull();
@@ -311,7 +311,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void skipRoot() throws Exception { @Test public void skipRoot() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList(new StringBuilder("x"))); JsonReader reader = new JsonValueReader(singletonList(new StringBuilder("x")));
reader.skipValue(); reader.skipValue();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
} }
@@ -321,7 +321,7 @@ public final class ObjectJsonReaderTest {
root.add("a"); root.add("a");
root.add("b"); root.add("b");
root.add("c"); root.add("c");
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
reader.beginArray(); reader.beginArray();
@@ -342,7 +342,7 @@ public final class ObjectJsonReaderTest {
root.put("a", "s"); root.put("a", "s");
root.put("b", 1.5d); root.put("b", 1.5d);
root.put("c", true); root.put("c", true);
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
reader.beginObject(); reader.beginObject();
@@ -369,7 +369,7 @@ public final class ObjectJsonReaderTest {
root.put("a", "s"); root.put("a", "s");
root.put("b", 1.5d); root.put("b", 1.5d);
root.put("c", true); root.put("c", true);
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
reader.beginObject(); reader.beginObject();
@@ -392,7 +392,7 @@ public final class ObjectJsonReaderTest {
} }
@Test public void failOnUnknown() throws Exception { @Test public void failOnUnknown() throws Exception {
JsonReader reader = new ObjectJsonReader(singletonList("a")); JsonReader reader = new JsonValueReader(singletonList("a"));
reader.setFailOnUnknown(true); reader.setFailOnUnknown(true);
reader.beginArray(); reader.beginArray();
@@ -406,7 +406,7 @@ public final class ObjectJsonReaderTest {
@Test public void close() throws Exception { @Test public void close() throws Exception {
try { try {
JsonReader reader = new ObjectJsonReader(singletonList("a")); JsonReader reader = new JsonValueReader(singletonList("a"));
reader.beginArray(); reader.beginArray();
reader.close(); reader.close();
reader.nextString(); reader.nextString();
@@ -415,7 +415,7 @@ public final class ObjectJsonReaderTest {
} }
try { try {
JsonReader reader = new ObjectJsonReader(singletonList("a")); JsonReader reader = new JsonValueReader(singletonList("a"));
reader.close(); reader.close();
reader.beginArray(); reader.beginArray();
fail(); fail();
@@ -428,7 +428,7 @@ public final class ObjectJsonReaderTest {
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
root = singletonList(root); root = singletonList(root);
} }
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
reader.beginArray(); reader.beginArray();
} }
@@ -446,7 +446,7 @@ public final class ObjectJsonReaderTest {
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
root = singletonMap("a", root); root = singletonMap("a", root);
} }
JsonReader reader = new ObjectJsonReader(root); JsonReader reader = new JsonValueReader(root);
for (int i = 0; i < 31; i++) { for (int i = 0; i < 31; i++) {
reader.beginObject(); reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a"); 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.assertj.core.api.Assertions.entry;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public final class ObjectJsonWriterTest { public final class JsonValueWriterTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test public void array() throws Exception { @Test public void array() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value("s"); writer.value("s");
@@ -45,7 +45,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void object() throws Exception { @Test public void object() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.setSerializeNulls(true); writer.setSerializeNulls(true);
writer.beginObject(); writer.beginObject();
@@ -60,7 +60,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void repeatedNameThrows() throws IOException { @Test public void repeatedNameThrows() throws IOException {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginObject(); writer.beginObject();
writer.name("a").value(1L); writer.name("a").value(1L);
try { try {
@@ -73,7 +73,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void valueLongEmitsLong() throws Exception { @Test public void valueLongEmitsLong() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value(Long.MIN_VALUE); writer.value(Long.MIN_VALUE);
writer.value(-1L); writer.value(-1L);
@@ -92,7 +92,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void valueDoubleEmitsDouble() throws Exception { @Test public void valueDoubleEmitsDouble() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.setLenient(true); writer.setLenient(true);
writer.beginArray(); writer.beginArray();
writer.value(-2147483649.0d); writer.value(-2147483649.0d);
@@ -146,7 +146,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void primitiveIntegerTypesEmitLong() throws Exception { @Test public void primitiveIntegerTypesEmitLong() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value(new Byte(Byte.MIN_VALUE)); writer.value(new Byte(Byte.MIN_VALUE));
writer.value(new Short(Short.MIN_VALUE)); writer.value(new Short(Short.MIN_VALUE));
@@ -163,7 +163,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void primitiveFloatingPointTypesEmitDouble() throws Exception { @Test public void primitiveFloatingPointTypesEmitDouble() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value(new Float(0.5f)); writer.value(new Float(0.5f));
writer.value(new Double(0.5d)); writer.value(new Double(0.5d));
@@ -176,7 +176,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void otherNumberTypesEmitBigDecimal() throws Exception { @Test public void otherNumberTypesEmitBigDecimal() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value(new AtomicInteger(-2147483648)); writer.value(new AtomicInteger(-2147483648));
writer.value(new AtomicLong(-9223372036854775808L)); writer.value(new AtomicLong(-9223372036854775808L));
@@ -223,7 +223,7 @@ public final class ObjectJsonWriterTest {
} }
@Test public void valueCustomNumberTypeEmitsLongOrBigDecimal() throws Exception { @Test public void valueCustomNumberTypeEmitsLongOrBigDecimal() throws Exception {
ObjectJsonWriter writer = new ObjectJsonWriter(); JsonValueWriter writer = new JsonValueWriter();
writer.beginArray(); writer.beginArray();
writer.value(stringNumber("-9223372036854775809")); writer.value(stringNumber("-9223372036854775809"));
writer.value(stringNumber("-9223372036854775808")); writer.value(stringNumber("-9223372036854775808"));

View File

@@ -137,8 +137,8 @@ public final class MapJsonAdapterTest {
jsonObject.put("7", null); jsonObject.put("7", null);
JsonAdapter<Map<Integer, Boolean>> jsonAdapter = mapAdapter(Integer.class, Boolean.class); JsonAdapter<Map<Integer, Boolean>> jsonAdapter = mapAdapter(Integer.class, Boolean.class);
assertThat(jsonAdapter.serializeNulls().toJsonObject(map)).isEqualTo(jsonObject); assertThat(jsonAdapter.serializeNulls().toJsonValue(map)).isEqualTo(jsonObject);
assertThat(jsonAdapter.fromJsonObject(jsonObject)).isEqualTo(map); assertThat(jsonAdapter.fromJsonValue(jsonObject)).isEqualTo(map);
} }
private <K, V> String toJson(Type keyType, Type valueType, Map<K, V> value) throws IOException { 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); pizzaObject.put("extraCheese", true);
JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class); JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class);
assertThat(jsonAdapter.toJsonObject(pizza)).isEqualTo(pizzaObject); assertThat(jsonAdapter.toJsonValue(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonObject(pizzaObject)).isEqualTo(pizza); assertThat(jsonAdapter.fromJsonValue(pizzaObject)).isEqualTo(pizza);
} }
@Test public void customJsonAdapterToObjectAndFromObject() throws Exception { @Test public void customJsonAdapterToObjectAndFromObject() throws Exception {
@@ -627,8 +627,8 @@ public final class MoshiTest {
pizzaObject.put("extra cheese", true); pizzaObject.put("extra cheese", true);
JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class); JsonAdapter<Pizza> jsonAdapter = moshi.adapter(Pizza.class);
assertThat(jsonAdapter.toJsonObject(pizza)).isEqualTo(pizzaObject); assertThat(jsonAdapter.toJsonValue(pizza)).isEqualTo(pizzaObject);
assertThat(jsonAdapter.fromJsonObject(pizzaObject)).isEqualTo(pizza); assertThat(jsonAdapter.fromJsonValue(pizzaObject)).isEqualTo(pizza);
} }
@Test public void indent() throws Exception { @Test public void indent() throws Exception {