Added support for deserializing from null input to a null value.

Added toString() method to ObjectTypePair.
This commit is contained in:
Inderjeet Singh 2010-10-22 16:06:59 +00:00
parent c01fd85adb
commit d3eda04f33
3 changed files with 16 additions and 2 deletions

View File

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

View File

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

View File

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