parent
89d1127fb2
commit
a3563f0ef5
@ -1,4 +1,5 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -15,6 +16,12 @@
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.10</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
|
@ -0,0 +1,11 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface Exclude {
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MyClass {
|
||||
private long id;
|
||||
private String name;
|
||||
private String other;
|
||||
private MySubClass subclass;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MyClassWithAnnotatedFields {
|
||||
|
||||
@Expose
|
||||
private long id;
|
||||
@Expose
|
||||
private String name;
|
||||
private String other;
|
||||
@Expose
|
||||
private MySubClassWithAnnotatedFields subclass;
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MyClassWithCustomAnnotatedFields {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
@Exclude
|
||||
private String other;
|
||||
private MySubClassWithCustomAnnotatedFields subclass;
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MyClassWithTransientFields {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
private transient String other;
|
||||
private MySubClassWithTransientFields subclass;
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MySubClass {
|
||||
private long id;
|
||||
private String description;
|
||||
private String otherVerboseInfo;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MySubClassWithAnnotatedFields {
|
||||
|
||||
@Expose private long id;
|
||||
@Expose private String description;
|
||||
private String otherVerboseInfo;
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MySubClassWithCustomAnnotatedFields {
|
||||
|
||||
private long id;
|
||||
private String description;
|
||||
@Exclude
|
||||
private String otherVerboseInfo;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.baeldung.gson.serializationwithexclusions;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MySubClassWithTransientFields {
|
||||
|
||||
private long id;
|
||||
private String description;
|
||||
private transient String otherVerboseInfo;
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user