Converted Number type adapter to the new style.

This commit is contained in:
Inderjeet Singh 2011-09-19 17:02:02 +00:00
parent 2780a2a9bf
commit 4402240294
3 changed files with 24 additions and 31 deletions

View File

@ -47,8 +47,6 @@ final class DefaultTypeAdapters {
private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER =
new DefaultTimestampDeserializer();
private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter();
private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER =
new GregorianCalendarTypeAdapter();
@ -73,8 +71,6 @@ final class DefaultTypeAdapters {
map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
map.makeUnmodifiable();
return map;
}
@ -89,8 +85,6 @@ final class DefaultTypeAdapters {
map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER, true);
map.register(Number.class, NUMBER_TYPE_ADAPTER, true);
map.makeUnmodifiable();
return map;
}
@ -299,29 +293,4 @@ final class DefaultTypeAdapters {
return GregorianCalendarTypeAdapter.class.getSimpleName();
}
}
private static final class NumberTypeAdapter
implements JsonSerializer<Number>, JsonDeserializer<Number> {
public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src);
}
public Number deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
return json.getAsNumber();
} 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 NumberTypeAdapter.class.getSimpleName();
}
}
}

View File

@ -236,6 +236,7 @@ public final class Gson {
floatAdapter(serializeSpecialFloatingPointValues)))
.factory(new ExcludedTypeAdapterFactory(
serializationExclusionStrategy, deserializationExclusionStrategy))
.factory(TypeAdapters.NUMBER_FACTORY)
.factory(TypeAdapters.CHARACTER_FACTORY)
.factory(TypeAdapters.STRING_FACTORY)
.factory(TypeAdapters.STRING_BUILDER_FACTORY)

View File

@ -28,6 +28,7 @@ import java.util.UUID;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.LazilyParsedNumber;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
@ -231,6 +232,28 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory DOUBLE_FACTORY
= newFactory(double.class, Double.class, DOUBLE);
public static final TypeAdapter<Number> NUMBER = new TypeAdapter<Number>() {
@Override
public Number read(JsonReader reader) throws IOException {
JsonToken jsonToken = reader.peek();
switch (jsonToken) {
case NULL:
reader.nextNull(); // TODO: does this belong here?
return null;
case NUMBER:
return new LazilyParsedNumber(reader.nextString());
default:
throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
}
}
@Override
public void write(JsonWriter writer, Number value) throws IOException {
writer.value(value);
}
};
public static final TypeAdapter.Factory NUMBER_FACTORY = newFactory(Number.class, NUMBER);
public static final TypeAdapter<Character> CHARACTER = new TypeAdapter<Character>() {
@Override
public Character read(JsonReader reader) throws IOException {