From 0b5f3dc8255a4811df48817dc9b899d67e8725fa Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Sat, 13 Dec 2008 20:42:31 +0000 Subject: [PATCH] Added UUID as a default type supported by Gson. --- .../com/google/gson/DefaultTypeAdapters.java | 20 +++++++++++++++++++ .../functional/DefaultTypeAdaptersTest.java | 14 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index d7f3b4d6..186f4468 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -41,6 +41,7 @@ import java.util.Set; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; +import java.util.UUID; /** * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, @@ -57,6 +58,7 @@ final class DefaultTypeAdapters { private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter(); private static final UrlTypeAdapter URL_TYPE_ADAPTER = new UrlTypeAdapter(); 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 CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter(); private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); @@ -94,6 +96,7 @@ final class DefaultTypeAdapters { map.register(Enum.class, ENUM_TYPE_ADAPTER); map.register(URL.class, URL_TYPE_ADAPTER); map.register(URI.class, URI_TYPE_ADAPTER); + map.register(UUID.class, UUUID_TYPE_ADAPTER); map.register(Locale.class, LOCALE_TYPE_ADAPTER); map.register(Collection.class, COLLECTION_TYPE_ADAPTER); map.register(Map.class, MAP_TYPE_ADAPTER); @@ -131,6 +134,7 @@ final class DefaultTypeAdapters { map.register(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER)); map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER)); map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER)); + map.register(UUID.class, UUUID_TYPE_ADAPTER); map.register(Locale.class, wrapDeserializer(LOCALE_TYPE_ADAPTER)); map.register(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER)); map.register(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER)); @@ -328,6 +332,22 @@ final class DefaultTypeAdapters { return UriTypeAdapter.class.getSimpleName(); } } + + private static class UuidTypeAdapter implements JsonSerializer, JsonDeserializer { + public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + + public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return UUID.fromString(json.getAsString()); + } + + @Override + public String toString() { + return UuidTypeAdapter.class.getSimpleName(); + } + } private static class LocaleTypeAdapter implements JsonSerializer, JsonDeserializer, InstanceCreator { diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index f2b352ef..695c1dfe 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -25,6 +25,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Locale; import java.util.Set; +import java.util.UUID; import junit.framework.TestCase; @@ -88,6 +89,19 @@ public class DefaultTypeAdaptersTest extends TestCase { URI target = gson.fromJson(json, URI.class); assertEquals(uriValue, target.toASCIIString()); } + + public void testUuidSerialization() throws Exception { + String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0"; + UUID uuid = UUID.fromString(uuidValue); + assertEquals('"' + uuidValue + '"', gson.toJson(uuid)); + } + + public void testUuidDeserialization() { + String uuidValue = "c237bec1-19ef-4858-a98e-521cf0aad4c0"; + String json = '"' + uuidValue + '"'; + UUID target = gson.fromJson(json, UUID.class); + assertEquals(uuidValue, target.toString()); + } public void testLocaleSerializationWithLanguage() { Locale target = new Locale("en");