Added UUID as a default type supported by Gson.

This commit is contained in:
Joel Leitch 2008-12-13 20:42:31 +00:00
parent d16553701b
commit 0b5f3dc825
2 changed files with 34 additions and 0 deletions

View File

@ -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));
@ -329,6 +333,22 @@ final class DefaultTypeAdapters {
}
}
private static class UuidTypeAdapter implements JsonSerializer<UUID>, JsonDeserializer<UUID> {
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<Locale>,
JsonDeserializer<Locale>, InstanceCreator<Locale> {
public JsonElement serialize(Locale src, Type typeOfSrc, JsonSerializationContext context) {

View File

@ -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;
@ -89,6 +90,19 @@ public class DefaultTypeAdaptersTest extends TestCase {
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");
assertEquals("\"en\"", gson.toJson(target));