diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 33c3b4b8..c29eada4 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -232,6 +232,7 @@ public final class Gson { factories.add(TypeAdapters.URL_FACTORY); factories.add(TypeAdapters.URI_FACTORY); factories.add(TypeAdapters.UUID_FACTORY); + factories.add(TypeAdapters.CURRENCY_FACTORY); factories.add(TypeAdapters.LOCALE_FACTORY); factories.add(TypeAdapters.INET_ADDRESS_FACTORY); factories.add(TypeAdapters.BIT_SET_FACTORY); diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index d284dd5b..6f03eaa9 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -27,6 +27,7 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.BitSet; import java.util.Calendar; +import java.util.Currency; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; @@ -576,6 +577,18 @@ public final class TypeAdapters { public static final TypeAdapterFactory UUID_FACTORY = newFactory(UUID.class, UUID); + public static final TypeAdapter CURRENCY = new TypeAdapter() { + @Override + public Currency read(JsonReader in) throws IOException { + return Currency.getInstance(in.nextString()); + } + @Override + public void write(JsonWriter out, Currency value) throws IOException { + out.value(value.getCurrencyCode()); + } + }.nullSafe(); + public static final TypeAdapterFactory CURRENCY_FACTORY = newFactory(Currency.class, CURRENCY); + public static final TypeAdapterFactory TIMESTAMP_FACTORY = new TypeAdapterFactory() { @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal @Override public TypeAdapter create(Gson gson, TypeToken typeToken) { diff --git a/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java b/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java new file mode 100644 index 00000000..70f4234b --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 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.functional; + +import java.util.Currency; + +import com.google.gson.Gson; + +import junit.framework.TestCase; + +/** + * Functional test for Json serialization and deserialization for classes in java.util + */ +public class JavaUtilTest extends TestCase { + private Gson gson; + + @Override + protected void setUp() throws Exception { + super.setUp(); + gson = new Gson(); + } + + public void testCurrency() throws Exception { + CurrencyHolder target = gson.fromJson("{'value':'USD'}", CurrencyHolder.class); + assertEquals("USD", target.value.getCurrencyCode()); + String json = gson.toJson(target); + assertEquals("{\"value\":\"USD\"}", json); + + // null handling + target = gson.fromJson("{'value':null}", CurrencyHolder.class); + assertNull(target.value); + assertEquals("{}", gson.toJson(target)); + } + + private static class CurrencyHolder { + Currency value; + } +}