gson cleanup

This commit is contained in:
eugenp 2014-09-08 20:59:31 +03:00
parent eb3925fb0e
commit 895e273ec5
3 changed files with 7 additions and 6 deletions

View File

@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
public class SourceClassChangingFieldNamesSerializer implements JsonSerializer<SourceClass> { public class DifferentNameSerializer implements JsonSerializer<SourceClass> {
@Override @Override
public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) { public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) {

View File

@ -39,10 +39,10 @@ public class GsonSerializationTest {
} }
@Test @Test
public void givenUsingCustomSerializer_whenSerializingObjectToJsonWithDissimilarFieldNames_thenCorrect() { public void givenUsingCustomSerializer_whenChangingNameOfFieldOnSerializing_thenCorrect() {
final SourceClass sourceObject = new SourceClass(7, "seven"); final SourceClass sourceObject = new SourceClass(7, "seven");
final GsonBuilder gsonBuildr = new GsonBuilder(); final GsonBuilder gsonBuildr = new GsonBuilder();
gsonBuildr.registerTypeAdapter(SourceClass.class, new SourceClassChangingFieldNamesSerializer()); gsonBuildr.registerTypeAdapter(SourceClass.class, new DifferentNameSerializer());
final Gson gson = gsonBuildr.create(); final Gson gson = gsonBuildr.create();
final String jsonString = gson.toJson(sourceObject); final String jsonString = gson.toJson(sourceObject);
@ -52,10 +52,10 @@ public class GsonSerializationTest {
} }
@Test @Test
public void givenUsingCustomSerializer_whenSerializingObject_thenFieldIgnored() { public void givenIgnoringAField_whenSerializingWithCustomSerializer_thenFieldIgnored() {
final SourceClass sourceObject = new SourceClass(7, "seven"); final SourceClass sourceObject = new SourceClass(7, "seven");
final GsonBuilder gsonBuildr = new GsonBuilder(); final GsonBuilder gsonBuildr = new GsonBuilder();
gsonBuildr.registerTypeAdapter(SourceClass.class, new SourceClassIgnoringExtraFieldsSerializer()); gsonBuildr.registerTypeAdapter(SourceClass.class, new IgnoringFieldsSerializer());
final Gson gson = gsonBuildr.create(); final Gson gson = gsonBuildr.create();
final String jsonString = gson.toJson(sourceObject); final String jsonString = gson.toJson(sourceObject);
@ -76,4 +76,5 @@ public class GsonSerializationTest {
final String estimatedResult = "\"Jan 1, 1970 3:16:40 AM\""; final String estimatedResult = "\"Jan 1, 1970 3:16:40 AM\"";
assertTrue(jsonDate.equals(estimatedResult)); assertTrue(jsonDate.equals(estimatedResult));
} }
} }

View File

@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
public class SourceClassIgnoringExtraFieldsSerializer implements JsonSerializer<SourceClass> { public class IgnoringFieldsSerializer implements JsonSerializer<SourceClass> {
@Override @Override
public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) { public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) {
final String intValue = "intValue"; final String intValue = "intValue";