From 1e2fbd81f500671670885b7fc110d3c6a160aa78 Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Mon, 10 Jan 2011 23:03:48 +0000 Subject: [PATCH] Adding default type adapter for InetAddress. --- .../com/google/gson/DefaultTypeAdapters.java | 26 ++++++++++- .../DefaultInetAddressTypeAdapterTest.java | 45 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index c1ac294d..311987e5 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -20,10 +20,12 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; +import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.net.UnknownHostException; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; @@ -68,6 +70,8 @@ final class DefaultTypeAdapters { private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter(); private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter(); 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 MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); 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 HashSetCreator HASH_SET_CREATOR = new HashSetCreator(); private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = - new GregorianCalendarTypeAdapter(); + new GregorianCalendarTypeAdapter(); // The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS // must be defined after the constants for the type adapters. Otherwise, the type adapter @@ -105,6 +109,7 @@ final class DefaultTypeAdapters { new ParameterizedTypeHandlerMap>(); map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER); + map.registerForTypeHierarchy(InetAddress.class, INET_ADDRESS_ADAPTER); map.register(URL.class, URL_TYPE_ADAPTER); map.register(URI.class, URI_TYPE_ADAPTER); map.register(UUID.class, UUUID_TYPE_ADAPTER); @@ -142,6 +147,7 @@ final class DefaultTypeAdapters { ParameterizedTypeHandlerMap> map = new ParameterizedTypeHandlerMap>(); 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(URI.class, wrapDeserializer(URI_TYPE_ADAPTER)); map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER)); @@ -397,6 +403,24 @@ final class DefaultTypeAdapters { } } + static class DefaultInetAddressAdapter + implements JsonDeserializer, JsonSerializer { + + 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 class EnumTypeAdapter> implements JsonSerializer, JsonDeserializer { diff --git a/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java b/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java new file mode 100644 index 00000000..7c67d975 --- /dev/null +++ b/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java @@ -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); + } +}