From 47cc34548d2ede1f294ff72ae02c05e2b0eae404 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 6 Nov 2015 15:41:15 -0800 Subject: [PATCH 1/3] added currency class --- gson/src/main/java/com/google/gson/Gson.java | 1 + .../gson/internal/bind/TypeAdapters.java | 13 +++++ .../google/gson/functional/JavaUtilTest.java | 52 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/functional/JavaUtilTest.java 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; + } +} From 01944b246b08439c96653a3f152b6ed8d23e8b88 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 6 Nov 2015 16:16:56 -0800 Subject: [PATCH 2/3] additional tests for PriorityQueue, Vector and Stack. --- .../gson/functional/CollectionTest.java | 60 +++++++++++++++---- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/CollectionTest.java b/gson/src/test/java/com/google/gson/functional/CollectionTest.java index ac6fec93..9a8f707b 100644 --- a/gson/src/test/java/com/google/gson/functional/CollectionTest.java +++ b/gson/src/test/java/com/google/gson/functional/CollectionTest.java @@ -16,6 +16,21 @@ package com.google.gson.functional; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; +import java.util.Vector; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; @@ -28,18 +43,6 @@ import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - /** * Functional tests for Json serialization and deserialization of collections. * @@ -124,6 +127,39 @@ public class CollectionTest extends TestCase { assertEquals("a2", queue.element()); } + public void testPriorityQueue() throws Exception { + Type type = new TypeToken>(){}.getType(); + PriorityQueue queue = gson.fromJson("[10, 20, 22]", type); + assertEquals(3, queue.size()); + String json = gson.toJson(queue); + assertEquals(10, queue.remove().intValue()); + assertEquals(20, queue.remove().intValue()); + assertEquals(22, queue.remove().intValue()); + assertEquals("[10,20,22]", json); + } + + public void testVector() { + Type type = new TypeToken>(){}.getType(); + Vector target = gson.fromJson("[10, 20, 31]", type); + assertEquals(3, target.size()); + assertEquals(10, target.get(0).intValue()); + assertEquals(20, target.get(1).intValue()); + assertEquals(31, target.get(2).intValue()); + String json = gson.toJson(target); + assertEquals("[10,20,31]", json); + } + + public void testStack() { + Type type = new TypeToken>(){}.getType(); + Stack target = gson.fromJson("[11, 13, 17]", type); + assertEquals(3, target.size()); + String json = gson.toJson(target); + assertEquals(17, target.pop().intValue()); + assertEquals(13, target.pop().intValue()); + assertEquals(11, target.pop().intValue()); + assertEquals("[11,13,17]", json); + } + public void testNullsInListSerialization() { List list = new ArrayList(); list.add("foo"); From 10cefa49d2ade8f1ba99693509916128c8786a6d Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 6 Nov 2015 16:17:47 -0800 Subject: [PATCH 3/3] added tests for Properties --- .../java/com/google/gson/functional/JavaUtilTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java b/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java index 70f4234b..05209653 100644 --- a/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java +++ b/gson/src/test/java/com/google/gson/functional/JavaUtilTest.java @@ -17,6 +17,7 @@ package com.google.gson.functional; import java.util.Currency; +import java.util.Properties; import com.google.gson.Gson; @@ -49,4 +50,13 @@ public class JavaUtilTest extends TestCase { private static class CurrencyHolder { Currency value; } + + public void testProperties() { + Properties props = gson.fromJson("{'a':'v1','b':'v2'}", Properties.class); + assertEquals("v1", props.getProperty("a")); + assertEquals("v2", props.getProperty("b")); + String json = gson.toJson(props); + assertTrue(json.contains("\"a\":\"v1\"")); + assertTrue(json.contains("\"b\":\"v2\"")); + } }