Adapt bytes
This commit is contained in:
parent
a8133efeb8
commit
9db0c53217
@ -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<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> {
|
||||
private final boolean serializeSpecialFloatingPointValues;
|
||||
|
||||
|
@ -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<Double> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
if (serializeSpecialFloatingPointValues) {
|
||||
return TypeAdapters.DOUBLE;
|
||||
}
|
||||
return new TypeAdapter<Double>() {
|
||||
return new TypeAdapter<Number>() {
|
||||
@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<Float> floatAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
if (serializeSpecialFloatingPointValues) {
|
||||
return TypeAdapters.FLOAT;
|
||||
}
|
||||
return new TypeAdapter<Float>() {
|
||||
return new TypeAdapter<Number>() {
|
||||
@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<Long> longAdapter(LongSerializationPolicy longSerializationPolicy) {
|
||||
private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
|
||||
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
|
||||
return TypeAdapters.LONG;
|
||||
}
|
||||
return new TypeAdapter<Long>() {
|
||||
@Override public Long read(JsonReader reader) throws IOException {
|
||||
return new TypeAdapter<Number>() {
|
||||
@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());
|
||||
}
|
||||
};
|
||||
|
@ -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> INTEGER = new TypeAdapter<Integer>() {
|
||||
public Integer read(JsonReader reader) throws IOException {
|
||||
return reader.nextInt();
|
||||
public static final TypeAdapter<Number> BYTE = new TypeAdapter<Number>() {
|
||||
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> SHORT = new TypeAdapter<Short>() {
|
||||
public Short read(JsonReader reader) throws IOException {
|
||||
public static final TypeAdapter<Number> SHORT = new TypeAdapter<Number>() {
|
||||
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> LONG = new TypeAdapter<Long>() {
|
||||
public Long read(JsonReader reader) throws IOException {
|
||||
public static final TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() {
|
||||
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();
|
||||
}
|
||||
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> DOUBLE = new TypeAdapter<Double>() {
|
||||
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> FLOAT = new TypeAdapter<Float>() {
|
||||
public Float read(JsonReader reader) throws IOException {
|
||||
public static final TypeAdapter<Number> FLOAT = new TypeAdapter<Number>() {
|
||||
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<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 String read(JsonReader reader) throws IOException {
|
||||
if (reader.peek() == JsonToken.NULL) {
|
||||
@ -260,7 +272,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
|
||||
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() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user