* Also needed

* BAEL-1698

* BAEL-1698

* BAEL-1778
This commit is contained in:
Andrea Ligios
2018-05-27 22:49:31 +02:00
committed by maibin
parent 89d1127fb2
commit a3563f0ef5
11 changed files with 301 additions and 61 deletions
@@ -0,0 +1,104 @@
package org.baeldung.gson.serializationwithexclusions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class SerializationWithExclusionsUnitTest {
final String expectedResult = "{\"id\":1,\"name\":\"foo\",\"subclass\":{\"id\":42,\"description\":\"the answer\"}}";
@Test
public void givenClassWithTransientFields_whenSerializing_thenCorrectWithoutTransientFields() {
MyClassWithTransientFields source = new MyClassWithTransientFields(1L, "foo", "bar", new MySubClassWithTransientFields(42L, "the answer", "Verbose field which we don't want to be serialized"));
String jsonString = new Gson().toJson(source);
assertEquals(expectedResult, jsonString);
}
@Test
public void givenClassAnnotated_whenSerializing_thenCorrectWithoutNotAnnotatedFields() {
MyClassWithAnnotatedFields source = new MyClassWithAnnotatedFields(1L, "foo", "bar", new MySubClassWithAnnotatedFields(42L, "the answer", "Verbose field which we don't want to be serialized"));
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create();
String jsonString = gson.toJson(source);
assertEquals(expectedResult, jsonString);
}
@Test
public void givenExclusionStrategyByClassesAndFields_whenSerializing_thenFollowStrategy() {
MyClass source = new MyClass(1L, "foo", "bar", new MySubClass(42L, "the answer", "Verbose field which we don't want to be serialized"));
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes field) {
if (field.getDeclaringClass() == MyClass.class && field.getName()
.equals("other"))
return true;
if (field.getDeclaringClass() == MySubClass.class && field.getName()
.equals("otherVerboseInfo"))
return true;
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
};
Gson gson = new GsonBuilder().addSerializationExclusionStrategy(strategy)
.create();
String jsonString = gson.toJson(source);
assertEquals(expectedResult, jsonString);
}
@Test
public void givenExclusionStrategyByStartsWith_whenSerializing_thenFollowStrategy() {
MyClass source = new MyClass(1L, "foo", "bar", new MySubClass(42L, "the answer", "Verbose field which we don't want to be serialized"));
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes field) {
return field.getName().startsWith("other");
}
};
Gson gson = new GsonBuilder().setExclusionStrategies(strategy)
.create();
String jsonString = gson.toJson(source);
assertEquals(expectedResult, jsonString);
}
@Test
public void givenExclusionStrategyByCustomAnnotation_whenSerializing_thenFollowStrategy() {
MyClassWithCustomAnnotatedFields source = new MyClassWithCustomAnnotatedFields(1L, "foo", "bar", new MySubClassWithCustomAnnotatedFields(42L, "the answer", "Verbose field which we don't want to be serialized"));
ExclusionStrategy strategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes field) {
return field.getAnnotation(Exclude.class) != null;
}
};
Gson gson = new GsonBuilder().setExclusionStrategies(strategy)
.create();
String jsonString = gson.toJson(source);
assertEquals(expectedResult, jsonString);
}
}