From f9976f4b0125ffb6482728448e56c917dd6b35a7 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 26 Aug 2011 02:33:54 +0000 Subject: [PATCH] Switched Short, URI, URL, UUID, StringBuilder and StringBuffer type adapters to new-style. --- .../com/google/gson/DefaultTypeAdapters.java | 140 ------------------ gson/src/main/java/com/google/gson/Gson.java | 6 + .../gson/internal/bind/TypeAdapters.java | 82 +++++++++- 3 files changed, 87 insertions(+), 141 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 62e7d16f..9f826e9a 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -19,10 +19,6 @@ package com.google.gson; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.InetAddress; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.net.UnknownHostException; import java.sql.Time; import java.sql.Timestamp; @@ -47,7 +43,6 @@ import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TimeZone; import java.util.TreeSet; -import java.util.UUID; import com.google.gson.internal.$Gson$Types; @@ -70,9 +65,6 @@ final class DefaultTypeAdapters { @SuppressWarnings("unchecked") private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter(); - private static final UrlTypeAdapter URL_TYPE_ADAPTER = new UrlTypeAdapter(); - private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter(); - private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter(); private static final LocaleTypeAdapter LOCALE_TYPE_ADAPTER = new LocaleTypeAdapter(); private static final BitSetTypeAdapter BIT_SET_ADAPTER = new BitSetTypeAdapter(); private static final DefaultInetAddressAdapter INET_ADDRESS_ADAPTER = @@ -84,11 +76,6 @@ final class DefaultTypeAdapters { private static final CharacterTypeAdapter CHARACTER_TYPE_ADAPTER = new CharacterTypeAdapter(); 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 StringBuilderTypeAdapter STRING_BUILDER_TYPE_ADAPTER = - new StringBuilderTypeAdapter(); - private static final StringBufferTypeAdapter STRING_BUFFER_TYPE_ADAPTER = - new StringBufferTypeAdapter(); private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = new GregorianCalendarTypeAdapter(); @@ -111,9 +98,6 @@ final class DefaultTypeAdapters { ParameterizedTypeHandlerMap> map = new ParameterizedTypeHandlerMap>(); - map.register(URL.class, URL_TYPE_ADAPTER, true); - map.register(URI.class, URI_TYPE_ADAPTER, true); - map.register(UUID.class, UUUID_TYPE_ADAPTER, true); map.register(Locale.class, LOCALE_TYPE_ADAPTER, true); map.register(Date.class, DATE_TYPE_ADAPTER, true); map.register(java.sql.Date.class, JAVA_SQL_DATE_TYPE_ADAPTER, true); @@ -128,10 +112,6 @@ final class DefaultTypeAdapters { map.register(byte.class, BYTE_TYPE_ADAPTER, true); map.register(Character.class, CHARACTER_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(StringBuilder.class, STRING_BUILDER_TYPE_ADAPTER, true); - map.register(StringBuffer.class, STRING_BUFFER_TYPE_ADAPTER, true); map.makeUnmodifiable(); return map; @@ -151,9 +131,6 @@ final class DefaultTypeAdapters { private static ParameterizedTypeHandlerMap> createDefaultDeserializers() { ParameterizedTypeHandlerMap> map = new ParameterizedTypeHandlerMap>(); - map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER), true); - map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER), true); - map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER), true); map.register(Locale.class, wrapDeserializer(LOCALE_TYPE_ADAPTER), true); map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER), true); map.register(java.sql.Date.class, wrapDeserializer(JAVA_SQL_DATE_TYPE_ADAPTER), true); @@ -170,10 +147,6 @@ final class DefaultTypeAdapters { 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(StringBuilder.class, wrapDeserializer(STRING_BUILDER_TYPE_ADAPTER), true); - map.register(StringBuffer.class, wrapDeserializer(STRING_BUFFER_TYPE_ADAPTER), true); map.makeUnmodifiable(); return map; @@ -538,60 +511,6 @@ final class DefaultTypeAdapters { } } - private static final class UrlTypeAdapter implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(URL src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.toExternalForm()); - } - - public URL deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return new URL(json.getAsString()); - } catch (MalformedURLException e) { - throw new JsonSyntaxException(e); - } - } - - @Override - public String toString() { - return UrlTypeAdapter.class.getSimpleName(); - } - } - - private static final class UriTypeAdapter implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(URI src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.toASCIIString()); - } - public URI deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return new URI(json.getAsString()); - } catch (URISyntaxException e) { - throw new JsonSyntaxException(e); - } - } - @Override - public String toString() { - return UriTypeAdapter.class.getSimpleName(); - } - } - - private static final class UuidTypeAdapter implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.toString()); - } - - public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return UUID.fromString(json.getAsString()); - } - - @Override - public String toString() { - return UuidTypeAdapter.class.getSimpleName(); - } - } - private static final class LocaleTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(Locale src, Type typeOfSrc, JsonSerializationContext context) { @@ -743,31 +662,6 @@ final class DefaultTypeAdapters { } } - private static final class ShortTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Short src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public Short deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsShort(); - } 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 ShortTypeAdapter.class.getSimpleName(); - } - } - private static final class ByteTypeAdapter implements JsonSerializer, JsonDeserializer { public JsonElement serialize(Byte src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); @@ -847,40 +741,6 @@ final class DefaultTypeAdapters { } } - private static final class StringBuilderTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(StringBuilder src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.toString()); - } - - public StringBuilder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return new StringBuilder(json.getAsString()); - } - - @Override - public String toString() { - return StringBuilderTypeAdapter.class.getSimpleName(); - } - } - - private static final class StringBufferTypeAdapter - implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(StringBuffer src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.toString()); - } - - public StringBuffer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return new StringBuffer(json.getAsString()); - } - - @Override - public String toString() { - return StringBufferTypeAdapter.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 90dcabbe..e0382b82 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -228,6 +228,7 @@ public final class Gson { MiniGson.Builder builder = new MiniGson.Builder() .withoutDefaultFactories() .factory(TypeAdapters.BOOLEAN_FACTORY) + .factory(TypeAdapters.SHORT_FACTORY) .factory(TypeAdapters.INTEGER_FACTORY) .factory(TypeAdapters.newFactory(double.class, Double.class, doubleAdapter(serializeSpecialFloatingPointValues))) @@ -236,6 +237,11 @@ public final class Gson { .factory(TypeAdapters.newFactory(long.class, Long.class, longAdapter(longSerializationPolicy))) .factory(TypeAdapters.STRING_FACTORY) + .factory(TypeAdapters.STRING_BUILDER_FACTORY) + .factory(TypeAdapters.STRING_BUFFER_FACTORY) + .factory(TypeAdapters.URL_FACTORY) + .factory(TypeAdapters.URI_FACTORY) + .factory(TypeAdapters.UUID_FACTORY) .typeAdapter(BigDecimal.class, new BigDecimalTypeAdapter()) .typeAdapter(BigInteger.class, new BigIntegerTypeAdapter()) .factory(excludedTypeFactory) 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 3518122a..d7b62ea3 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 @@ -16,10 +16,15 @@ package com.google.gson.internal.bind; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.UUID; + import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -import java.io.IOException; /** * Type adapters for basic types. @@ -51,6 +56,18 @@ public final class TypeAdapters { public static final TypeAdapter.Factory INTEGER_FACTORY = newFactory(int.class, Integer.class, INTEGER); + public static final TypeAdapter SHORT = new TypeAdapter() { + public Short read(JsonReader reader) throws IOException { + return (short) reader.nextInt(); + } + public void write(JsonWriter writer, Short value) throws IOException { + writer.value(value); + } + }; + + public static final TypeAdapter.Factory SHORT_FACTORY + = newFactory(short.class, Short.class, SHORT); + public static final TypeAdapter LONG = new TypeAdapter() { public Long read(JsonReader reader) throws IOException { return reader.nextLong(); @@ -98,6 +115,69 @@ public final class TypeAdapters { public static final TypeAdapter.Factory STRING_FACTORY = newFactory(String.class, STRING); + public static final TypeAdapter STRING_BUILDER = new TypeAdapter() { + public StringBuilder read(JsonReader reader) throws IOException { + return new StringBuilder(reader.nextString()); + } + public void write(JsonWriter writer, StringBuilder value) throws IOException { + writer.value(value.toString()); + } + }; + + public static final TypeAdapter.Factory STRING_BUILDER_FACTORY = + newFactory(StringBuilder.class, STRING_BUILDER); + + public static final TypeAdapter STRING_BUFFER = new TypeAdapter() { + public StringBuffer read(JsonReader reader) throws IOException { + return new StringBuffer(reader.nextString()); + } + public void write(JsonWriter writer, StringBuffer value) throws IOException { + writer.value(value.toString()); + } + }; + + public static final TypeAdapter.Factory STRING_BUFFER_FACTORY = + newFactory(StringBuffer.class, STRING_BUFFER); + + public static final TypeAdapter URL = new TypeAdapter() { + public URL read(JsonReader reader) throws IOException { + String nextString = reader.nextString(); + return "null".equals(nextString) ? null : new URL(nextString); + } + public void write(JsonWriter writer, URL value) throws IOException { + writer.value(value == null ? null : value.toExternalForm()); + } + }; + + public static final TypeAdapter.Factory URL_FACTORY = newFactory(URL.class, URL); + + public static final TypeAdapter URI = new TypeAdapter() { + public URI read(JsonReader reader) throws IOException { + try { + String nextString = reader.nextString(); + return "null".equals(nextString) ? null : new URI(nextString); + } catch (URISyntaxException e) { + throw new IOException(e); + } + } + public void write(JsonWriter writer, URI value) throws IOException { + writer.value(value == null ? null : value.toASCIIString()); + } + }; + + public static final TypeAdapter.Factory URI_FACTORY = newFactory(URI.class, URI); + + public static final TypeAdapter UUID = new TypeAdapter() { + public UUID read(JsonReader reader) throws IOException { + return java.util.UUID.fromString(reader.nextString()); + } + public void write(JsonWriter writer, UUID value) throws IOException { + writer.value(value.toString()); + } + }; + + public static final TypeAdapter.Factory UUID_FACTORY = newFactory(UUID.class, UUID); + public static final TypeAdapter EXCLUDED_TYPE_ADAPTER = new TypeAdapter() { @Override public Object read(JsonReader reader) throws IOException { reader.skipValue();