diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 03ae677b..b307ff5f 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -16,10 +16,8 @@ package com.google.gson; -import java.lang.reflect.ParameterizedType; +import com.google.gson.internal.$Gson$Types; import java.lang.reflect.Type; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; @@ -43,8 +41,6 @@ import java.util.SortedSet; import java.util.TimeZone; import java.util.TreeSet; -import com.google.gson.internal.$Gson$Types; - /** * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, * and {@link InstanceCreator}s. @@ -67,7 +63,6 @@ final class DefaultTypeAdapters { private static final BitSetTypeAdapter BIT_SET_ADAPTER = new BitSetTypeAdapter(); private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); - private static final ByteTypeAdapter BYTE_TYPE_ADAPTER = new ByteTypeAdapter(); 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(); @@ -102,8 +97,6 @@ final class DefaultTypeAdapters { map.register(BitSet.class, BIT_SET_ADAPTER, true); // Add primitive serializers - 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(Number.class, NUMBER_TYPE_ADAPTER, true); @@ -132,8 +125,6 @@ final class DefaultTypeAdapters { map.register(BitSet.class, BIT_SET_ADAPTER, true); // Add primitive deserializers - 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(Long.class, LONG_DESERIALIZER, true); map.register(long.class, LONG_DESERIALIZER, true); @@ -544,30 +535,6 @@ final class DefaultTypeAdapters { } } - private static final class ByteTypeAdapter implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Byte src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src); - } - - public Byte deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - try { - return json.getAsByte(); - } 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 ByteTypeAdapter.class.getSimpleName(); - } - } - static final class FloatSerializer implements JsonSerializer { private final boolean serializeSpecialFloatingPointValues; diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index a00cde00..b02db3fb 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -228,14 +228,15 @@ public final class Gson { MiniGson.Builder builder = new MiniGson.Builder() .withoutDefaultFactories() .factory(TypeAdapters.BOOLEAN_FACTORY) + .factory(TypeAdapters.BYTE_FACTORY) .factory(TypeAdapters.SHORT_FACTORY) .factory(TypeAdapters.INTEGER_FACTORY) + .factory(TypeAdapters.newFactory(long.class, Long.class, + longAdapter(longSerializationPolicy))) .factory(TypeAdapters.newFactory(double.class, Double.class, doubleAdapter(serializeSpecialFloatingPointValues))) .factory(TypeAdapters.newFactory(float.class, Float.class, floatAdapter(serializeSpecialFloatingPointValues))) - .factory(TypeAdapters.newFactory(long.class, Long.class, - longAdapter(longSerializationPolicy))) .factory(TypeAdapters.STRING_FACTORY) .factory(TypeAdapters.STRING_BUILDER_FACTORY) .factory(TypeAdapters.STRING_BUFFER_FACTORY) @@ -257,31 +258,33 @@ public final class Gson { this.miniGson = builder.build(); } - private TypeAdapter doubleAdapter(boolean serializeSpecialFloatingPointValues) { + private TypeAdapter doubleAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.DOUBLE; } - return new TypeAdapter() { + return new TypeAdapter() { @Override public Double read(JsonReader reader) throws IOException { return reader.nextDouble(); } - @Override public void write(JsonWriter writer, Double value) throws IOException { - checkValidFloatingPoint(value); + @Override public void write(JsonWriter writer, Number value) throws IOException { + double doubleValue = value.doubleValue(); + checkValidFloatingPoint(doubleValue); writer.value(value); } }; } - private TypeAdapter floatAdapter(boolean serializeSpecialFloatingPointValues) { + private TypeAdapter floatAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.FLOAT; } - return new TypeAdapter() { + return new TypeAdapter() { @Override public Float read(JsonReader reader) throws IOException { return (float) reader.nextDouble(); } - @Override public void write(JsonWriter writer, Float value) throws IOException { - checkValidFloatingPoint(value); + @Override public void write(JsonWriter writer, Number value) throws IOException { + float floatValue = value.floatValue(); + checkValidFloatingPoint(floatValue); writer.value(value); } }; @@ -295,15 +298,15 @@ public final class Gson { } } - private TypeAdapter longAdapter(LongSerializationPolicy longSerializationPolicy) { + private TypeAdapter longAdapter(LongSerializationPolicy longSerializationPolicy) { if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) { return TypeAdapters.LONG; } - return new TypeAdapter() { - @Override public Long read(JsonReader reader) throws IOException { + return new TypeAdapter() { + @Override public Number read(JsonReader reader) throws IOException { return reader.nextLong(); } - @Override public void write(JsonWriter writer, Long value) throws IOException { + @Override public void write(JsonWriter writer, Number value) throws IOException { writer.value(value.toString()); } }; 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 ea4a5f9b..1aa46d75 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,6 +16,10 @@ package com.google.gson.internal.bind; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.net.InetAddress; import java.net.URI; @@ -25,11 +29,6 @@ import java.util.Locale; import java.util.StringTokenizer; import java.util.UUID; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; - /** * Type adapters for basic types. */ @@ -48,23 +47,24 @@ public final class TypeAdapters { public static final TypeAdapter.Factory BOOLEAN_FACTORY = newFactory(boolean.class, Boolean.class, BOOLEAN); - public static final TypeAdapter INTEGER = new TypeAdapter() { - public Integer read(JsonReader reader) throws IOException { - return reader.nextInt(); + public static final TypeAdapter BYTE = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { + int intValue = reader.nextInt(); + return (byte) intValue; } - public void write(JsonWriter writer, Integer value) throws IOException { + public void write(JsonWriter writer, Number value) throws IOException { writer.value(value); } }; - public static final TypeAdapter.Factory INTEGER_FACTORY - = newFactory(int.class, Integer.class, INTEGER); + public static final TypeAdapter.Factory BYTE_FACTORY + = newFactory(byte.class, Byte.class, BYTE); - public static final TypeAdapter SHORT = new TypeAdapter() { - public Short read(JsonReader reader) throws IOException { + public static final TypeAdapter SHORT = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { return (short) reader.nextInt(); } - public void write(JsonWriter writer, Short value) throws IOException { + public void write(JsonWriter writer, Number value) throws IOException { writer.value(value); } }; @@ -72,11 +72,23 @@ public final class TypeAdapters { 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 { + public static final TypeAdapter INTEGER = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { + return reader.nextInt(); + } + public void write(JsonWriter writer, Number value) throws IOException { + writer.value(value); + } + }; + + public static final TypeAdapter.Factory INTEGER_FACTORY + = newFactory(int.class, Integer.class, INTEGER); + + public static final TypeAdapter LONG = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { return reader.nextLong(); } - public void write(JsonWriter writer, Long value) throws IOException { + public void write(JsonWriter writer, Number value) throws IOException { writer.value(value); } }; @@ -84,23 +96,11 @@ public final class TypeAdapters { public static final TypeAdapter.Factory LONG_FACTORY = newFactory(long.class, Long.class, LONG); - public static final TypeAdapter DOUBLE = new TypeAdapter() { - public Double read(JsonReader reader) throws IOException { - return reader.nextDouble(); - } - public void write(JsonWriter writer, Double value) throws IOException { - writer.value(value); - } - }; - - public static final TypeAdapter.Factory DOUBLE_FACTORY - = newFactory(double.class, Double.class, DOUBLE); - - public static final TypeAdapter FLOAT = new TypeAdapter() { - public Float read(JsonReader reader) throws IOException { + public static final TypeAdapter FLOAT = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { return (float) reader.nextDouble(); } - public void write(JsonWriter writer, Float value) throws IOException { + public void write(JsonWriter writer, Number value) throws IOException { writer.value(value); } }; @@ -108,6 +108,18 @@ public final class TypeAdapters { public static final TypeAdapter.Factory FLOAT_FACTORY = newFactory(float.class, Float.class, FLOAT); + public static final TypeAdapter DOUBLE = new TypeAdapter() { + public Number read(JsonReader reader) throws IOException { + return reader.nextDouble(); + } + public void write(JsonWriter writer, Number value) throws IOException { + writer.value(value); + } + }; + + public static final TypeAdapter.Factory DOUBLE_FACTORY + = newFactory(double.class, Double.class, DOUBLE); + public static final TypeAdapter STRING = new TypeAdapter() { public String read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { @@ -260,7 +272,7 @@ public final class TypeAdapters { } public static TypeAdapter.Factory newFactory( - final Class unboxed, final Class boxed, final TypeAdapter typeAdapter) { + final Class unboxed, final Class boxed, final TypeAdapter typeAdapter) { return new TypeAdapter.Factory() { @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal public TypeAdapter create(MiniGson context, TypeToken typeToken) { diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index 93b9ae04..2695a7d3 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -67,6 +67,11 @@ public class PrimitiveTest extends TestCase { assertEquals("1", gson.toJson(1, Byte.class)); } + public void testShortSerialization() { + assertEquals("1", gson.toJson(1, short.class)); + assertEquals("1", gson.toJson(1, Short.class)); + } + public void testByteDeserialization() { Byte target = gson.fromJson("1", Byte.class); assertEquals(1, (byte)target);