From 364de8061173b4b91f4477a55059f68e765fc3d1 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 30 Sep 2011 06:32:33 +0000 Subject: [PATCH] Stream to a DOM --- .../google/gson/internal/bind/JsonElementWriter.java | 11 ++++++++++- .../com/google/gson/internal/bind/TypeAdapter.java | 9 ++------- .../gson/internal/bind/JsonElementWriterTest.java | 1 + 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java index a6ea3641..0a815617 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java @@ -51,8 +51,11 @@ public final class JsonElementWriter extends JsonWriter { } public JsonElement get() { + if (stack.isEmpty()) { + return JsonNull.INSTANCE; // TODO: is this really what we want? + } if (stack.size() != 1) { - throw new IllegalStateException(); + throw new IllegalStateException("Expected one JSON element but was " + stack); } return stack.get(0); } @@ -129,6 +132,9 @@ public final class JsonElementWriter extends JsonWriter { } @Override public JsonWriter value(String value) throws IOException { + if (value == null) { + return nullValue(); + } put(new JsonPrimitive(value)); return this; } @@ -154,6 +160,9 @@ public final class JsonElementWriter extends JsonWriter { } @Override public JsonWriter value(Number value) throws IOException { + if (value == null) { + return nullValue(); + } put(new JsonPrimitive(value)); return this; } diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java index 6e3f3f83..bedb778b 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java @@ -18,11 +18,9 @@ package com.google.gson.internal.bind; import com.google.gson.JsonElement; import com.google.gson.JsonIOException; -import com.google.gson.internal.Streams; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; - import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -56,13 +54,10 @@ public abstract class TypeAdapter { public JsonElement toJsonElement(T src) { try { - StringWriter stringWriter = new StringWriter(); - JsonWriter jsonWriter = new JsonWriter(stringWriter); + JsonElementWriter jsonWriter = new JsonElementWriter(); jsonWriter.setLenient(true); write(jsonWriter, src); - JsonReader reader = new JsonReader(new StringReader(stringWriter.toString())); - reader.setLenient(true); - return Streams.parse(reader); + return jsonWriter.get(); } catch (IOException e) { throw new JsonIOException(e); } diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java index 2ba05589..27931938 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java @@ -23,6 +23,7 @@ public final class JsonElementWriterTest extends TestCase { // TODO: more tests // TODO: close support + // TODO: figure out what should be returned by an empty writer public void testArray() throws IOException { JsonElementWriter writer = new JsonElementWriter();