Added support for deserializing from null input to a null value.
Added toString() method to ObjectTypePair.
This commit is contained in:
parent
c01fd85adb
commit
d3eda04f33
|
@ -415,6 +415,9 @@ public final class Gson {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
|
public <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
StringReader reader = new StringReader(json);
|
StringReader reader = new StringReader(json);
|
||||||
T target = (T) fromJson(reader, typeOfT);
|
T target = (T) fromJson(reader, typeOfT);
|
||||||
return target;
|
return target;
|
||||||
|
|
|
@ -45,6 +45,11 @@ final class ObjectTypePair {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("preserveType: %b, type: %s, obj: %s", preserveType, type, obj);
|
||||||
|
}
|
||||||
|
|
||||||
<HANDLER> Pair<HANDLER, ObjectTypePair> getMatchingHandler(
|
<HANDLER> Pair<HANDLER, ObjectTypePair> getMatchingHandler(
|
||||||
ParameterizedTypeHandlerMap<HANDLER> handlers) {
|
ParameterizedTypeHandlerMap<HANDLER> handlers) {
|
||||||
HANDLER handler = null;
|
HANDLER handler = null;
|
||||||
|
|
|
@ -140,11 +140,17 @@ public class ObjectTest extends TestCase {
|
||||||
assertEquals("", gson.toJson(null));
|
assertEquals("", gson.toJson(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNullDeserialization() throws Exception {
|
public void testEmptyStringDeserialization() throws Exception {
|
||||||
Object object = gson.fromJson("", Object.class);
|
Object object = gson.fromJson("", Object.class);
|
||||||
assertNull(object);
|
assertNull(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testNullDeserialization() throws Exception {
|
||||||
|
String myNullObject = null;
|
||||||
|
Object object = gson.fromJson(myNullObject, Object.class);
|
||||||
|
assertNull(object);
|
||||||
|
}
|
||||||
|
|
||||||
public void testNullFieldsSerialization() throws Exception {
|
public void testNullFieldsSerialization() throws Exception {
|
||||||
Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"), null);
|
Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"), null);
|
||||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user