Adapt bytes

This commit is contained in:
Jesse Wilson 2011-09-09 04:39:29 +00:00
parent a8133efeb8
commit 9db0c53217
4 changed files with 68 additions and 81 deletions

View File

@ -16,10 +16,8 @@
package com.google.gson; package com.google.gson;
import java.lang.reflect.ParameterizedType; import com.google.gson.internal.$Gson$Types;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
@ -43,8 +41,6 @@ import java.util.SortedSet;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.TreeSet; import java.util.TreeSet;
import com.google.gson.internal.$Gson$Types;
/** /**
* List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s,
* and {@link InstanceCreator}s. * and {@link InstanceCreator}s.
@ -67,7 +63,6 @@ final class DefaultTypeAdapters {
private static final BitSetTypeAdapter BIT_SET_ADAPTER = new BitSetTypeAdapter(); private static final BitSetTypeAdapter BIT_SET_ADAPTER = new BitSetTypeAdapter();
private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); 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 CharacterTypeAdapter CHARACTER_TYPE_ADAPTER = new CharacterTypeAdapter();
private static final LongDeserializer LONG_DESERIALIZER = new LongDeserializer(); private static final LongDeserializer LONG_DESERIALIZER = new LongDeserializer();
private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter(); private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter();
@ -102,8 +97,6 @@ final class DefaultTypeAdapters {
map.register(BitSet.class, BIT_SET_ADAPTER, true); map.register(BitSet.class, BIT_SET_ADAPTER, true);
// Add primitive serializers // 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(Character.class, CHARACTER_TYPE_ADAPTER, true);
map.register(Number.class, NUMBER_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); map.register(BitSet.class, BIT_SET_ADAPTER, true);
// Add primitive deserializers // 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(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER), true);
map.register(Long.class, LONG_DESERIALIZER, true); map.register(Long.class, LONG_DESERIALIZER, 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<Byte>, JsonDeserializer<Byte> {
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<Float> { static final class FloatSerializer implements JsonSerializer<Float> {
private final boolean serializeSpecialFloatingPointValues; private final boolean serializeSpecialFloatingPointValues;

View File

@ -228,14 +228,15 @@ public final class Gson {
MiniGson.Builder builder = new MiniGson.Builder() MiniGson.Builder builder = new MiniGson.Builder()
.withoutDefaultFactories() .withoutDefaultFactories()
.factory(TypeAdapters.BOOLEAN_FACTORY) .factory(TypeAdapters.BOOLEAN_FACTORY)
.factory(TypeAdapters.BYTE_FACTORY)
.factory(TypeAdapters.SHORT_FACTORY) .factory(TypeAdapters.SHORT_FACTORY)
.factory(TypeAdapters.INTEGER_FACTORY) .factory(TypeAdapters.INTEGER_FACTORY)
.factory(TypeAdapters.newFactory(long.class, Long.class,
longAdapter(longSerializationPolicy)))
.factory(TypeAdapters.newFactory(double.class, Double.class, .factory(TypeAdapters.newFactory(double.class, Double.class,
doubleAdapter(serializeSpecialFloatingPointValues))) doubleAdapter(serializeSpecialFloatingPointValues)))
.factory(TypeAdapters.newFactory(float.class, Float.class, .factory(TypeAdapters.newFactory(float.class, Float.class,
floatAdapter(serializeSpecialFloatingPointValues))) floatAdapter(serializeSpecialFloatingPointValues)))
.factory(TypeAdapters.newFactory(long.class, Long.class,
longAdapter(longSerializationPolicy)))
.factory(TypeAdapters.STRING_FACTORY) .factory(TypeAdapters.STRING_FACTORY)
.factory(TypeAdapters.STRING_BUILDER_FACTORY) .factory(TypeAdapters.STRING_BUILDER_FACTORY)
.factory(TypeAdapters.STRING_BUFFER_FACTORY) .factory(TypeAdapters.STRING_BUFFER_FACTORY)
@ -257,31 +258,33 @@ public final class Gson {
this.miniGson = builder.build(); this.miniGson = builder.build();
} }
private TypeAdapter<Double> doubleAdapter(boolean serializeSpecialFloatingPointValues) { private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
if (serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) {
return TypeAdapters.DOUBLE; return TypeAdapters.DOUBLE;
} }
return new TypeAdapter<Double>() { return new TypeAdapter<Number>() {
@Override public Double read(JsonReader reader) throws IOException { @Override public Double read(JsonReader reader) throws IOException {
return reader.nextDouble(); return reader.nextDouble();
} }
@Override public void write(JsonWriter writer, Double value) throws IOException { @Override public void write(JsonWriter writer, Number value) throws IOException {
checkValidFloatingPoint(value); double doubleValue = value.doubleValue();
checkValidFloatingPoint(doubleValue);
writer.value(value); writer.value(value);
} }
}; };
} }
private TypeAdapter<Float> floatAdapter(boolean serializeSpecialFloatingPointValues) { private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointValues) {
if (serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) {
return TypeAdapters.FLOAT; return TypeAdapters.FLOAT;
} }
return new TypeAdapter<Float>() { return new TypeAdapter<Number>() {
@Override public Float read(JsonReader reader) throws IOException { @Override public Float read(JsonReader reader) throws IOException {
return (float) reader.nextDouble(); return (float) reader.nextDouble();
} }
@Override public void write(JsonWriter writer, Float value) throws IOException { @Override public void write(JsonWriter writer, Number value) throws IOException {
checkValidFloatingPoint(value); float floatValue = value.floatValue();
checkValidFloatingPoint(floatValue);
writer.value(value); writer.value(value);
} }
}; };
@ -295,15 +298,15 @@ public final class Gson {
} }
} }
private TypeAdapter<Long> longAdapter(LongSerializationPolicy longSerializationPolicy) { private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) { if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
return TypeAdapters.LONG; return TypeAdapters.LONG;
} }
return new TypeAdapter<Long>() { return new TypeAdapter<Number>() {
@Override public Long read(JsonReader reader) throws IOException { @Override public Number read(JsonReader reader) throws IOException {
return reader.nextLong(); 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()); writer.value(value.toString());
} }
}; };

View File

@ -16,6 +16,10 @@
package com.google.gson.internal.bind; 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.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URI; import java.net.URI;
@ -25,11 +29,6 @@ import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.UUID; 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. * Type adapters for basic types.
*/ */
@ -48,23 +47,24 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory BOOLEAN_FACTORY public static final TypeAdapter.Factory BOOLEAN_FACTORY
= newFactory(boolean.class, Boolean.class, BOOLEAN); = newFactory(boolean.class, Boolean.class, BOOLEAN);
public static final TypeAdapter<Integer> INTEGER = new TypeAdapter<Integer>() { public static final TypeAdapter<Number> BYTE = new TypeAdapter<Number>() {
public Integer read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
return reader.nextInt(); 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); writer.value(value);
} }
}; };
public static final TypeAdapter.Factory INTEGER_FACTORY public static final TypeAdapter.Factory BYTE_FACTORY
= newFactory(int.class, Integer.class, INTEGER); = newFactory(byte.class, Byte.class, BYTE);
public static final TypeAdapter<Short> SHORT = new TypeAdapter<Short>() { public static final TypeAdapter<Number> SHORT = new TypeAdapter<Number>() {
public Short read(JsonReader reader) throws IOException { public Number read(JsonReader reader) throws IOException {
return (short) reader.nextInt(); 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); writer.value(value);
} }
}; };
@ -72,11 +72,23 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory SHORT_FACTORY public static final TypeAdapter.Factory SHORT_FACTORY
= newFactory(short.class, Short.class, SHORT); = newFactory(short.class, Short.class, SHORT);
public static final TypeAdapter<Long> LONG = new TypeAdapter<Long>() { public static final TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() {
public Long read(JsonReader reader) throws IOException { 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<Number> LONG = new TypeAdapter<Number>() {
public Number read(JsonReader reader) throws IOException {
return reader.nextLong(); return reader.nextLong();
} }
public void write(JsonWriter writer, Long value) throws IOException { public void write(JsonWriter writer, Number value) throws IOException {
writer.value(value); writer.value(value);
} }
}; };
@ -84,23 +96,11 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory LONG_FACTORY public static final TypeAdapter.Factory LONG_FACTORY
= newFactory(long.class, Long.class, LONG); = newFactory(long.class, Long.class, LONG);
public static final TypeAdapter<Double> DOUBLE = new TypeAdapter<Double>() { public static final TypeAdapter<Number> FLOAT = new TypeAdapter<Number>() {
public Double read(JsonReader reader) throws IOException { public Number 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> FLOAT = new TypeAdapter<Float>() {
public Float read(JsonReader reader) throws IOException {
return (float) reader.nextDouble(); 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); writer.value(value);
} }
}; };
@ -108,6 +108,18 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory FLOAT_FACTORY public static final TypeAdapter.Factory FLOAT_FACTORY
= newFactory(float.class, Float.class, FLOAT); = newFactory(float.class, Float.class, FLOAT);
public static final TypeAdapter<Number> DOUBLE = new TypeAdapter<Number>() {
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> STRING = new TypeAdapter<String>() { public static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
public String read(JsonReader reader) throws IOException { public String read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) { if (reader.peek() == JsonToken.NULL) {
@ -260,7 +272,7 @@ public final class TypeAdapters {
} }
public static <T> TypeAdapter.Factory newFactory( public static <T> TypeAdapter.Factory newFactory(
final Class<T> unboxed, final Class<T> boxed, final TypeAdapter<T> typeAdapter) { final Class<T> unboxed, final Class<T> boxed, final TypeAdapter<? super T> typeAdapter) {
return new TypeAdapter.Factory() { return new TypeAdapter.Factory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) { public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) {

View File

@ -67,6 +67,11 @@ public class PrimitiveTest extends TestCase {
assertEquals("1", gson.toJson(1, Byte.class)); 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() { public void testByteDeserialization() {
Byte target = gson.fromJson("1", Byte.class); Byte target = gson.fromJson("1", Byte.class);
assertEquals(1, (byte)target); assertEquals(1, (byte)target);