gson serialization work

This commit is contained in:
eugenp 2014-09-05 20:29:14 +03:00
parent 0b28b4b983
commit 9254032a03
4 changed files with 154 additions and 0 deletions

View File

@ -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<SourceClass> sourceCollection = Lists.newArrayList(new SourceClass(1, "one"), new SourceClass(2, "two"));
final Type sourceCollectionType = new TypeToken<Collection<SourceClass>>() {
}.getType();
final String jsonCollection = new Gson().toJson(sourceCollection, sourceCollectionType);
// test
final Collection<SourceClass> 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);
}
}

View File

@ -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 + '\'' + '}';
}
}

View File

@ -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<SourceClass> {
@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;
}
}

View File

@ -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<SourceClass> {
@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;
}
}