Stream to a DOM
This commit is contained in:
parent
d26c818918
commit
364de80611
@ -51,8 +51,11 @@ public final class JsonElementWriter extends JsonWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JsonElement get() {
|
public JsonElement get() {
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
return JsonNull.INSTANCE; // TODO: is this really what we want?
|
||||||
|
}
|
||||||
if (stack.size() != 1) {
|
if (stack.size() != 1) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException("Expected one JSON element but was " + stack);
|
||||||
}
|
}
|
||||||
return stack.get(0);
|
return stack.get(0);
|
||||||
}
|
}
|
||||||
@ -129,6 +132,9 @@ public final class JsonElementWriter extends JsonWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public JsonWriter value(String value) throws IOException {
|
@Override public JsonWriter value(String value) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
return nullValue();
|
||||||
|
}
|
||||||
put(new JsonPrimitive(value));
|
put(new JsonPrimitive(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -154,6 +160,9 @@ public final class JsonElementWriter extends JsonWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public JsonWriter value(Number value) throws IOException {
|
@Override public JsonWriter value(Number value) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
return nullValue();
|
||||||
|
}
|
||||||
put(new JsonPrimitive(value));
|
put(new JsonPrimitive(value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,9 @@ package com.google.gson.internal.bind;
|
|||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonIOException;
|
import com.google.gson.JsonIOException;
|
||||||
import com.google.gson.internal.Streams;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
@ -56,13 +54,10 @@ public abstract class TypeAdapter<T> {
|
|||||||
|
|
||||||
public JsonElement toJsonElement(T src) {
|
public JsonElement toJsonElement(T src) {
|
||||||
try {
|
try {
|
||||||
StringWriter stringWriter = new StringWriter();
|
JsonElementWriter jsonWriter = new JsonElementWriter();
|
||||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
|
||||||
jsonWriter.setLenient(true);
|
jsonWriter.setLenient(true);
|
||||||
write(jsonWriter, src);
|
write(jsonWriter, src);
|
||||||
JsonReader reader = new JsonReader(new StringReader(stringWriter.toString()));
|
return jsonWriter.get();
|
||||||
reader.setLenient(true);
|
|
||||||
return Streams.parse(reader);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new JsonIOException(e);
|
throw new JsonIOException(e);
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ public final class JsonElementWriterTest extends TestCase {
|
|||||||
|
|
||||||
// TODO: more tests
|
// TODO: more tests
|
||||||
// TODO: close support
|
// TODO: close support
|
||||||
|
// TODO: figure out what should be returned by an empty writer
|
||||||
|
|
||||||
public void testArray() throws IOException {
|
public void testArray() throws IOException {
|
||||||
JsonElementWriter writer = new JsonElementWriter();
|
JsonElementWriter writer = new JsonElementWriter();
|
||||||
|
Loading…
Reference in New Issue
Block a user