review comments

This commit is contained in:
Ganesh Pagade 2019-03-09 14:59:08 +05:30
parent b1834b6d96
commit 02fd91ad35
2 changed files with 51 additions and 25 deletions

View File

@ -1,14 +1,18 @@
package org.baeldung.gson.entities; package org.baeldung.gson.entities;
import java.util.Objects;
public class MyClass { public class MyClass {
private int id; private int id;
private String[] strings; private String name;
public MyClass() { public MyClass(int id, String name) {
id = 1; this.id = id;
strings = new String[] { "a", "b" }; this.name = name;
} }
public MyClass() { }
public int getId() { public int getId() {
return id; return id;
} }
@ -17,11 +21,27 @@ public class MyClass {
this.id = id; this.id = id;
} }
public String[] getStrings() { public String getName() {
return strings; return name;
} }
public void setStrings(String[] strings) { public void setName(String name) {
this.strings = strings; 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);
} }
} }

View File

@ -8,6 +8,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.baeldung.gson.entities.Animal; import org.baeldung.gson.entities.Animal;
import org.baeldung.gson.entities.Cow; import org.baeldung.gson.entities.Cow;
@ -18,40 +19,42 @@ import org.junit.Test;
public class GsonAdvanceUnitTest { public class GsonAdvanceUnitTest {
@Test public void givenListOfMyClass_whenSerializing_thenCorrect() { @Test
List<MyClass> list = new ArrayList<>(); public void givenListOfMyClass_whenSerializing_thenCorrect() {
list.add(new MyClass()); List<MyClass> list = Arrays.asList(new MyClass(1, "name1"), new MyClass(2, "name2"));
list.add(new MyClass());
Gson gson = new Gson(); Gson gson = new Gson();
String jsonString = gson.toJson(list); 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); assertEquals(expectedString, jsonString);
} }
@Test(expected = ClassCastException.class) @Test(expected = ClassCastException.class)
public void givenJsonString_whenIncorrectDeserializing_thenThrowClassCastException() { 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(); 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() { @Test
String inputString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]"; 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(); Type listOfMyClassObject = new TypeToken<ArrayList<MyClass>>() {}.getType();
Gson gson = new Gson(); Gson gson = new Gson();
List<MyClass> list = gson.fromJson(inputString, listOfMyClassObject); List<MyClass> outputList = gson.fromJson(inputString, listOfMyClassObject);
assertEquals(2, list.size()); assertEquals(inputList, outputList);
assertEquals(1, list.get(0).getId());
} }
@Test public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() { @Test
public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() {
String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]"; String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
List<Animal> inList = new ArrayList<>(); List<Animal> inList = new ArrayList<>();
@ -63,7 +66,8 @@ public class GsonAdvanceUnitTest {
assertEquals(expectedString, jsonString); 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\"}]"; String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
AnimalDeserializer deserializer = new AnimalDeserializer("type"); AnimalDeserializer deserializer = new AnimalDeserializer("type");
@ -79,7 +83,8 @@ public class GsonAdvanceUnitTest {
assertTrue(outList.get(0) instanceof Dog); 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\"}]"; String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
List<Animal> inList = new ArrayList<>(); List<Animal> inList = new ArrayList<>();
@ -90,7 +95,8 @@ public class GsonAdvanceUnitTest {
assertEquals(expectedString, jsonString); 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\"}]"; String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]";
Type listOfAnimals = new TypeToken<ArrayList<Animal>>() {}.getType(); Type listOfAnimals = new TypeToken<ArrayList<Animal>>() {}.getType();