gson cleanup
This commit is contained in:
parent
2b879ce8c3
commit
782216f214
|
@ -16,5 +16,4 @@ public class IgnoringFieldsSerializer implements JsonSerializer<SourceClass> {
|
|||
|
||||
return jObject;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,9 +33,7 @@ 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);
|
||||
final String jsonCollection = new Gson().toJson(sourceCollection);
|
||||
|
||||
final String expectedResult = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
|
||||
assertEquals(expectedResult, jsonCollection);
|
||||
|
@ -63,19 +61,6 @@ public class GsonSerializationTest {
|
|||
assertEquals(expectedResult, 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();
|
||||
String jsonDate = gson.toJson(sourceDate, sourceDateType);
|
||||
|
||||
System.out.println("jsonDate:\n" + jsonDate);
|
||||
final String expectedResult = "\"Jan 1, 1970 3:16:40 AM\"";
|
||||
assertTrue(jsonDate.equals(expectedResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCustomDeserializer_whenFieldNotMatchesCriteria_thenIgnored() {
|
||||
final SourceClass sourceObject = new SourceClass(-1, "minus 1");
|
||||
|
@ -84,10 +69,23 @@ public class GsonSerializationTest {
|
|||
final Gson gson = gsonBuildr.create();
|
||||
final Type sourceObjectType = new TypeToken<SourceClass>() {
|
||||
}.getType();
|
||||
String jsonString = gson.toJson(sourceObject, sourceObjectType);
|
||||
final String jsonString = gson.toJson(sourceObject, sourceObjectType);
|
||||
|
||||
final String expectedResult = "{\"stringValue\":\"minus 1\"}";
|
||||
assertEquals(expectedResult, 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);
|
||||
|
||||
System.out.println("jsonDate:\n" + jsonDate);
|
||||
final String expectedResult = "\"Jan 1, 1970 3:16:40 AM\"";
|
||||
assertTrue(jsonDate.equals(expectedResult));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package org.baeldung.gson;
|
||||
|
||||
public class GenericSourceClass {
|
||||
int intField;
|
||||
|
||||
public GenericSourceClass(final int i) {
|
||||
intField = i;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package org.baeldung.gson;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
public class GsonDeserializationUnitTest {
|
||||
|
||||
private Gson gson;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
|
||||
final java.lang.reflect.Type genericSourceClassType = new TypeToken<GenericSourceClass>() {
|
||||
}.getType();
|
||||
final GenericSourceClass sourceObject = new GenericSourceClass(1);
|
||||
final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType);
|
||||
|
||||
final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType);
|
||||
|
||||
System.out.println(targetObject);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue