From 84c71409da812c1ccc62196ac2ff71f761bbf31b Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 19 Aug 2011 03:13:06 +0000 Subject: [PATCH] Removed Gson type adapters for boolean, integer, float, double, String, BigDecimal and BigInteger. Switched Gson.fromJson() methods to use miniGson directly instead of using a DOM. --- .../com/google/gson/DefaultTypeAdapters.java | 189 +----------------- gson/src/main/java/com/google/gson/Gson.java | 27 ++- 2 files changed, 24 insertions(+), 192 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 64673021..62e7d16f 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -16,12 +16,8 @@ package com.google.gson; -import com.google.gson.internal.$Gson$Types; - import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.math.BigInteger; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URI; @@ -53,6 +49,8 @@ import java.util.TimeZone; import java.util.TreeSet; import java.util.UUID; +import com.google.gson.internal.$Gson$Types; + /** * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, * and {@link InstanceCreator}s. @@ -81,19 +79,12 @@ final class DefaultTypeAdapters { new DefaultInetAddressAdapter(); private static final CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter(); private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); - private static final BigDecimalTypeAdapter BIG_DECIMAL_TYPE_ADAPTER = new BigDecimalTypeAdapter(); - private static final BigIntegerTypeAdapter BIG_INTEGER_TYPE_ADAPTER = new BigIntegerTypeAdapter(); - private static final BooleanTypeAdapter BOOLEAN_TYPE_ADAPTER = new BooleanTypeAdapter(); private static final ByteTypeAdapter BYTE_TYPE_ADAPTER = new ByteTypeAdapter(); private static final CharacterTypeAdapter CHARACTER_TYPE_ADAPTER = new CharacterTypeAdapter(); - private static final DoubleDeserializer DOUBLE_TYPE_ADAPTER = new DoubleDeserializer(); - private static final FloatDeserializer FLOAT_TYPE_ADAPTER = new FloatDeserializer(); - private static final IntegerTypeAdapter INTEGER_TYPE_ADAPTER = new IntegerTypeAdapter(); private static final LongDeserializer LONG_DESERIALIZER = new LongDeserializer(); private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter(); private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter(); - private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter(); private static final StringBuilderTypeAdapter STRING_BUILDER_TYPE_ADAPTER = new StringBuilderTypeAdapter(); private static final StringBufferTypeAdapter STRING_BUFFER_TYPE_ADAPTER = @@ -130,23 +121,15 @@ final class DefaultTypeAdapters { map.register(Time.class, TIME_TYPE_ADAPTER, true); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); - map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER, true); - map.register(BigInteger.class, BIG_INTEGER_TYPE_ADAPTER, true); map.register(BitSet.class, BIT_SET_ADAPTER, true); // Add primitive serializers - map.register(Boolean.class, BOOLEAN_TYPE_ADAPTER, true); - map.register(boolean.class, BOOLEAN_TYPE_ADAPTER, true); map.register(Byte.class, BYTE_TYPE_ADAPTER, true); map.register(byte.class, BYTE_TYPE_ADAPTER, true); map.register(Character.class, CHARACTER_TYPE_ADAPTER, true); - map.register(char.class, CHARACTER_TYPE_ADAPTER, true); - map.register(Integer.class, INTEGER_TYPE_ADAPTER, true); - map.register(int.class, INTEGER_TYPE_ADAPTER, true); map.register(Number.class, NUMBER_TYPE_ADAPTER, true); map.register(Short.class, SHORT_TYPE_ADAPTER, true); map.register(short.class, SHORT_TYPE_ADAPTER, true); - map.register(String.class, STRING_TYPE_ADAPTER, true); map.register(StringBuilder.class, STRING_BUILDER_TYPE_ADAPTER, true); map.register(StringBuffer.class, STRING_BUFFER_TYPE_ADAPTER, true); @@ -178,29 +161,17 @@ final class DefaultTypeAdapters { map.register(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER), true); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); - map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER, true); - map.register(BigInteger.class, BIG_INTEGER_TYPE_ADAPTER, true); map.register(BitSet.class, BIT_SET_ADAPTER, true); // Add primitive deserializers - map.register(Boolean.class, BOOLEAN_TYPE_ADAPTER, true); - map.register(boolean.class, BOOLEAN_TYPE_ADAPTER, true); map.register(Byte.class, BYTE_TYPE_ADAPTER, true); map.register(byte.class, BYTE_TYPE_ADAPTER, true); map.register(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true); - map.register(char.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true); - map.register(Double.class, DOUBLE_TYPE_ADAPTER, true); - map.register(double.class, DOUBLE_TYPE_ADAPTER, true); - map.register(Float.class, FLOAT_TYPE_ADAPTER, true); - map.register(float.class, FLOAT_TYPE_ADAPTER, true); - map.register(Integer.class, INTEGER_TYPE_ADAPTER, true); - map.register(int.class, INTEGER_TYPE_ADAPTER, true); map.register(Long.class, LONG_DESERIALIZER, true); map.register(long.class, LONG_DESERIALIZER, true); map.register(Number.class, NUMBER_TYPE_ADAPTER, true); map.register(Short.class, SHORT_TYPE_ADAPTER, true); map.register(short.class, SHORT_TYPE_ADAPTER, true); - map.register(String.class, wrapDeserializer(STRING_TYPE_ADAPTER), true); map.register(StringBuilder.class, wrapDeserializer(STRING_BUILDER_TYPE_ADAPTER), true); map.register(StringBuffer.class, wrapDeserializer(STRING_BUFFER_TYPE_ADAPTER), true); @@ -710,57 +681,6 @@ final class DefaultTypeAdapters { } } - private static final class BigDecimalTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(BigDecimal src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public BigDecimal deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - try { - return json.getAsBigDecimal(); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return BigDecimalTypeAdapter.class.getSimpleName(); - } - } - - private static final class BigIntegerTypeAdapter - implements JsonSerializer, JsonDeserializer { - - public JsonElement serialize(BigInteger src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public BigInteger deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - try { - return json.getAsBigInteger(); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return BigIntegerTypeAdapter.class.getSimpleName(); - } - } - private static final class NumberTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext context) { @@ -823,31 +743,6 @@ final class DefaultTypeAdapters { } } - private static final class IntegerTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsInt(); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return IntegerTypeAdapter.class.getSimpleName(); - } - } - private static final class ShortTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(Short src, Type typeOfSrc, JsonSerializationContext context) { @@ -916,26 +811,6 @@ final class DefaultTypeAdapters { } } - private static final class FloatDeserializer implements JsonDeserializer { - public Float deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsFloat(); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return FloatDeserializer.class.getSimpleName(); - } - } - static final class DoubleSerializer implements JsonSerializer { private final boolean serializeSpecialFloatingPointValues; @@ -955,26 +830,6 @@ final class DefaultTypeAdapters { } } - private static final class DoubleDeserializer implements JsonDeserializer { - public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsDouble(); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return DoubleDeserializer.class.getSimpleName(); - } - } - private static final class CharacterTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(Character src, Type typeOfSrc, JsonSerializationContext context) { @@ -992,23 +847,6 @@ final class DefaultTypeAdapters { } } - private static final class StringTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public String deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return json.getAsString(); - } - - @Override - public String toString() { - return StringTypeAdapter.class.getSimpleName(); - } - } - private static final class StringBuilderTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(StringBuilder src, Type typeOfSrc, JsonSerializationContext context) { @@ -1043,29 +881,6 @@ final class DefaultTypeAdapters { } } - private static final class BooleanTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Boolean src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsBoolean(); - } catch (UnsupportedOperationException e) { - throw new JsonSyntaxException(e); - } catch (IllegalStateException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return BooleanTypeAdapter.class.getSimpleName(); - } - } - @SuppressWarnings("unchecked") private static final class DefaultConstructorCreator implements InstanceCreator { private final Class defaultInstance; diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index f7437e3c..e1b2cb1b 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -544,9 +544,16 @@ public final class Gson { if (json == null) { return null; } - StringReader reader = new StringReader(json); - T target = (T) fromJson(reader, typeOfT); - return target; + try { + TypeAdapter typeAdapter = (TypeAdapter)miniGson.getAdapter(TypeToken.get(typeOfT)); + return typeAdapter.fromJson(json); + } catch (IllegalStateException e) { + // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException + throw new JsonSyntaxException(e); + } catch (IOException e) { + // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException + throw new JsonSyntaxException(e); + } } /** @@ -622,11 +629,21 @@ public final class Gson { */ @SuppressWarnings("unchecked") public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { + if (reader == null) { + // TODO(inder): remove this null check since we didnt have it in a previously released version + return null; + } boolean oldLenient = reader.isLenient(); reader.setLenient(true); try { - JsonElement root = Streams.parse(reader); - return (T) fromJson(root, typeOfT); + TypeAdapter typeAdapter = (TypeAdapter)miniGson.getAdapter(TypeToken.get(typeOfT)); + return typeAdapter.read(reader); + } catch (IllegalStateException e) { + // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException + throw new JsonSyntaxException(e); + } catch (IOException e) { + // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException + throw new JsonSyntaxException(e); } finally { reader.setLenient(oldLenient); }