added currency class

This commit is contained in:
Inderjeet Singh 2015-11-06 15:41:15 -08:00
parent 457f53f08f
commit 47cc34548d
3 changed files with 66 additions and 0 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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;
}
}