Switched Locale type adapter to new-style.

This commit is contained in:
Inderjeet Singh 2011-08-26 03:14:01 +00:00
parent f9976f4b01
commit 9fb39c89ea
3 changed files with 34 additions and 41 deletions

View File

@ -40,7 +40,6 @@ import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.TreeSet;
@ -65,7 +64,6 @@ final class DefaultTypeAdapters {
@SuppressWarnings("unchecked")
private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter();
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 =
new DefaultInetAddressAdapter();
@ -98,7 +96,6 @@ final class DefaultTypeAdapters {
ParameterizedTypeHandlerMap<JsonSerializer<?>> map =
new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
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);
map.register(Timestamp.class, DATE_TYPE_ADAPTER, true);
@ -131,7 +128,6 @@ final class DefaultTypeAdapters {
private static ParameterizedTypeHandlerMap<JsonDeserializer<?>> createDefaultDeserializers() {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
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);
map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER), true);
@ -511,43 +507,6 @@ final class DefaultTypeAdapters {
}
}
private static final class LocaleTypeAdapter
implements JsonSerializer<Locale>, JsonDeserializer<Locale> {
public JsonElement serialize(Locale src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
public Locale deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String locale = json.getAsString();
StringTokenizer tokenizer = new StringTokenizer(locale, "_");
String language = null;
String country = null;
String variant = null;
if (tokenizer.hasMoreElements()) {
language = tokenizer.nextToken();
}
if (tokenizer.hasMoreElements()) {
country = tokenizer.nextToken();
}
if (tokenizer.hasMoreElements()) {
variant = tokenizer.nextToken();
}
if (country == null && variant == null) {
return new Locale(language);
} else if (variant == null) {
return new Locale(language, country);
} else {
return new Locale(language, country, variant);
}
}
@Override
public String toString() {
return LocaleTypeAdapter.class.getSimpleName();
}
}
@SuppressWarnings("unchecked")
private static final class CollectionTypeAdapter implements JsonSerializer<Collection>,
JsonDeserializer<Collection> {

View File

@ -242,6 +242,7 @@ public final class Gson {
.factory(TypeAdapters.URL_FACTORY)
.factory(TypeAdapters.URI_FACTORY)
.factory(TypeAdapters.UUID_FACTORY)
.factory(TypeAdapters.LOCALE_FACTORY)
.typeAdapter(BigDecimal.class, new BigDecimalTypeAdapter())
.typeAdapter(BigInteger.class, new BigIntegerTypeAdapter())
.factory(excludedTypeFactory)

View File

@ -20,6 +20,8 @@ import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.UUID;
import com.google.gson.reflect.TypeToken;
@ -178,6 +180,37 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory UUID_FACTORY = newFactory(UUID.class, UUID);
public static final TypeAdapter<Locale> LOCALE = new TypeAdapter<Locale>() {
public Locale read(JsonReader reader) throws IOException {
String locale = reader.nextString();
StringTokenizer tokenizer = new StringTokenizer(locale, "_");
String language = null;
String country = null;
String variant = null;
if (tokenizer.hasMoreElements()) {
language = tokenizer.nextToken();
}
if (tokenizer.hasMoreElements()) {
country = tokenizer.nextToken();
}
if (tokenizer.hasMoreElements()) {
variant = tokenizer.nextToken();
}
if (country == null && variant == null) {
return new Locale(language);
} else if (variant == null) {
return new Locale(language, country);
} else {
return new Locale(language, country, variant);
}
}
public void write(JsonWriter writer, Locale value) throws IOException {
writer.value(value.toString());
}
};
public static final TypeAdapter.Factory LOCALE_FACTORY = newFactory(Locale.class, LOCALE);
public static final TypeAdapter EXCLUDED_TYPE_ADAPTER = new TypeAdapter<Object>() {
@Override public Object read(JsonReader reader) throws IOException {
reader.skipValue();