Fix primitive wrapping and casting.
Issue: 235
This commit is contained in:
parent
d0977c2e3a
commit
20d895ff95
@ -392,8 +392,9 @@ public final class Gson {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
|
public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
|
||||||
T target = (T) fromJson(json, (Type) classOfT);
|
Class<T> wrapped = Primitives.wrap(classOfT);
|
||||||
return target;
|
Object object = fromJson(json, (Type) wrapped);
|
||||||
|
return wrapped.cast(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -442,8 +443,9 @@ public final class Gson {
|
|||||||
* @since 1.2
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonParseException {
|
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonParseException {
|
||||||
T target = classOfT.cast(fromJson(new JsonReader(json), classOfT));
|
Class<T> wrapped = Primitives.wrap(classOfT);
|
||||||
return target;
|
Object object = fromJson(new JsonReader(json), wrapped);
|
||||||
|
return wrapped.cast(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -501,8 +503,9 @@ public final class Gson {
|
|||||||
* @since 1.3
|
* @since 1.3
|
||||||
*/
|
*/
|
||||||
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException {
|
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException {
|
||||||
T target = classOfT.cast(fromJson(json, (Type) classOfT));
|
Class<T> wrapped = Primitives.wrap(classOfT);
|
||||||
return target;
|
Object object = fromJson(json, (Type) wrapped);
|
||||||
|
return wrapped.cast(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,15 +16,13 @@
|
|||||||
|
|
||||||
package com.google.gson.functional;
|
package com.google.gson.functional;
|
||||||
|
|
||||||
|
import java.io.StringReader;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
import com.google.gson.LongSerializationPolicy;
|
|
||||||
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
|
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -578,4 +576,10 @@ public class PrimitiveTest extends TestCase {
|
|||||||
private static class ClassWithIntegerField {
|
private static class ClassWithIntegerField {
|
||||||
Integer i;
|
Integer i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testPrimitiveClassLiteral() {
|
||||||
|
assertEquals(1, gson.fromJson("1", int.class).intValue());
|
||||||
|
assertEquals(1, gson.fromJson(new StringReader("1"), int.class).intValue());
|
||||||
|
assertEquals(1, gson.fromJson(new JsonPrimitive(1), int.class).intValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user