Skip to content

Commit

Permalink
gson work
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenp committed Aug 23, 2014
1 parent 2e48d26 commit c00ef53
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
4 changes: 4 additions & 0 deletions gson/src/test/java/org/baeldung/gson/deserialization/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public Foo(final int intValue, final String stringValue) {
this.stringValue = stringValue;
}

public Foo() {
super();
}

// API

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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 + '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

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;
Expand All @@ -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);

assertThat(targetList, hasItem(new Foo(1, "one")));
assertThat(targetList, hasItem(new Foo(2, "two")));
assertThat(targetList, not(hasItem(new Foo(1, "two"))));
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(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));
}

Expand Down

0 comments on commit c00ef53

Please sign in to comment.