Skip to content

Commit bb61669

Browse files
committed
add some gson deserialization examples
1 parent f0284f3 commit bb61669

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

gson/src/test/java/org/baeldung/gson/deserialization/Foo.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public Foo(final int intValue, final String stringValue) {
99
this.stringValue = stringValue;
1010
}
1111

12+
public Foo(final String stringValue) {
13+
this.stringValue = stringValue;
14+
}
15+
1216
public Foo() {
1317
super();
1418
}
@@ -19,27 +23,33 @@ public Foo() {
1923
public int hashCode() {
2024
final int prime = 31;
2125
int result = 1;
22-
result = prime * result + intValue;
23-
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
26+
result = (prime * result) + intValue;
27+
result = (prime * result) + ((stringValue == null) ? 0 : stringValue.hashCode());
2428
return result;
2529
}
2630

2731
@Override
2832
public boolean equals(final Object obj) {
29-
if (this == obj)
33+
if (this == obj) {
3034
return true;
31-
if (obj == null)
35+
}
36+
if (obj == null) {
3237
return false;
33-
if (getClass() != obj.getClass())
38+
}
39+
if (getClass() != obj.getClass()) {
3440
return false;
41+
}
3542
final Foo other = (Foo) obj;
36-
if (intValue != other.intValue)
43+
if (intValue != other.intValue) {
3744
return false;
45+
}
3846
if (stringValue == null) {
39-
if (other.stringValue != null)
47+
if (other.stringValue != null) {
4048
return false;
41-
} else if (!stringValue.equals(other.stringValue))
49+
}
50+
} else if (!stringValue.equals(other.stringValue)) {
4251
return false;
52+
}
4353
return true;
4454
}
4555

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.gson.deserialization;
2+
3+
import java.lang.reflect.Type;
4+
5+
import com.google.gson.InstanceCreator;
6+
7+
public class FooInstanceCreator implements InstanceCreator<Foo> {
8+
9+
@Override
10+
public Foo createInstance(Type type) {
11+
return new Foo("sample");
12+
}
13+
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.gson.deserialization;
2+
3+
public class FooWithInner {
4+
public int intValue;
5+
public String stringValue;
6+
public InnerFoo innerFoo;
7+
8+
public FooWithInner(int intValue, String stringValue, String name) {
9+
super();
10+
this.intValue = intValue;
11+
this.stringValue = stringValue;
12+
this.innerFoo = new InnerFoo(name);
13+
}
14+
15+
public class InnerFoo {
16+
public String name;
17+
18+
public InnerFoo(String name) {
19+
super();
20+
this.name = name;
21+
}
22+
23+
}
24+
}

gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import org.baeldung.gson.deserialization.Foo;
1414
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
15+
import org.baeldung.gson.deserialization.FooInstanceCreator;
16+
import org.baeldung.gson.deserialization.FooWithInner;
1517
import org.baeldung.gson.deserialization.GenericFoo;
1618
import org.junit.Test;
1719

@@ -108,4 +110,29 @@ public void whenDeserializingJsonIntoElements_thenCorrect() {
108110
assertEquals(targetObject.stringValue, "seven");
109111
}
110112

113+
// new examples
114+
115+
@Test
116+
public void whenDeserializingToNestedObjects_thenCorrect() {
117+
final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"innerFoo\":{\"name\":\"inner\"}}";
118+
119+
final FooWithInner targetObject = new Gson().fromJson(json, FooWithInner.class);
120+
121+
assertEquals(targetObject.intValue, 1);
122+
assertEquals(targetObject.stringValue, "one");
123+
assertEquals(targetObject.innerFoo.name, "inner");
124+
}
125+
126+
@Test
127+
public void whenDeserializingUsingInstanceCreator_thenCorrect() {
128+
final String json = "{\"intValue\":1}";
129+
130+
final GsonBuilder gsonBldr = new GsonBuilder();
131+
gsonBldr.registerTypeAdapter(Foo.class, new FooInstanceCreator());
132+
final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class);
133+
134+
assertEquals(targetObject.intValue, 1);
135+
assertEquals(targetObject.stringValue, "sample");
136+
}
137+
111138
}

0 commit comments

Comments
 (0)