add some gson deserialization examples
This commit is contained in:
parent
f0284f3fba
commit
bb6166986e
|
@ -9,6 +9,10 @@ public class Foo {
|
|||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public Foo(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
@ -19,27 +23,33 @@ public class Foo {
|
|||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + intValue;
|
||||
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
|
||||
result = (prime * result) + intValue;
|
||||
result = (prime * result) + ((stringValue == null) ? 0 : stringValue.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
if (this == obj) {
|
||||
return true;
|
||||
if (obj == null)
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Foo other = (Foo) obj;
|
||||
if (intValue != other.intValue)
|
||||
if (intValue != other.intValue) {
|
||||
return false;
|
||||
}
|
||||
if (stringValue == null) {
|
||||
if (other.stringValue != null)
|
||||
if (other.stringValue != null) {
|
||||
return false;
|
||||
} else if (!stringValue.equals(other.stringValue))
|
||||
}
|
||||
} else if (!stringValue.equals(other.stringValue)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -12,6 +12,8 @@ import java.util.Collection;
|
|||
|
||||
import org.baeldung.gson.deserialization.Foo;
|
||||
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.junit.Test;
|
||||
|
||||
|
@ -108,4 +110,29 @@ public class GsonDeserializationTest {
|
|||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue