added currency class
This commit is contained in:
parent
457f53f08f
commit
47cc34548d
@ -232,6 +232,7 @@ public final class Gson {
|
|||||||
factories.add(TypeAdapters.URL_FACTORY);
|
factories.add(TypeAdapters.URL_FACTORY);
|
||||||
factories.add(TypeAdapters.URI_FACTORY);
|
factories.add(TypeAdapters.URI_FACTORY);
|
||||||
factories.add(TypeAdapters.UUID_FACTORY);
|
factories.add(TypeAdapters.UUID_FACTORY);
|
||||||
|
factories.add(TypeAdapters.CURRENCY_FACTORY);
|
||||||
factories.add(TypeAdapters.LOCALE_FACTORY);
|
factories.add(TypeAdapters.LOCALE_FACTORY);
|
||||||
factories.add(TypeAdapters.INET_ADDRESS_FACTORY);
|
factories.add(TypeAdapters.INET_ADDRESS_FACTORY);
|
||||||
factories.add(TypeAdapters.BIT_SET_FACTORY);
|
factories.add(TypeAdapters.BIT_SET_FACTORY);
|
||||||
|
@ -27,6 +27,7 @@ import java.sql.Timestamp;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Currency;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.HashMap;
|
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 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() {
|
public static final TypeAdapterFactory TIMESTAMP_FACTORY = new TypeAdapterFactory() {
|
||||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
@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) {
|
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user