Added methods to convert to JsonElement in TypeAdapter.

Using lenient mode while working with Gson.
Handling nulls while invoking legacy Gson type adapters.
This commit is contained in:
Inderjeet Singh 2011-08-03 02:40:18 +00:00
parent 566c27cf21
commit d70fb90ef7
10 changed files with 67 additions and 38 deletions

View File

@ -28,6 +28,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.gson.internal.Streams;
import com.google.gson.internal.bind.ArrayTypeAdapter; import com.google.gson.internal.bind.ArrayTypeAdapter;
import com.google.gson.internal.bind.CollectionTypeAdapter; import com.google.gson.internal.bind.CollectionTypeAdapter;
import com.google.gson.internal.bind.MiniGson; import com.google.gson.internal.bind.MiniGson;
@ -264,13 +265,7 @@ public final class Gson {
public JsonElement toJsonTree(Object src, Type typeOfSrc) { public JsonElement toJsonTree(Object src, Type typeOfSrc) {
// Serialize 'src' to JSON, then deserialize that to a JSON tree. // Serialize 'src' to JSON, then deserialize that to a JSON tree.
TypeAdapter adapter = miniGson.getAdapter(TypeToken.get(typeOfSrc)); TypeAdapter adapter = miniGson.getAdapter(TypeToken.get(typeOfSrc));
StringWriter writer = new StringWriter(); return adapter.toJsonElement(src);
try {
adapter.write(writer, src);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Streams.parse(new JsonReader(new StringReader(writer.toString())));
} }
/** /**

View File

@ -21,6 +21,7 @@ import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import com.google.gson.internal.Streams;
import com.google.gson.internal.bind.MiniGson; import com.google.gson.internal.bind.MiniGson;
import com.google.gson.internal.bind.TypeAdapter; import com.google.gson.internal.bind.TypeAdapter;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -61,6 +62,10 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
// TODO: handle if serializer is null // TODO: handle if serializer is null
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
if (value == null) {
writer.nullValue();
return;
}
JsonElement element = serializer.serialize(value, typeOfT, createSerializationContext(miniGson)); JsonElement element = serializer.serialize(value, typeOfT, createSerializationContext(miniGson));
Streams.write(element, serializeNulls, writer); Streams.write(element, serializeNulls, writer);
} }
@ -71,15 +76,8 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
return new JsonSerializationContext() { return new JsonSerializationContext() {
@Override @Override
JsonElement serialize(Object src, Type typeOfSrc, boolean preserveType, boolean defaultOnly) { JsonElement serialize(Object src, Type typeOfSrc, boolean preserveType, boolean defaultOnly) {
try {
TypeToken typeToken = TypeToken.get(typeOfSrc); TypeToken typeToken = TypeToken.get(typeOfSrc);
String json = miniGson.getAdapter(typeToken).toJson(src); return miniGson.getAdapter(typeToken).toJsonElement(src);
JsonReader jsonReader = new JsonReader(new StringReader(json));
jsonReader.setLenient(true);
return Streams.parse(jsonReader);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
}; };
} }
@ -87,17 +85,8 @@ final class GsonToMiniGsonTypeAdapter implements TypeAdapter.Factory {
return new JsonDeserializationContext() { return new JsonDeserializationContext() {
@Override @Override
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
try {
TypeToken typeToken = TypeToken.get(typeOfT); TypeToken typeToken = TypeToken.get(typeOfT);
StringWriter stringWriter = new StringWriter(); return (T) miniGson.getAdapter(typeToken).fromJsonElement(json);
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setLenient(true);
Streams.write(json, serializeNulls, jsonWriter);
Object target = miniGson.getAdapter(typeToken).fromJson(stringWriter.toString());
return (T) target;
} catch (IOException e) {
throw new JsonParseException(e);
}
} }
}; };
} }

View File

@ -16,6 +16,7 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;

View File

@ -15,6 +15,7 @@
*/ */
package com.google.gson; package com.google.gson;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonToken;
import com.google.gson.stream.MalformedJsonException; import com.google.gson.stream.MalformedJsonException;

View File

@ -17,6 +17,8 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.$Gson$Preconditions;
import com.google.gson.internal.LazilyParsedNumber;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;

View File

@ -22,6 +22,7 @@ import java.io.StringReader;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonToken;
import com.google.gson.stream.MalformedJsonException; import com.google.gson.stream.MalformedJsonException;

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson; package com.google.gson.internal;
import java.math.BigInteger; import java.math.BigInteger;
@ -23,10 +23,10 @@ import java.math.BigInteger;
* @author Inderjeet Singh * @author Inderjeet Singh
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
final class LazilyParsedNumber extends Number { public final class LazilyParsedNumber extends Number {
private final String value; private final String value;
LazilyParsedNumber(String value) { public LazilyParsedNumber(String value) {
this.value = value; this.value = value;
} }

View File

@ -14,8 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson; package com.google.gson.internal;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
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 com.google.gson.stream.MalformedJsonException; import com.google.gson.stream.MalformedJsonException;
@ -27,12 +35,12 @@ import java.util.Map;
/** /**
* Reads and writes GSON parse trees over streams. * Reads and writes GSON parse trees over streams.
*/ */
final class Streams { public final class Streams {
/** /**
* Takes a reader in any state and returns the next value as a JsonElement. * Takes a reader in any state and returns the next value as a JsonElement.
*/ */
static JsonElement parse(JsonReader reader) throws JsonParseException { public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true; boolean isEmpty = true;
try { try {
reader.peek(); reader.peek();
@ -96,7 +104,7 @@ final class Streams {
/** /**
* Writes the JSON element to the writer, recursively. * Writes the JSON element to the writer, recursively.
*/ */
static void write(JsonElement element, boolean serializeNulls, JsonWriter writer) public static void write(JsonElement element, boolean serializeNulls, JsonWriter writer)
throws IOException { throws IOException {
if (element == null || element.isJsonNull()) { if (element == null || element.isJsonNull()) {
if (serializeNulls) { if (serializeNulls) {
@ -142,7 +150,7 @@ final class Streams {
} }
} }
static Writer writerForAppendable(Appendable appendable) { public static Writer writerForAppendable(Appendable appendable) {
return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable); return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable);
} }

View File

@ -16,15 +16,18 @@
package com.google.gson.internal.bind; package com.google.gson.internal.bind;
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.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import com.google.gson.JsonElement;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public abstract class TypeAdapter<T> { public abstract class TypeAdapter<T> {
public abstract T read(JsonReader reader) throws IOException; public abstract T read(JsonReader reader) throws IOException;
public abstract void write(JsonWriter writer, T value) throws IOException; public abstract void write(JsonWriter writer, T value) throws IOException;
@ -50,6 +53,34 @@ public abstract class TypeAdapter<T> {
return read(reader); return read(reader);
} }
public JsonElement toJsonElement(T src) {
try {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setLenient(true);
write(jsonWriter, src);
JsonReader reader = new JsonReader(new StringReader(stringWriter.toString()));
reader.setLenient(true);
return Streams.parse(reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public T fromJsonElement(JsonElement json) {
try {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setLenient(true);
Streams.write(json, false, jsonWriter);
JsonReader jsonReader = new JsonReader(new StringReader(stringWriter.toString()));
jsonReader.setLenient(true);
return read(jsonReader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public interface Factory { public interface Factory {
<T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type); <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type);
} }

View File

@ -17,6 +17,7 @@
package com.google.gson; package com.google.gson;
import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import java.io.CharArrayReader; import java.io.CharArrayReader;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;