gson cleanup work
This commit is contained in:
parent
e8a6b469f6
commit
42d17c1c4d
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>jackson</name>
|
||||
<name>gson</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
package org.baeldung.gson.serialization;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
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 String estimatedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
assertEquals(estimatedResult, jsonCollection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfObjects_whenSerializing_thenCorrect() {
|
||||
final SourceClass[] sourceArray = { new SourceClass(1, "one"), new SourceClass(2, "two") };
|
||||
final String jsonString = new Gson().toJson(sourceArray);
|
||||
|
||||
// test
|
||||
final String estimatedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
assertEquals(estimatedResult, jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCustomSerializer_whenChangingNameOfFieldOnSerializing_thenCorrect() {
|
||||
final SourceClass sourceObject = new SourceClass(7, "seven");
|
||||
final GsonBuilder gsonBuildr = new GsonBuilder();
|
||||
gsonBuildr.registerTypeAdapter(SourceClass.class, new DifferentNameSerializer());
|
||||
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 givenIgnoringAField_whenSerializingWithCustomSerializer_thenFieldIgnored() {
|
||||
final SourceClass sourceObject = new SourceClass(7, "seven");
|
||||
final GsonBuilder gsonBuildr = new GsonBuilder();
|
||||
gsonBuildr.registerTypeAdapter(SourceClass.class, new IgnoringFieldsSerializer());
|
||||
final Gson gson = gsonBuildr.create();
|
||||
final String jsonString = gson.toJson(sourceObject);
|
||||
|
||||
// test
|
||||
final String estimatedResult = "{\"intValue\":7}";
|
||||
assertEquals(estimatedResult, jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenSerializing_thenCorrect() {
|
||||
final Date sourceDate = new Date(1000000L);
|
||||
final Gson gson = new Gson();
|
||||
final Type sourceDateType = new TypeToken<Date>() {
|
||||
}.getType();
|
||||
final String jsonDate = gson.toJson(sourceDate, sourceDateType);
|
||||
|
||||
// test
|
||||
final String estimatedResult = "\"Jan 1, 1970 3:16:40 AM\"";
|
||||
assertTrue(jsonDate.equals(estimatedResult));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package org.baeldung.gson.serialization;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class IgnoringFieldsNotMatchingCriteriaSerializer implements JsonSerializer<SourceClass> {
|
||||
@Override
|
||||
public JsonElement serialize(SourceClass src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject jObject = new JsonObject();
|
||||
|
||||
// Criteria: intValue >= 0
|
||||
if (src.getIntValue() >= 0) {
|
||||
String intValue = "intValue";
|
||||
jObject.addProperty(intValue, src.getIntValue());
|
||||
}
|
||||
|
||||
String stringValue = "stringValue";
|
||||
jObject.addProperty(stringValue, src.getStringValue());
|
||||
|
||||
return jObject;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package org.baeldung.gson.serialization.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import org.baeldung.gson.serialization.DifferentNameSerializer;
|
||||
import org.baeldung.gson.serialization.IgnoringFieldsNotMatchingCriteriaSerializer;
|
||||
import org.baeldung.gson.serialization.IgnoringFieldsSerializer;
|
||||
import org.baeldung.gson.serialization.SourceClass;
|
||||
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 givenArrayOfObjects_whenSerializing_thenCorrect() {
|
||||
final SourceClass[] sourceArray = { new SourceClass(1, "one"), new SourceClass(2, "two") };
|
||||
final String jsonString = new Gson().toJson(sourceArray);
|
||||
|
||||
// test
|
||||
final String expectedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
assertEquals(expectedResult, jsonString);
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
final String expectedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
assertEquals(expectedResult, jsonCollection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCustomSerializer_whenChangingNameOfFieldOnSerializing_thenCorrect() {
|
||||
final SourceClass sourceObject = new SourceClass(7, "seven");
|
||||
final GsonBuilder gsonBuildr = new GsonBuilder();
|
||||
gsonBuildr.registerTypeAdapter(SourceClass.class, new DifferentNameSerializer());
|
||||
final String jsonString = gsonBuildr.create().toJson(sourceObject);
|
||||
|
||||
final String expectedResult = "{\"otherIntValue\":7,\"otherStringValue\":\"seven\"}";
|
||||
assertEquals(expectedResult, jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIgnoringAField_whenSerializingWithCustomSerializer_thenFieldIgnored() {
|
||||
final SourceClass sourceObject = new SourceClass(7, "seven");
|
||||
final GsonBuilder gsonBuildr = new GsonBuilder();
|
||||
gsonBuildr.registerTypeAdapter(SourceClass.class, new IgnoringFieldsSerializer());
|
||||
final String jsonString = gsonBuildr.create().toJson(sourceObject);
|
||||
|
||||
final String expectedResult = "{\"intValue\":7}";
|
||||
assertEquals(expectedResult, jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenSerializing_thenCorrect() {
|
||||
Date sourceDate = new Date(1000000L);
|
||||
Gson gson = new Gson();
|
||||
Type sourceDateType = new TypeToken<Date>() {
|
||||
}.getType();
|
||||
String jsonDate = gson.toJson(sourceDate, sourceDateType);
|
||||
// test
|
||||
System.out.println("jsonDate:\n" + jsonDate);
|
||||
String estimatedResult = "\"Jan 1, 1970 3:16:40 AM\"";
|
||||
assertTrue(jsonDate.equals(estimatedResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCustomDeserializer_whenFieldNotMatchesCriteria_thenIgnoringIt() {
|
||||
SourceClass sourceObject = new SourceClass(-1, "minus 1");
|
||||
GsonBuilder gsonBuildr = new GsonBuilder();
|
||||
gsonBuildr.registerTypeAdapter(SourceClass.class, new IgnoringFieldsNotMatchingCriteriaSerializer());
|
||||
Gson gson = gsonBuildr.create();
|
||||
Type sourceObjectType = new TypeToken<SourceClass>() {
|
||||
}.getType();
|
||||
String jsonString = gson.toJson(sourceObject, sourceObjectType);
|
||||
// test
|
||||
String estimatedResult = "{\"stringValue\":\"minus 1\"}";
|
||||
assertEquals(estimatedResult, jsonString);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue