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

View File

@ -44,7 +44,12 @@ final class ObjectTypePair {
Type getType() { Type getType() {
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;

View File

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