|
| 1 | +package org.baeldung.jackson.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | + |
| 8 | +import org.baeldung.jackson.dynamicIgnore.Address; |
| 9 | +import org.baeldung.jackson.dynamicIgnore.Hidable; |
| 10 | +import org.baeldung.jackson.dynamicIgnore.HidableSerializer; |
| 11 | +import org.baeldung.jackson.dynamicIgnore.Person; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 16 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 17 | +import com.fasterxml.jackson.databind.BeanDescription; |
| 18 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.fasterxml.jackson.databind.SerializationConfig; |
| 21 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 22 | +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; |
| 23 | + |
| 24 | +public class JacksonDynamicIgnoreTest { |
| 25 | + |
| 26 | + private ObjectMapper mapper = new ObjectMapper(); |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + mapper.setSerializationInclusion(Include.NON_EMPTY); |
| 31 | + mapper.registerModule(new SimpleModule() { |
| 32 | + @Override |
| 33 | + public void setupModule(final SetupContext context) { |
| 34 | + super.setupModule(context); |
| 35 | + context.addBeanSerializerModifier(new BeanSerializerModifier() { |
| 36 | + @Override |
| 37 | + public JsonSerializer<?> modifySerializer(final SerializationConfig config, final BeanDescription beanDesc, final JsonSerializer<?> serializer) { |
| 38 | + if (Hidable.class.isAssignableFrom(beanDesc.getBeanClass())) { |
| 39 | + return new HidableSerializer((JsonSerializer<Object>) serializer); |
| 40 | + } |
| 41 | + return serializer; |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void whenNotHidden_thenCorrect() throws JsonProcessingException { |
| 50 | + final Address ad = new Address("ny", "usa", false); |
| 51 | + final Person person = new Person("john", ad, false); |
| 52 | + final String result = mapper.writeValueAsString(person); |
| 53 | + |
| 54 | + assertTrue(result.contains("name")); |
| 55 | + assertTrue(result.contains("john")); |
| 56 | + assertTrue(result.contains("address")); |
| 57 | + assertTrue(result.contains("usa")); |
| 58 | + |
| 59 | + System.out.println("Not Hidden = " + result); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenAddressHidden_thenCorrect() throws JsonProcessingException { |
| 64 | + final Address ad = new Address("ny", "usa", true); |
| 65 | + final Person person = new Person("john", ad, false); |
| 66 | + final String result = mapper.writeValueAsString(person); |
| 67 | + |
| 68 | + assertTrue(result.contains("name")); |
| 69 | + assertTrue(result.contains("john")); |
| 70 | + assertFalse(result.contains("address")); |
| 71 | + assertFalse(result.contains("usa")); |
| 72 | + |
| 73 | + System.out.println("Address Hidden = " + result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void whenAllHidden_thenCorrect() throws JsonProcessingException { |
| 78 | + final Address ad = new Address("ny", "usa", false); |
| 79 | + final Person person = new Person("john", ad, true); |
| 80 | + final String result = mapper.writeValueAsString(person); |
| 81 | + |
| 82 | + assertTrue(result.length() == 0); |
| 83 | + |
| 84 | + System.out.println("All Hidden = " + result); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void whenSerializeList_thenCorrect() throws JsonProcessingException { |
| 89 | + final Address ad1 = new Address("tokyo", "jp", true); |
| 90 | + final Address ad2 = new Address("london", "uk", false); |
| 91 | + final Address ad3 = new Address("ny", "usa", false); |
| 92 | + final Person p1 = new Person("john", ad1, false); |
| 93 | + final Person p2 = new Person("tom", ad2, true); |
| 94 | + final Person p3 = new Person("adam", ad3, false); |
| 95 | + |
| 96 | + final String result = mapper.writeValueAsString(Arrays.asList(p1, p2, p3)); |
| 97 | + |
| 98 | + System.out.println(result); |
| 99 | + } |
| 100 | +} |
0 commit comments