|
4 | 4 | import java.util.*;
|
5 | 5 |
|
6 | 6 | import com.fasterxml.jackson.annotation.*;
|
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.core.JsonProcessingException; |
7 | 11 | import com.fasterxml.jackson.core.type.TypeReference;
|
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.DeserializationContext; |
8 | 14 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 15 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 16 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 17 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 18 | +import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; |
| 19 | +import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; |
9 | 20 |
|
10 | 21 | public class OptionalTest extends ModuleTestBase
|
11 | 22 | {
|
@@ -92,6 +103,39 @@ public BooleanBean(Boolean b) {
|
92 | 103 | }
|
93 | 104 | }
|
94 | 105 |
|
| 106 | + static class CaseChangingStringWrapper { |
| 107 | + @JsonSerialize(contentUsing=UpperCasingSerializer.class) |
| 108 | + @JsonDeserialize(contentUsing=LowerCasingDeserializer.class) |
| 109 | + public Optional<String> value; |
| 110 | + |
| 111 | + CaseChangingStringWrapper() { } |
| 112 | + public CaseChangingStringWrapper(String s) { value = Optional.ofNullable(s); } |
| 113 | + } |
| 114 | + |
| 115 | + @SuppressWarnings("serial") |
| 116 | + public static class UpperCasingSerializer extends StdScalarSerializer<String> |
| 117 | + { |
| 118 | + public UpperCasingSerializer() { super(String.class); } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void serialize(String value, JsonGenerator gen, |
| 122 | + SerializerProvider provider) throws IOException { |
| 123 | + gen.writeString(value.toUpperCase()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @SuppressWarnings("serial") |
| 128 | + public static class LowerCasingDeserializer extends StdScalarDeserializer<String> |
| 129 | + { |
| 130 | + public LowerCasingDeserializer() { super(String.class); } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String deserialize(JsonParser p, DeserializationContext ctxt) |
| 134 | + throws IOException, JsonProcessingException { |
| 135 | + return p.getText().toLowerCase(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
95 | 139 | private ObjectMapper MAPPER;
|
96 | 140 |
|
97 | 141 | @Override
|
@@ -249,7 +293,21 @@ public void testBoolean() throws Exception
|
249 | 293 | assertNotNull(b.value);
|
250 | 294 | assertFalse(b.value.isPresent());
|
251 | 295 | }
|
252 |
| - |
| 296 | + |
| 297 | + public void testWithCustomDeserializer() throws Exception |
| 298 | + { |
| 299 | + CaseChangingStringWrapper w = MAPPER.readValue(aposToQuotes("{'value':'FoobaR'}"), |
| 300 | + CaseChangingStringWrapper.class); |
| 301 | + assertEquals("foobar", w.value.get()); |
| 302 | + } |
| 303 | + |
| 304 | + public void testCustomSerializer() throws Exception |
| 305 | + { |
| 306 | + final String VALUE = "fooBAR"; |
| 307 | + String json = MAPPER.writeValueAsString(new CaseChangingStringWrapper(VALUE)); |
| 308 | + assertEquals(json, aposToQuotes("{'value':'FOOBAR'}")); |
| 309 | + } |
| 310 | + |
253 | 311 | /*
|
254 | 312 | /**********************************************************
|
255 | 313 | /* Helper methods
|
|
0 commit comments