gson work

This commit is contained in:
eugenp 2014-08-23 13:26:09 +03:00
parent 2e48d26ff5
commit c00ef532c9
3 changed files with 41 additions and 29 deletions

View File

@ -9,6 +9,10 @@ public class Foo {
this.stringValue = stringValue;
}
public Foo() {
super();
}
// API
@Override

View File

@ -1,18 +1,18 @@
package org.baeldung.gson.deserialization;
public class GenericFoo<INTEGER> {
public class GenericFoo<T> {
public INTEGER intField;
public T theValue;
GenericFoo(final INTEGER value) {
intField = value;
public GenericFoo(final T value) {
theValue = value;
}
//
@Override
public String toString() {
return "GenericTargetClass{" + "intField=" + intField + '}';
public final String toString() {
return "GenericTargetClass{" + "intField=" + theValue + '}';
}
}

View File

@ -8,15 +8,14 @@ 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.Foo;
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import org.baeldung.gson.deserialization.GenericFoo;
import org.junit.Test;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
@ -29,57 +28,66 @@ public class GsonDeserializationTest {
// tests - single element
@Test
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);
public final void whenDeserializingToSimpleObject_thenCorrect() {
final String json = "{\"intValue\":1,\"stringValue\":\"one\"}";
final Foo targetObject = new Gson().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "one");
}
@Test
public final void givenJsonHasNonMatchingFieldNames_whenDeserializingWithCustomDeserializer_thenCorrect() {
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect() {
final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}";
final Foo targetObject = new Gson().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "one");
}
@Test
public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect() {
final String json = "{\"valueInt\":7,\"valueString\":\"seven\"}";
final GsonBuilder gsonBldr = new GsonBuilder();
gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields());
final Foo targetObject = gsonBldr.create().fromJson(jsonSourceObject, Foo.class);
final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 7);
assertEquals(targetObject.stringValue, "seven");
}
@Test
public final void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
final Type genericTargetClassType = new TypeToken<GenericFoo<Integer>>() {
public final void whenDeserializingToGenericObject_thenCorrect() {
final Type typeToken = new TypeToken<GenericFoo<Integer>>() {
}.getType();
final String serializedSourceObject = "{\"intField\":1}";
final String json = "{\"theValue\":1}";
final GenericFoo<Integer> targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType);
final GenericFoo<Integer> targetObject = new Gson().fromJson(json, typeToken);
assertEquals(targetObject.intField, new Integer(1));
assertEquals(targetObject.theValue, 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 Foo[] objectsAsArray = new GsonBuilder().create().fromJson(jsonSourceObject, Foo[].class);
final List<Foo> targetList = Arrays.asList(objectsAsArray);
public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() {
final String json = "[{\"intValue\":1,\"stringValue\":\"one\"}," + "{\"intValue\":2,\"stringValue\":\"two\"}]";
final Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class);
assertThat(targetList, hasItem(new Foo(1, "one")));
assertThat(targetList, hasItem(new Foo(2, "two")));
assertThat(targetList, not(hasItem(new Foo(1, "two"))));
assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one")));
assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two")));
assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two"))));
}
@Test
public final void givenUsingGson_whenDeserializingCollection_thenCorrect() {
final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
public final void givenJsonArrayOfFoos_whenDeserializingCollection_thenCorrect() {
final String json = "[{\"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);
final Collection<Foo> targetCollection = new Gson().fromJson(json, targetClassType);
assertThat(targetCollection, instanceOf(ArrayList.class));
}