From 9254032a037ca803396b33547a6cb3c18cbfc60f Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 5 Sep 2014 20:29:14 +0300 Subject: [PATCH] gson serialization work --- .../serialization/GsonSerializationTest.java | 64 +++++++++++++++++++ .../gson/serialization/SourceClass.java | 45 +++++++++++++ ...urceClassChangingFieldNamesSerializer.java | 24 +++++++ ...rceClassIgnoringExtraFieldsSerializer.java | 21 ++++++ 4 files changed, 154 insertions(+) create mode 100644 gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java create mode 100644 gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java create mode 100644 gson/src/test/java/org/baeldung/gson/serialization/SourceClassIgnoringExtraFieldsSerializer.java diff --git a/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java b/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java new file mode 100644 index 0000000000..a0fd3c0ff9 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java @@ -0,0 +1,64 @@ +package org.baeldung.gson.serialization; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Type; +import java.util.Collection; + +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +public class GsonSerializationTest { + + @Test + public void givenCollection_whenSerializing_thenCorrect() { + final Collection sourceCollection = Lists.newArrayList(new SourceClass(1, "one"), new SourceClass(2, "two")); + final Type sourceCollectionType = new TypeToken>() { + }.getType(); + final String jsonCollection = new Gson().toJson(sourceCollection, sourceCollectionType); + + // test + final Collection testCollection = new Gson().fromJson(jsonCollection, sourceCollectionType); + assertEquals(sourceCollection, testCollection); + } + + @Test + public void givenArrayOfObjects_whenSerializing_thenMapToJsonCollection() { + final SourceClass[] sourceArray = { new SourceClass(1, "one"), new SourceClass(2, "two") }; + final String jsonCollection = new Gson().toJson(sourceArray); + + // test + final String estimatedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + assertEquals(estimatedResult, jsonCollection); + } + + @Test + public void givenUsingCustomSerializer_whenSerializingObjectToJsonWithDissimilarFieldNames_thenCorrect() { + final SourceClass sourceObject = new SourceClass(7, "seven"); + final GsonBuilder gsonBuildr = new GsonBuilder(); + gsonBuildr.registerTypeAdapter(SourceClass.class, new SourceClassChangingFieldNamesSerializer()); + final Gson gson = gsonBuildr.create(); + final String jsonString = gson.toJson(sourceObject); + + // test + final String estimatedResult = "{\"otherIntValue\":7,\"otherStringValue\":\"seven\"}"; + assertEquals(estimatedResult, jsonString); + } + + @Test + public void givenUsingCustomSerializer_whenSerializingObject_thenFieldIgnored() { + final SourceClass sourceObject = new SourceClass(7, "seven"); + final GsonBuilder gsonBuildr = new GsonBuilder(); + gsonBuildr.registerTypeAdapter(SourceClass.class, new SourceClassIgnoringExtraFieldsSerializer()); + final Gson gson = gsonBuildr.create(); + final String jsonString = gson.toJson(sourceObject); + + // test + final String estimatedResult = "{\"intValue\":7}"; + assertEquals(estimatedResult, jsonString); + } +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java b/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java new file mode 100644 index 0000000000..27f9192ab8 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java @@ -0,0 +1,45 @@ +package org.baeldung.gson.serialization; + +public class SourceClass { + public int intValue; + public String stringValue; + + public SourceClass(final int intValue, final String stringValue) { + this.intValue = intValue; + this.stringValue = stringValue; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + intValue; + result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final SourceClass other = (SourceClass) obj; + if (intValue != other.intValue) + return false; + if (stringValue == null) { + if (other.stringValue != null) + return false; + } else if (!stringValue.equals(other.stringValue)) + return false; + return true; + } + + @Override + public String toString() { + return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java new file mode 100644 index 0000000000..b15a9aa4e5 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java @@ -0,0 +1,24 @@ +package org.baeldung.gson.serialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public class SourceClassChangingFieldNamesSerializer implements JsonSerializer { + + @Override + public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) { + final String otherIntValueName = "otherIntValue"; + final String otherStringValueName = "otherStringValue"; + + final JsonObject jObject = new JsonObject(); + jObject.addProperty(otherIntValueName, src.intValue); + jObject.addProperty(otherStringValueName, src.stringValue); + + return jObject; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/serialization/SourceClassIgnoringExtraFieldsSerializer.java b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassIgnoringExtraFieldsSerializer.java new file mode 100644 index 0000000000..fd4bb64a3a --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassIgnoringExtraFieldsSerializer.java @@ -0,0 +1,21 @@ +package org.baeldung.gson.serialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public class SourceClassIgnoringExtraFieldsSerializer implements JsonSerializer { + + @Override + public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) { + final String intValue = "intValue"; + final JsonObject jObject = new JsonObject(); + jObject.addProperty(intValue, src.intValue); + + return jObject; + } + +}