Switched Short, URI, URL, UUID, StringBuilder and StringBuffer type adapters to new-style.

This commit is contained in:
Inderjeet Singh 2011-08-26 02:33:54 +00:00
parent 9b6954decd
commit f9976f4b01
3 changed files with 87 additions and 141 deletions

View File

@ -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<JsonSerializer<?>> map =
new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
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<JsonDeserializer<?>> createDefaultDeserializers() {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
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<URL>, JsonDeserializer<URL> {
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<URI>, JsonDeserializer<URI> {
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<UUID>, JsonDeserializer<UUID> {
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<Locale>, JsonDeserializer<Locale> {
public JsonElement serialize(Locale src, Type typeOfSrc, JsonSerializationContext context) {
@ -743,31 +662,6 @@ final class DefaultTypeAdapters {
}
}
private static final class ShortTypeAdapter
implements JsonSerializer<Short>, JsonDeserializer<Short> {
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<Byte>, JsonDeserializer<Byte> {
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<StringBuilder>, JsonDeserializer<StringBuilder> {
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<StringBuffer>, JsonDeserializer<StringBuffer> {
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<T> implements InstanceCreator<T> {
private final Class<? extends T> defaultInstance;

View File

@ -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)

View File

@ -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> SHORT = new TypeAdapter<Short>() {
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> LONG = new TypeAdapter<Long>() {
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<StringBuilder> STRING_BUILDER = new TypeAdapter<StringBuilder>() {
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<StringBuffer> STRING_BUFFER = new TypeAdapter<StringBuffer>() {
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> URL = new TypeAdapter<URL>() {
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> URI = new TypeAdapter<URI>() {
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> UUID = new TypeAdapter<UUID>() {
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<Object>() {
@Override public Object read(JsonReader reader) throws IOException {
reader.skipValue();