|
| 1 | +package org.baeldung.jackson.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.containsString; |
| 4 | +import static org.hamcrest.Matchers.not; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 12 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 13 | +import com.fasterxml.jackson.core.JsonParseException; |
| 14 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 15 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 16 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 17 | +import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; |
| 18 | +import com.fasterxml.jackson.databind.ser.FilterProvider; |
| 19 | +import com.fasterxml.jackson.databind.ser.PropertyFilter; |
| 20 | +import com.fasterxml.jackson.databind.ser.PropertyWriter; |
| 21 | +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
| 22 | +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
| 23 | + |
| 24 | +import dtos.MyDto; |
| 25 | +import dtos.MyDtoIgnoreField; |
| 26 | +import dtos.MyDtoIgnoreFieldByName; |
| 27 | +import dtos.MyDtoIgnoreNull; |
| 28 | +import dtos.MyDtoIncludeNonDefault; |
| 29 | +import dtos.MyDtoWithFilter; |
| 30 | +import dtos.MyMixInForString; |
| 31 | + |
| 32 | +public class JacksonSerializationIgnoreUnitTest { |
| 33 | + |
| 34 | + // tests - single entity to json |
| 35 | + |
| 36 | + // ignore |
| 37 | + |
| 38 | + @Test |
| 39 | + public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_whenSerializing_thenCorrect() throws JsonParseException, IOException { |
| 40 | + final ObjectMapper mapper = new ObjectMapper(); |
| 41 | + final String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault()); |
| 42 | + |
| 43 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 44 | + System.out.println(dtoAsString); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect() throws JsonParseException, IOException { |
| 49 | + final ObjectMapper mapper = new ObjectMapper(); |
| 50 | + final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault(); |
| 51 | + dtoObject.setBooleanValue(true); |
| 52 | + |
| 53 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 54 | + |
| 55 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 56 | + System.out.println(dtoAsString); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 61 | + final ObjectMapper mapper = new ObjectMapper(); |
| 62 | + final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName(); |
| 63 | + dtoObject.setBooleanValue(true); |
| 64 | + |
| 65 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 66 | + |
| 67 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 68 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 69 | + System.out.println(dtoAsString); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 74 | + final ObjectMapper mapper = new ObjectMapper(); |
| 75 | + final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField(); |
| 76 | + |
| 77 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 78 | + |
| 79 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 80 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 81 | + System.out.println(dtoAsString); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 86 | + final ObjectMapper mapper = new ObjectMapper(); |
| 87 | + mapper.addMixInAnnotations(String.class, MyMixInForString.class); |
| 88 | + final MyDto dtoObject = new MyDto(); |
| 89 | + dtoObject.setBooleanValue(true); |
| 90 | + |
| 91 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 92 | + |
| 93 | + assertThat(dtoAsString, containsString("intValue")); |
| 94 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 95 | + assertThat(dtoAsString, not(containsString("stringValue"))); |
| 96 | + System.out.println(dtoAsString); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 101 | + final ObjectMapper mapper = new ObjectMapper(); |
| 102 | + final SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue"); |
| 103 | + final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter); |
| 104 | + |
| 105 | + final MyDtoWithFilter dtoObject = new MyDtoWithFilter(); |
| 106 | + dtoObject.setIntValue(12); |
| 107 | + |
| 108 | + final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject); |
| 109 | + |
| 110 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 111 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 112 | + assertThat(dtoAsString, containsString("stringValue")); |
| 113 | + System.out.println(dtoAsString); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 118 | + final PropertyFilter theFilter = new SimpleBeanPropertyFilter() { |
| 119 | + @Override |
| 120 | + public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception { |
| 121 | + if (include(writer)) { |
| 122 | + if (!writer.getName().equals("intValue")) { |
| 123 | + writer.serializeAsField(pojo, jgen, provider); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + final int intValue = ((MyDtoWithFilter) pojo).getIntValue(); |
| 128 | + if (intValue >= 0) { |
| 129 | + writer.serializeAsField(pojo, jgen, provider); |
| 130 | + } |
| 131 | + } else if (!jgen.canOmitFields()) { // since 2.3 |
| 132 | + writer.serializeAsOmittedField(pojo, jgen, provider); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected final boolean include(final BeanPropertyWriter writer) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected final boolean include(final PropertyWriter writer) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + }; |
| 146 | + final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter); |
| 147 | + |
| 148 | + final MyDtoWithFilter dtoObject = new MyDtoWithFilter(); |
| 149 | + dtoObject.setIntValue(-1); |
| 150 | + |
| 151 | + final ObjectMapper mapper = new ObjectMapper(); |
| 152 | + final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject); |
| 153 | + |
| 154 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 155 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 156 | + assertThat(dtoAsString, containsString("stringValue")); |
| 157 | + System.out.println(dtoAsString); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException { |
| 162 | + final ObjectMapper mapper = new ObjectMapper(); |
| 163 | + final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull(); |
| 164 | + |
| 165 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 166 | + |
| 167 | + assertThat(dtoAsString, containsString("intValue")); |
| 168 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 169 | + assertThat(dtoAsString, not(containsString("stringValue"))); |
| 170 | + System.out.println(dtoAsString); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException { |
| 175 | + final ObjectMapper mapper = new ObjectMapper(); |
| 176 | + mapper.setSerializationInclusion(Include.NON_NULL); |
| 177 | + final MyDto dtoObject = new MyDto(); |
| 178 | + |
| 179 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 180 | + |
| 181 | + assertThat(dtoAsString, containsString("intValue")); |
| 182 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 183 | + assertThat(dtoAsString, not(containsString("stringValue"))); |
| 184 | + System.out.println(dtoAsString); |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments