|
| 1 | +package org.baeldung.gson.deserialization.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.instanceOf; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import org.baeldung.gson.deserialization.GenericTargetClass; |
| 14 | +import org.baeldung.gson.deserialization.SourceClass; |
| 15 | +import org.baeldung.gson.deserialization.SourceClassDeserializer; |
| 16 | +import org.baeldung.gson.deserialization.TargetClass; |
| 17 | +import org.baeldung.gson.deserialization.TargetClassDeserializer; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import com.google.gson.Gson; |
| 21 | +import com.google.gson.GsonBuilder; |
| 22 | +import com.google.gson.JsonElement; |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import com.google.gson.JsonParser; |
| 25 | +import com.google.gson.reflect.TypeToken; |
| 26 | + |
| 27 | +public class GsonDeserializationTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenUsingCustomDeserializer_thenCorrect() { |
| 31 | + final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; |
| 32 | + final GsonBuilder gsonBldr = new GsonBuilder(); |
| 33 | + gsonBldr.registerTypeAdapter(TargetClass.class, new TargetClassDeserializer()); |
| 34 | + final Gson gson = gsonBldr.create(); |
| 35 | + final TargetClass targetObject = gson.fromJson(jsonSourceObject, TargetClass.class); |
| 36 | + |
| 37 | + assertEquals(targetObject.intValue, 7); |
| 38 | + assertEquals(targetObject.stringValue, "seven"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenJsonWithArray_whenUsingGsonCustomDeserializer_thenMapsToArrayList() { |
| 43 | + // It is necessary to override the equals() method in SourceClass |
| 44 | + final String jsonSourceObject = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; |
| 45 | + final GsonBuilder gsonBldr = new GsonBuilder(); |
| 46 | + gsonBldr.registerTypeHierarchyAdapter(SourceClass[].class, new SourceClassDeserializer()); |
| 47 | + final Gson gson = gsonBldr.create(); |
| 48 | + |
| 49 | + final List<SourceClass> targetList = Arrays.asList(gson.fromJson(jsonSourceObject, SourceClass[].class)); |
| 50 | + |
| 51 | + assertEquals(new SourceClass(1, "one"), targetList.get(0)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenJsonHasDissimilarFieldNamesButGsonMapsRight_whenDeserializingManualy_thenCorrect() { |
| 56 | + final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; |
| 57 | + final JsonParser jParser = new JsonParser(); |
| 58 | + final JsonElement jElement = jParser.parse(jsonSourceObject); |
| 59 | + final JsonObject jObject = jElement.getAsJsonObject(); |
| 60 | + final int intValue = jObject.get("valueInt").getAsInt(); |
| 61 | + final String stringValue = jObject.get("valueString").getAsString(); |
| 62 | + |
| 63 | + final TargetClass targetObject = new TargetClass(intValue, stringValue); |
| 64 | + |
| 65 | + assertEquals(targetObject.intValue, 7); |
| 66 | + assertEquals(targetObject.stringValue, "seven"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect() { |
| 71 | + final String serializedSourceObject = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; |
| 72 | + final TargetClass targetObject = new Gson().fromJson(serializedSourceObject, TargetClass.class); |
| 73 | + |
| 74 | + assertEquals(targetObject.intValue, 1); |
| 75 | + assertEquals(targetObject.stringValue, "one"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { |
| 80 | + final Type genericTargetClassType = new TypeToken<GenericTargetClass<Integer>>() { |
| 81 | + }.getType(); |
| 82 | + final String serializedSourceObject = "{\"intField\":1}"; |
| 83 | + |
| 84 | + final GenericTargetClass<Integer> targetObject = new Gson().fromJson(serializedSourceObject, genericTargetClassType); |
| 85 | + |
| 86 | + assertEquals(targetObject.intField, new Integer(1)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenUsingGson_whenDeserializingCollection_thenCorrect() { |
| 91 | + final String serializedSourceCollection = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; |
| 92 | + final Type targetClassType = new TypeToken<ArrayList<TargetClass>>() { |
| 93 | + }.getType(); |
| 94 | + |
| 95 | + final Collection<TargetClass> targetCollection = new Gson().fromJson(serializedSourceCollection, targetClassType); |
| 96 | + assertThat(targetCollection, instanceOf(ArrayList.class)); |
| 97 | + } |
| 98 | +} |
0 commit comments