review comments
This commit is contained in:
parent
b1834b6d96
commit
02fd91ad35
|
@ -1,14 +1,18 @@
|
|||
package org.baeldung.gson.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class MyClass {
|
||||
private int id;
|
||||
private String[] strings;
|
||||
private String name;
|
||||
|
||||
public MyClass() {
|
||||
id = 1;
|
||||
strings = new String[] { "a", "b" };
|
||||
public MyClass(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public MyClass() { }
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -17,11 +21,27 @@ public class MyClass {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public String[] getStrings() {
|
||||
return strings;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setStrings(String[] strings) {
|
||||
this.strings = strings;
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MyClass myClass = (MyClass) o;
|
||||
return id == myClass.id && Objects.equals(name, myClass.name);
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.google.gson.GsonBuilder;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.baeldung.gson.entities.Animal;
|
||||
import org.baeldung.gson.entities.Cow;
|
||||
|
@ -18,40 +19,42 @@ import org.junit.Test;
|
|||
|
||||
public class GsonAdvanceUnitTest {
|
||||
|
||||
@Test public void givenListOfMyClass_whenSerializing_thenCorrect() {
|
||||
List<MyClass> list = new ArrayList<>();
|
||||
list.add(new MyClass());
|
||||
list.add(new MyClass());
|
||||
@Test
|
||||
public void givenListOfMyClass_whenSerializing_thenCorrect() {
|
||||
List<MyClass> list = Arrays.asList(new MyClass(1, "name1"), new MyClass(2, "name2"));
|
||||
|
||||
Gson gson = new Gson();
|
||||
String jsonString = gson.toJson(list);
|
||||
String expectedString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]";
|
||||
String expectedString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]";
|
||||
|
||||
assertEquals(expectedString, jsonString);
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenJsonString_whenIncorrectDeserializing_thenThrowClassCastException() {
|
||||
String inputString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]";
|
||||
String inputString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]";
|
||||
|
||||
Gson gson = new Gson();
|
||||
List<MyClass> list = gson.fromJson(inputString, ArrayList.class);
|
||||
List<MyClass> outputList = gson.fromJson(inputString, ArrayList.class);
|
||||
|
||||
assertEquals(1, list.get(0).getId());
|
||||
assertEquals(1, outputList.get(0).getId());
|
||||
}
|
||||
|
||||
@Test public void givenJsonString_whenDeserializing_thenReturnListOfMyClass() {
|
||||
String inputString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]";
|
||||
@Test
|
||||
public void givenJsonString_whenDeserializing_thenReturnListOfMyClass() {
|
||||
String inputString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]";
|
||||
List<MyClass> inputList = Arrays.asList(new MyClass(1, "name1"), new MyClass(2, "name2"));
|
||||
|
||||
Type listOfMyClassObject = new TypeToken<ArrayList<MyClass>>() {}.getType();
|
||||
|
||||
Gson gson = new Gson();
|
||||
List<MyClass> list = gson.fromJson(inputString, listOfMyClassObject);
|
||||
List<MyClass> outputList = gson.fromJson(inputString, listOfMyClassObject);
|
||||
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, list.get(0).getId());
|
||||
assertEquals(inputList, outputList);
|
||||
}
|
||||
|
||||
@Test public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() {
|
||||
@Test
|
||||
public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() {
|
||||
String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
|
||||
|
||||
List<Animal> inList = new ArrayList<>();
|
||||
|
@ -63,7 +66,8 @@ public class GsonAdvanceUnitTest {
|
|||
assertEquals(expectedString, jsonString);
|
||||
}
|
||||
|
||||
@Test public void givenPolymorphicList_whenDeserializeWithTypeAdapter_thenCorrect() {
|
||||
@Test
|
||||
public void givenPolymorphicList_whenDeserializeWithTypeAdapter_thenCorrect() {
|
||||
String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
|
||||
|
||||
AnimalDeserializer deserializer = new AnimalDeserializer("type");
|
||||
|
@ -79,7 +83,8 @@ public class GsonAdvanceUnitTest {
|
|||
assertTrue(outList.get(0) instanceof Dog);
|
||||
}
|
||||
|
||||
@Test public void givenPolymorphicList_whenSerializeWithRuntimeTypeAdapter_thenCorrect() {
|
||||
@Test
|
||||
public void givenPolymorphicList_whenSerializeWithRuntimeTypeAdapter_thenCorrect() {
|
||||
String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
|
||||
|
||||
List<Animal> inList = new ArrayList<>();
|
||||
|
@ -90,7 +95,8 @@ public class GsonAdvanceUnitTest {
|
|||
assertEquals(expectedString, jsonString);
|
||||
}
|
||||
|
||||
@Test public void givenPolymorphicList_whenDeserializeWithRuntimeTypeAdapter_thenCorrect() {
|
||||
@Test
|
||||
public void givenPolymorphicList_whenDeserializeWithRuntimeTypeAdapter_thenCorrect() {
|
||||
String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
|
||||
|
||||
Type listOfAnimals = new TypeToken<ArrayList<Animal>>() {}.getType();
|
||||
|
|
Loading…
Reference in New Issue