add some gson deserialization examples

This commit is contained in:
DOHA 2015-12-22 17:43:44 +02:00
parent f0284f3fba
commit bb6166986e
4 changed files with 83 additions and 8 deletions

View File

@ -9,6 +9,10 @@ public class Foo {
this.stringValue = stringValue; this.stringValue = stringValue;
} }
public Foo(final String stringValue) {
this.stringValue = stringValue;
}
public Foo() { public Foo() {
super(); super();
} }
@ -19,27 +23,33 @@ public class Foo {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + intValue; result = (prime * result) + intValue;
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); result = (prime * result) + ((stringValue == null) ? 0 : stringValue.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (this == obj) if (this == obj) {
return true; return true;
if (obj == null) }
if (obj == null) {
return false; return false;
if (getClass() != obj.getClass()) }
if (getClass() != obj.getClass()) {
return false; return false;
}
final Foo other = (Foo) obj; final Foo other = (Foo) obj;
if (intValue != other.intValue) if (intValue != other.intValue) {
return false; return false;
}
if (stringValue == null) { if (stringValue == null) {
if (other.stringValue != null) if (other.stringValue != null) {
return false; return false;
} else if (!stringValue.equals(other.stringValue)) }
} else if (!stringValue.equals(other.stringValue)) {
return false; return false;
}
return true; return true;
} }

View File

@ -0,0 +1,14 @@
package org.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import com.google.gson.InstanceCreator;
public class FooInstanceCreator implements InstanceCreator<Foo> {
@Override
public Foo createInstance(Type type) {
return new Foo("sample");
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.gson.deserialization;
public class FooWithInner {
public int intValue;
public String stringValue;
public InnerFoo innerFoo;
public FooWithInner(int intValue, String stringValue, String name) {
super();
this.intValue = intValue;
this.stringValue = stringValue;
this.innerFoo = new InnerFoo(name);
}
public class InnerFoo {
public String name;
public InnerFoo(String name) {
super();
this.name = name;
}
}
}

View File

@ -12,6 +12,8 @@ import java.util.Collection;
import org.baeldung.gson.deserialization.Foo; import org.baeldung.gson.deserialization.Foo;
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields; import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import org.baeldung.gson.deserialization.FooInstanceCreator;
import org.baeldung.gson.deserialization.FooWithInner;
import org.baeldung.gson.deserialization.GenericFoo; import org.baeldung.gson.deserialization.GenericFoo;
import org.junit.Test; import org.junit.Test;
@ -108,4 +110,29 @@ public class GsonDeserializationTest {
assertEquals(targetObject.stringValue, "seven"); assertEquals(targetObject.stringValue, "seven");
} }
// new examples
@Test
public void whenDeserializingToNestedObjects_thenCorrect() {
final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"innerFoo\":{\"name\":\"inner\"}}";
final FooWithInner targetObject = new Gson().fromJson(json, FooWithInner.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "one");
assertEquals(targetObject.innerFoo.name, "inner");
}
@Test
public void whenDeserializingUsingInstanceCreator_thenCorrect() {
final String json = "{\"intValue\":1}";
final GsonBuilder gsonBldr = new GsonBuilder();
gsonBldr.registerTypeAdapter(Foo.class, new FooInstanceCreator());
final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "sample");
}
} }