jackson skip conditionally
This commit is contained in:
parent
ab4b2d8a1a
commit
1ba1bf2bc5
|
@ -0,0 +1,41 @@
|
|||
package org.baeldung.jackson.dynamicIgnore;
|
||||
|
||||
|
||||
public class Address implements Hidable {
|
||||
private String city;
|
||||
private String country;
|
||||
private boolean hidden;
|
||||
|
||||
public Address(final String city, final String country, final boolean hidden) {
|
||||
super();
|
||||
this.city = city;
|
||||
this.country = country;
|
||||
this.hidden = hidden;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(final String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(final String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHidden() {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
public void setHidden(final boolean hidden) {
|
||||
this.hidden = hidden;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.jackson.dynamicIgnore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
|
||||
@JsonIgnoreProperties("hidden")
|
||||
public interface Hidable {
|
||||
boolean isHidden();
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.jackson.dynamicIgnore;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
public class HidableSerializer extends JsonSerializer<Hidable> {
|
||||
|
||||
private JsonSerializer<Object> defaultSerializer;
|
||||
|
||||
public HidableSerializer(final JsonSerializer<Object> serializer) {
|
||||
defaultSerializer = serializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Hidable value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
if (value.isHidden())
|
||||
return;
|
||||
defaultSerializer.serialize(value, jgen, provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty(final SerializerProvider provider, final Hidable value) {
|
||||
return (value == null || value.isHidden());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package org.baeldung.jackson.dynamicIgnore;
|
||||
|
||||
|
||||
public class Person implements Hidable {
|
||||
private String name;
|
||||
private Address address;
|
||||
private boolean hidden;
|
||||
|
||||
public Person(final String name, final Address address, final boolean hidden) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.hidden = hidden;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(final Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
@Override
|
||||
public boolean isHidden() {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
public void setHidden(final boolean hidden) {
|
||||
this.hidden = hidden;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package org.baeldung.jackson.test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.baeldung.jackson.dynamicIgnore.Address;
|
||||
import org.baeldung.jackson.dynamicIgnore.Hidable;
|
||||
import org.baeldung.jackson.dynamicIgnore.HidableSerializer;
|
||||
import org.baeldung.jackson.dynamicIgnore.Person;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||
|
||||
public class JacksonDynamicIgnoreTest {
|
||||
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mapper.setSerializationInclusion(Include.NON_EMPTY);
|
||||
mapper.registerModule(new SimpleModule() {
|
||||
@Override
|
||||
public void setupModule(final SetupContext context) {
|
||||
super.setupModule(context);
|
||||
context.addBeanSerializerModifier(new BeanSerializerModifier() {
|
||||
@Override
|
||||
public JsonSerializer<?> modifySerializer(final SerializationConfig config, final BeanDescription beanDesc, final JsonSerializer<?> serializer) {
|
||||
if (Hidable.class.isAssignableFrom(beanDesc.getBeanClass())) {
|
||||
return new HidableSerializer((JsonSerializer<Object>) serializer);
|
||||
}
|
||||
return serializer;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotHidden_thenCorrect() throws JsonProcessingException {
|
||||
final Address ad = new Address("ny", "usa", false);
|
||||
final Person person = new Person("john", ad, false);
|
||||
final String result = mapper.writeValueAsString(person);
|
||||
|
||||
assertTrue(result.contains("name"));
|
||||
assertTrue(result.contains("john"));
|
||||
assertTrue(result.contains("address"));
|
||||
assertTrue(result.contains("usa"));
|
||||
|
||||
System.out.println("Not Hidden = " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddressHidden_thenCorrect() throws JsonProcessingException {
|
||||
final Address ad = new Address("ny", "usa", true);
|
||||
final Person person = new Person("john", ad, false);
|
||||
final String result = mapper.writeValueAsString(person);
|
||||
|
||||
assertTrue(result.contains("name"));
|
||||
assertTrue(result.contains("john"));
|
||||
assertFalse(result.contains("address"));
|
||||
assertFalse(result.contains("usa"));
|
||||
|
||||
System.out.println("Address Hidden = " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAllHidden_thenCorrect() throws JsonProcessingException {
|
||||
final Address ad = new Address("ny", "usa", false);
|
||||
final Person person = new Person("john", ad, true);
|
||||
final String result = mapper.writeValueAsString(person);
|
||||
|
||||
assertTrue(result.length() == 0);
|
||||
|
||||
System.out.println("All Hidden = " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializeList_thenCorrect() throws JsonProcessingException {
|
||||
final Address ad1 = new Address("tokyo", "jp", true);
|
||||
final Address ad2 = new Address("london", "uk", false);
|
||||
final Address ad3 = new Address("ny", "usa", false);
|
||||
final Person p1 = new Person("john", ad1, false);
|
||||
final Person p2 = new Person("tom", ad2, true);
|
||||
final Person p3 = new Person("adam", ad3, false);
|
||||
|
||||
final String result = mapper.writeValueAsString(Arrays.asList(p1, p2, p3));
|
||||
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue