From 4402240294300515e8f4141adba42732395b5c21 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Mon, 19 Sep 2011 17:02:02 +0000 Subject: [PATCH] Converted Number type adapter to the new style. --- .../com/google/gson/DefaultTypeAdapters.java | 31 ------------------- gson/src/main/java/com/google/gson/Gson.java | 1 + .../gson/internal/bind/TypeAdapters.java | 23 ++++++++++++++ 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index c2a26da4..0b21e161 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -47,8 +47,6 @@ final class DefaultTypeAdapters { private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER = new DefaultTimestampDeserializer(); - private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter(); - private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = new GregorianCalendarTypeAdapter(); @@ -73,8 +71,6 @@ final class DefaultTypeAdapters { map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); - map.register(Number.class, NUMBER_TYPE_ADAPTER, true); - map.makeUnmodifiable(); return map; } @@ -89,8 +85,6 @@ final class DefaultTypeAdapters { map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true); - map.register(Number.class, NUMBER_TYPE_ADAPTER, true); - map.makeUnmodifiable(); return map; } @@ -299,29 +293,4 @@ final class DefaultTypeAdapters { return GregorianCalendarTypeAdapter.class.getSimpleName(); } } - - private static final class NumberTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public Number deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsNumber(); - } 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 NumberTypeAdapter.class.getSimpleName(); - } - } } diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index dc30e30d..1962105f 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -236,6 +236,7 @@ public final class Gson { floatAdapter(serializeSpecialFloatingPointValues))) .factory(new ExcludedTypeAdapterFactory( serializationExclusionStrategy, deserializationExclusionStrategy)) + .factory(TypeAdapters.NUMBER_FACTORY) .factory(TypeAdapters.CHARACTER_FACTORY) .factory(TypeAdapters.STRING_FACTORY) .factory(TypeAdapters.STRING_BUILDER_FACTORY) diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index a883d767..40f03c13 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -28,6 +28,7 @@ import java.util.UUID; import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; +import com.google.gson.internal.LazilyParsedNumber; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -231,6 +232,28 @@ public final class TypeAdapters { public static final TypeAdapter.Factory DOUBLE_FACTORY = newFactory(double.class, Double.class, DOUBLE); + public static final TypeAdapter NUMBER = new TypeAdapter() { + @Override + public Number read(JsonReader reader) throws IOException { + JsonToken jsonToken = reader.peek(); + switch (jsonToken) { + case NULL: + reader.nextNull(); // TODO: does this belong here? + return null; + case NUMBER: + return new LazilyParsedNumber(reader.nextString()); + default: + throw new JsonSyntaxException("Expecting number, got: " + jsonToken); + } + } + @Override + public void write(JsonWriter writer, Number value) throws IOException { + writer.value(value); + } + }; + + public static final TypeAdapter.Factory NUMBER_FACTORY = newFactory(Number.class, NUMBER); + public static final TypeAdapter CHARACTER = new TypeAdapter() { @Override public Character read(JsonReader reader) throws IOException {