Merge pull request #733 from google/java_util_additional_classes
Supported Currency and added additional tests for Vector, Stack and Properties.
This commit is contained in:
commit
e48c780389
@ -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);
|
||||
|
@ -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> CURRENCY = new TypeAdapter<Currency>() {
|
||||
@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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
|
@ -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<PriorityQueue<Integer>>(){}.getType();
|
||||
PriorityQueue<Integer> 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<Vector<Integer>>(){}.getType();
|
||||
Vector<Integer> 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<Stack<Integer>>(){}.getType();
|
||||
Stack<Integer> 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<String> list = new ArrayList<String>();
|
||||
list.add("foo");
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 java.util.Properties;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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\""));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user