forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eugenp
committed
Aug 23, 2014
1 parent
01625ed
commit ade4f14
Showing
10 changed files
with
217 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
========= | ||
|
||
## Jackson Cookbooks and Examples | ||
## GSON Cookbooks and Examples | ||
|
||
### 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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
gson/src/test/java/org/baeldung/gson/deserialization/GenericTargetClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + '}'; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
gson/src/test/java/org/baeldung/gson/deserialization/SourceClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
gson/src/test/java/org/baeldung/gson/deserialization/SourceClassDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
gson/src/test/java/org/baeldung/gson/deserialization/TargetClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + '}'; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
gson/src/test/java/org/baeldung/gson/deserialization/TargetClassDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java
This file was deleted.
Oops, something went wrong.