further gson testing work
This commit is contained in:
parent
01625edeae
commit
ade4f14702
@ -1,12 +1,6 @@
|
|||||||
=========
|
=========
|
||||||
|
|
||||||
## Jackson Cookbooks and Examples
|
## GSON Cookbooks and Examples
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
|
||||||
- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array)
|
|
||||||
- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
|
||||||
- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization)
|
|
||||||
- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization)
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.3</version>
|
<version>${gson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
@ -76,7 +76,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>jackson</finalName>
|
<finalName>gson</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
@ -116,7 +116,7 @@
|
|||||||
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
|
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
|
||||||
|
|
||||||
<!-- marshalling -->
|
<!-- marshalling -->
|
||||||
<jackson.version>2.4.1</jackson.version>
|
<gson.version>2.3</gson.version>
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.7</org.slf4j.version>
|
<org.slf4j.version>1.7.7</org.slf4j.version>
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package org.baeldung.gson.deserialization;
|
|
||||||
|
|
||||||
public class GenericSourceClass {
|
|
||||||
int intField;
|
|
||||||
|
|
||||||
public GenericSourceClass(final int i) {
|
|
||||||
intField = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,18 @@
|
|||||||
|
package org.baeldung.gson.deserialization;
|
||||||
|
|
||||||
|
public class GenericTargetClass<INTEGER> {
|
||||||
|
|
||||||
|
public INTEGER intField;
|
||||||
|
|
||||||
|
GenericTargetClass(final INTEGER value) {
|
||||||
|
intField = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "GenericTargetClass{" + "intField=" + intField + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
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 + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
package org.baeldung.gson.deserialization.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
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.junit.Test;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
public class GsonDeserializationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenUsingCustomDeserializer_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);
|
||||||
|
|
||||||
|
assertEquals(targetObject.intValue, 7);
|
||||||
|
assertEquals(targetObject.stringValue, "seven");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonWithArray_whenUsingGsonCustomDeserializer_thenMapsToArrayList() {
|
||||||
|
// It is necessary to override the equals() method in SourceClass
|
||||||
|
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 List<SourceClass> targetList = Arrays.asList(gson.fromJson(jsonSourceObject, SourceClass[].class));
|
||||||
|
|
||||||
|
assertEquals(new SourceClass(1, "one"), targetList.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenDeserializingManualy_thenCorrect() {
|
||||||
|
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
|
||||||
|
final JsonParser jParser = new JsonParser();
|
||||||
|
final JsonElement jElement = jParser.parse(jsonSourceObject);
|
||||||
|
final JsonObject jObject = jElement.getAsJsonObject();
|
||||||
|
final int intValue = jObject.get("valueInt").getAsInt();
|
||||||
|
final String stringValue = jObject.get("valueString").getAsString();
|
||||||
|
|
||||||
|
final TargetClass targetObject = new TargetClass(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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,33 +0,0 @@
|
|||||||
package org.baeldung.gson.deserialization.test;
|
|
||||||
|
|
||||||
import org.baeldung.gson.deserialization.GenericSourceClass;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
|
|
||||||
public class GsonDeserializationUnitTest {
|
|
||||||
|
|
||||||
private Gson gson;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public final void before() {
|
|
||||||
gson = new Gson();
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
|
|
||||||
final java.lang.reflect.Type genericSourceClassType = new TypeToken<GenericSourceClass>() {
|
|
||||||
}.getType();
|
|
||||||
final GenericSourceClass sourceObject = new GenericSourceClass(1);
|
|
||||||
final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType);
|
|
||||||
|
|
||||||
final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType);
|
|
||||||
|
|
||||||
System.out.println(targetObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user