Converted InetAddress type adapter to new style.
This commit is contained in:
parent
0aab1d0659
commit
ea9c0236c7
@ -65,9 +65,7 @@ final class DefaultTypeAdapters {
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter();
|
||||
private static final BitSetTypeAdapter BIT_SET_ADAPTER = new BitSetTypeAdapter();
|
||||
private static final DefaultInetAddressAdapter INET_ADDRESS_ADAPTER =
|
||||
new DefaultInetAddressAdapter();
|
||||
private static final CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter();
|
||||
private static final CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter();
|
||||
private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter();
|
||||
|
||||
private static final ByteTypeAdapter BYTE_TYPE_ADAPTER = new ByteTypeAdapter();
|
||||
@ -118,7 +116,6 @@ final class DefaultTypeAdapters {
|
||||
ParameterizedTypeHandlerMap<JsonSerializer<?>> map =
|
||||
new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
|
||||
map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER, true);
|
||||
map.registerForTypeHierarchy(InetAddress.class, INET_ADDRESS_ADAPTER, true);
|
||||
map.registerForTypeHierarchy(Collection.class, COLLECTION_TYPE_ADAPTER, true);
|
||||
map.registerForTypeHierarchy(Map.class, MAP_TYPE_ADAPTER, true);
|
||||
map.makeUnmodifiable();
|
||||
@ -152,7 +149,6 @@ final class DefaultTypeAdapters {
|
||||
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
|
||||
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
|
||||
map.registerForTypeHierarchy(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER), true);
|
||||
map.registerForTypeHierarchy(InetAddress.class, wrapDeserializer(INET_ADDRESS_ADAPTER), true);
|
||||
map.registerForTypeHierarchy(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER), true);
|
||||
map.registerForTypeHierarchy(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER), true);
|
||||
map.makeUnmodifiable();
|
||||
@ -438,24 +434,6 @@ final class DefaultTypeAdapters {
|
||||
}
|
||||
}
|
||||
|
||||
static final class DefaultInetAddressAdapter
|
||||
implements JsonDeserializer<InetAddress>, JsonSerializer<InetAddress> {
|
||||
|
||||
public InetAddress deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
try {
|
||||
return InetAddress.getByName(json.getAsString());
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonElement serialize(InetAddress src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.getHostAddress());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final class EnumTypeAdapter<T extends Enum<T>>
|
||||
implements JsonSerializer<T>, JsonDeserializer<T> {
|
||||
|
@ -243,6 +243,7 @@ public final class Gson {
|
||||
.factory(TypeAdapters.URI_FACTORY)
|
||||
.factory(TypeAdapters.UUID_FACTORY)
|
||||
.factory(TypeAdapters.LOCALE_FACTORY)
|
||||
.factory(TypeAdapters.INET_ADDRESS_FACTORY)
|
||||
.typeAdapter(BigDecimal.class, new BigDecimalTypeAdapter())
|
||||
.typeAdapter(BigInteger.class, new BigIntegerTypeAdapter())
|
||||
.factory(excludedTypeFactory)
|
||||
|
@ -166,8 +166,8 @@ public final class MiniGson {
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T> Builder typeHierarchyAdapter(TypeToken<T> type, TypeAdapter<T> typeAdapter) {
|
||||
factories.add(TypeAdapters.newTypeHierarchyFactory(type, typeAdapter));
|
||||
public <T> Builder typeHierarchyAdapter(Class<T> clazz, TypeAdapter<T> typeAdapter) {
|
||||
factories.add(TypeAdapters.newTypeHierarchyFactory(clazz, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ import java.util.Map;
|
||||
public final class ObjectTypeAdapter extends TypeAdapter<Object> {
|
||||
public static final Factory FACTORY = new Factory() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type) {
|
||||
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type) {
|
||||
if (type.getRawType() == Object.class) {
|
||||
return (TypeAdapter<T>) new ObjectTypeAdapter(context);
|
||||
}
|
||||
|
@ -16,11 +16,8 @@
|
||||
|
||||
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;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
@ -28,6 +25,11 @@ 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.
|
||||
*/
|
||||
@ -173,6 +175,18 @@ public final class TypeAdapters {
|
||||
|
||||
public static final TypeAdapter.Factory URI_FACTORY = newFactory(URI.class, URI);
|
||||
|
||||
public static final TypeAdapter<InetAddress> INET_ADDRESS = new TypeAdapter<InetAddress>() {
|
||||
public InetAddress read(JsonReader reader) throws IOException {
|
||||
return InetAddress.getByName(reader.nextString());
|
||||
}
|
||||
public void write(JsonWriter writer, InetAddress value) throws IOException {
|
||||
writer.value(value.getHostAddress());
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory INET_ADDRESS_FACTORY =
|
||||
newTypeHierarchyFactory(InetAddress.class, INET_ADDRESS);
|
||||
|
||||
public static final TypeAdapter<UUID> UUID = new TypeAdapter<UUID>() {
|
||||
public UUID read(JsonReader reader) throws IOException {
|
||||
return java.util.UUID.fromString(reader.nextString());
|
||||
@ -257,11 +271,10 @@ public final class TypeAdapters {
|
||||
}
|
||||
|
||||
public static <T> TypeAdapter.Factory newTypeHierarchyFactory(
|
||||
TypeToken<T> type, TypeAdapter<T> typeAdapter) {
|
||||
final Class<T> clazz, final TypeAdapter<T> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) {
|
||||
// TODO: use Inder's TypeHierarchyAdapter here
|
||||
throw new UnsupportedOperationException();
|
||||
return clazz.isAssignableFrom(typeToken.getRawType()) ? (TypeAdapter<T>) typeAdapter : null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user