deserialization work
This commit is contained in:
parent
ade4f14702
commit
2e48d26ff5
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"collection": [
|
||||
{
|
||||
"name": "Test order1",
|
||||
"detail": "ahk ks"
|
||||
},
|
||||
{
|
||||
"name": "Test order2",
|
||||
"detail": "Fisteku"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
[
|
||||
{
|
||||
"name": "Test order1",
|
||||
"detail": "ahk ks"
|
||||
},
|
||||
{
|
||||
"name": "Test order2",
|
||||
"detail": "Fisteku"
|
||||
}
|
||||
]
|
|
@ -0,0 +1,47 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
public class Foo {
|
||||
public int intValue;
|
||||
public String stringValue;
|
||||
|
||||
public Foo(final int intValue, final String stringValue) {
|
||||
this.intValue = intValue;
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + intValue;
|
||||
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Foo other = (Foo) obj;
|
||||
if (intValue != other.intValue)
|
||||
return false;
|
||||
if (stringValue == null) {
|
||||
if (other.stringValue != null)
|
||||
return false;
|
||||
} else if (!stringValue.equals(other.stringValue))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public class FooDeserializer implements JsonDeserializer<Foo[]> {
|
||||
|
||||
@Override
|
||||
public Foo[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
||||
final JsonArray jArray = json.getAsJsonArray();
|
||||
final Foo[] scArray = new Foo[jArray.size()];
|
||||
int index = 0;
|
||||
for (final JsonElement jElement : jArray) {
|
||||
final int i = jElement.getAsJsonObject().get("intValue").getAsInt();
|
||||
final String s = jElement.getAsJsonObject().get("stringValue").getAsString();
|
||||
scArray[index++] = new Foo(i, s);
|
||||
}
|
||||
return scArray;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
public class FooDeserializerFromJsonWithDifferentFields implements JsonDeserializer<Foo> {
|
||||
|
||||
@Override
|
||||
public Foo deserialize(final JsonElement jElement, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
||||
final JsonObject jObject = jElement.getAsJsonObject();
|
||||
final int intValue = jObject.get("valueInt").getAsInt();
|
||||
final String stringValue = jObject.get("valueString").getAsString();
|
||||
return new Foo(intValue, stringValue);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
public class GenericTargetClass<INTEGER> {
|
||||
public class GenericFoo<INTEGER> {
|
||||
|
||||
public INTEGER intField;
|
||||
|
||||
GenericTargetClass(final INTEGER value) {
|
||||
GenericFoo(final INTEGER value) {
|
||||
intField = value;
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
public class SourceClass {
|
||||
int intValue;
|
||||
String stringValue;
|
||||
|
||||
public SourceClass(int intValue, String stringValue) {
|
||||
this.intValue = intValue;
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SourceClass{" +
|
||||
"intValue=" + intValue +
|
||||
", stringValue='" + stringValue + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof SourceClass)) return false;
|
||||
|
||||
SourceClass that = (SourceClass) o;
|
||||
|
||||
if (intValue != that.intValue) return false;
|
||||
if (!stringValue.equals(that.stringValue)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class SourceClassDeserializer implements JsonDeserializer<SourceClass[]> {
|
||||
|
||||
@Override
|
||||
public SourceClass[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonArray jArray = json.getAsJsonArray();
|
||||
SourceClass[] scArray = new SourceClass[jArray.size()];
|
||||
int index = 0;
|
||||
for (JsonElement jElement : jArray) {
|
||||
int i = jElement.getAsJsonObject().get("intValue").getAsInt();
|
||||
String s = jElement.getAsJsonObject().get("stringValue").getAsString();
|
||||
scArray[index++] = new SourceClass(i, s);
|
||||
}
|
||||
return scArray;
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
public class TargetClass {
|
||||
public int intValue;
|
||||
public String stringValue;
|
||||
|
||||
public TargetClass(final int intValue, final String stringValue) {
|
||||
this.intValue = intValue;
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}';
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package org.baeldung.gson.deserialization;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
|
||||
public class TargetClassDeserializer implements JsonDeserializer<TargetClass> {
|
||||
|
||||
@Override
|
||||
public TargetClass deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject jObject = jElement.getAsJsonObject();
|
||||
int intValue = jObject.get("valueInt").getAsInt();
|
||||
String stringValue = jObject.get("valueString").getAsString();
|
||||
return new TargetClass(intValue, stringValue);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package org.baeldung.gson.deserialization.test;
|
||||
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
@ -10,11 +12,9 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.gson.deserialization.GenericTargetClass;
|
||||
import org.baeldung.gson.deserialization.SourceClass;
|
||||
import org.baeldung.gson.deserialization.SourceClassDeserializer;
|
||||
import org.baeldung.gson.deserialization.TargetClass;
|
||||
import org.baeldung.gson.deserialization.TargetClassDeserializer;
|
||||
import org.baeldung.gson.deserialization.Foo;
|
||||
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
|
||||
import org.baeldung.gson.deserialization.GenericFoo;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
@ -26,33 +26,67 @@ import com.google.gson.reflect.TypeToken;
|
|||
|
||||
public class GsonDeserializationTest {
|
||||
|
||||
// tests - single element
|
||||
|
||||
@Test
|
||||
public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenUsingCustomDeserializer_thenCorrect() {
|
||||
public final void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() {
|
||||
final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}";
|
||||
final Foo targetObject = new Gson().fromJson(serializedSourceObject, Foo.class);
|
||||
|
||||
assertEquals(targetObject.intValue, 1);
|
||||
assertEquals(targetObject.stringValue, "one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenJsonHasNonMatchingFieldNames_whenDeserializingWithCustomDeserializer_thenCorrect() {
|
||||
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
|
||||
|
||||
final GsonBuilder gsonBldr = new GsonBuilder();
|
||||
gsonBldr.registerTypeAdapter(TargetClass.class, new TargetClassDeserializer());
|
||||
final Gson gson = gsonBldr.create();
|
||||
final TargetClass targetObject = gson.fromJson(jsonSourceObject, TargetClass.class);
|
||||
gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields());
|
||||
final Foo targetObject = gsonBldr.create().fromJson(jsonSourceObject, Foo.class);
|
||||
|
||||
assertEquals(targetObject.intValue, 7);
|
||||
assertEquals(targetObject.stringValue, "seven");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonWithArray_whenUsingGsonCustomDeserializer_thenMapsToArrayList() {
|
||||
// It is necessary to override the equals() method in SourceClass
|
||||
public final void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
|
||||
final Type genericTargetClassType = new TypeToken<GenericFoo<Integer>>() {
|
||||
}.getType();
|
||||
final String serializedSourceObject = "{\"intField\":1}";
|
||||
|
||||
final GenericFoo<Integer> targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType);
|
||||
|
||||
assertEquals(targetObject.intField, new Integer(1));
|
||||
}
|
||||
|
||||
// tests - multiple elements
|
||||
|
||||
@Test
|
||||
public final void givenJsonArrayOfFoos_whenDeserializingToList_thenCorrect() {
|
||||
final String jsonSourceObject = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
final GsonBuilder gsonBldr = new GsonBuilder();
|
||||
gsonBldr.registerTypeHierarchyAdapter(SourceClass[].class, new SourceClassDeserializer());
|
||||
final Gson gson = gsonBldr.create();
|
||||
final Foo[] objectsAsArray = new GsonBuilder().create().fromJson(jsonSourceObject, Foo[].class);
|
||||
final List<Foo> targetList = Arrays.asList(objectsAsArray);
|
||||
|
||||
final List<SourceClass> targetList = Arrays.asList(gson.fromJson(jsonSourceObject, SourceClass[].class));
|
||||
|
||||
assertEquals(new SourceClass(1, "one"), targetList.get(0));
|
||||
assertThat(targetList, hasItem(new Foo(1, "one")));
|
||||
assertThat(targetList, hasItem(new Foo(2, "two")));
|
||||
assertThat(targetList, not(hasItem(new Foo(1, "two"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenDeserializingManualy_thenCorrect() {
|
||||
public final void givenUsingGson_whenDeserializingCollection_thenCorrect() {
|
||||
final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
final Type targetClassType = new TypeToken<ArrayList<Foo>>() {
|
||||
}.getType();
|
||||
|
||||
final Collection<Foo> targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType);
|
||||
assertThat(targetCollection, instanceOf(ArrayList.class));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void whenDeserializingJsonIntoElements_thenCorrect() {
|
||||
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
|
||||
final JsonParser jParser = new JsonParser();
|
||||
final JsonElement jElement = jParser.parse(jsonSourceObject);
|
||||
|
@ -60,39 +94,10 @@ public class GsonDeserializationTest {
|
|||
final int intValue = jObject.get("valueInt").getAsInt();
|
||||
final String stringValue = jObject.get("valueString").getAsString();
|
||||
|
||||
final TargetClass targetObject = new TargetClass(intValue, stringValue);
|
||||
final Foo targetObject = new Foo(intValue, stringValue);
|
||||
|
||||
assertEquals(targetObject.intValue, 7);
|
||||
assertEquals(targetObject.stringValue, "seven");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() {
|
||||
final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}";
|
||||
final TargetClass targetObject = new Gson().fromJson(serializedSourceObject, TargetClass.class);
|
||||
|
||||
assertEquals(targetObject.intValue, 1);
|
||||
assertEquals(targetObject.stringValue, "one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
|
||||
final Type genericTargetClassType = new TypeToken<GenericTargetClass<Integer>>() {
|
||||
}.getType();
|
||||
final String serializedSourceObject = "{\"intField\":1}";
|
||||
|
||||
final GenericTargetClass<Integer> targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType);
|
||||
|
||||
assertEquals(targetObject.intField, new Integer(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGson_whenDeserializingCollection_thenCorrect() {
|
||||
final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
final Type targetClassType = new TypeToken<ArrayList<TargetClass>>() {
|
||||
}.getType();
|
||||
|
||||
final Collection<TargetClass> targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType);
|
||||
assertThat(targetCollection, instanceOf(ArrayList.class));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue