Adding default type adapter for InetAddress.

This commit is contained in:
Joel Leitch 2011-01-10 23:03:48 +00:00
parent 3926afbd30
commit 1e2fbd81f5
2 changed files with 70 additions and 1 deletions

View File

@ -20,10 +20,12 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
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;
@ -68,6 +70,8 @@ final class DefaultTypeAdapters {
private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter(); private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter();
private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter(); private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter();
private static final LocaleTypeAdapter LOCALE_TYPE_ADAPTER = new LocaleTypeAdapter(); private static final LocaleTypeAdapter LOCALE_TYPE_ADAPTER = new LocaleTypeAdapter();
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 MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter();
private static final BigDecimalTypeAdapter BIG_DECIMAL_TYPE_ADAPTER = new BigDecimalTypeAdapter(); private static final BigDecimalTypeAdapter BIG_DECIMAL_TYPE_ADAPTER = new BigDecimalTypeAdapter();
@ -88,7 +92,7 @@ final class DefaultTypeAdapters {
private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator(); private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator();
private static final HashSetCreator HASH_SET_CREATOR = new HashSetCreator(); private static final HashSetCreator HASH_SET_CREATOR = new HashSetCreator();
private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER =
new GregorianCalendarTypeAdapter(); new GregorianCalendarTypeAdapter();
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS // The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
// must be defined after the constants for the type adapters. Otherwise, the type adapter // must be defined after the constants for the type adapters. Otherwise, the type adapter
@ -105,6 +109,7 @@ final class DefaultTypeAdapters {
new ParameterizedTypeHandlerMap<JsonSerializer<?>>(); new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER); map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER);
map.registerForTypeHierarchy(InetAddress.class, INET_ADDRESS_ADAPTER);
map.register(URL.class, URL_TYPE_ADAPTER); map.register(URL.class, URL_TYPE_ADAPTER);
map.register(URI.class, URI_TYPE_ADAPTER); map.register(URI.class, URI_TYPE_ADAPTER);
map.register(UUID.class, UUUID_TYPE_ADAPTER); map.register(UUID.class, UUUID_TYPE_ADAPTER);
@ -142,6 +147,7 @@ final class DefaultTypeAdapters {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map = ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>(); new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
map.registerForTypeHierarchy(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER)); map.registerForTypeHierarchy(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER));
map.registerForTypeHierarchy(InetAddress.class, wrapDeserializer(INET_ADDRESS_ADAPTER));
map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER)); map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER));
map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER)); map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER));
map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER)); map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER));
@ -397,6 +403,24 @@ final class DefaultTypeAdapters {
} }
} }
static 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") @SuppressWarnings("unchecked")
private static class EnumTypeAdapter<T extends Enum<T>> private static class EnumTypeAdapter<T extends Enum<T>>
implements JsonSerializer<T>, JsonDeserializer<T> { implements JsonSerializer<T>, JsonDeserializer<T> {

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson;
import java.net.InetAddress;
import junit.framework.TestCase;
/**
* Unit tests for the default serializer/deserializer for the {@code InetAddress} type.
*
* @author Joel Leitch
*/
public class DefaultInetAddressTypeAdapterTest extends TestCase {
private Gson gson;
@Override
protected void setUp() throws Exception {
super.setUp();
gson = new Gson();
}
public void testInetAddressSerializationAndDeserialization() throws Exception {
InetAddress localhost = InetAddress.getLocalHost();
String localInetAddress = gson.toJson(localhost);
assertEquals("\"" + localhost.getHostAddress() + "\"", localInetAddress);
InetAddress value = gson.fromJson(localInetAddress, InetAddress.class);
assertEquals(localhost, value);
}
}