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")
|
||||
public <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
StringReader reader = new StringReader(json);
|
||||
T target = (T) fromJson(reader, typeOfT);
|
||||
return target;
|
||||
|
@ -44,7 +44,12 @@ final class ObjectTypePair {
|
||||
Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("preserveType: %b, type: %s, obj: %s", preserveType, type, obj);
|
||||
}
|
||||
|
||||
<HANDLER> Pair<HANDLER, ObjectTypePair> getMatchingHandler(
|
||||
ParameterizedTypeHandlerMap<HANDLER> handlers) {
|
||||
HANDLER handler = null;
|
||||
|
@ -140,11 +140,17 @@ public class ObjectTest extends TestCase {
|
||||
assertEquals("", gson.toJson(null));
|
||||
}
|
||||
|
||||
public void testNullDeserialization() throws Exception {
|
||||
public void testEmptyStringDeserialization() throws Exception {
|
||||
Object object = gson.fromJson("", Object.class);
|
||||
assertNull(object);
|
||||
}
|
||||
|
||||
public void testNullDeserialization() throws Exception {
|
||||
String myNullObject = null;
|
||||
Object object = gson.fromJson(myNullObject, Object.class);
|
||||
assertNull(object);
|
||||
}
|
||||
|
||||
public void testNullFieldsSerialization() throws Exception {
|
||||
Nested target = new Nested(new BagOfPrimitives(10, 20, false, "stringValue"), null);
|
||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||
|
Loading…
Reference in New Issue
Block a user