|
1 | 1 | package com.fasterxml.jackson.datatype.jdk8;
|
2 | 2 |
|
| 3 | +import java.util.LinkedHashMap; |
| 4 | +import java.util.Map; |
3 | 5 | import java.util.Optional;
|
4 | 6 |
|
5 | 7 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
@@ -38,6 +40,15 @@ public static <T> OptionalGenericData<T> construct(T data) {
|
38 | 40 | }
|
39 | 41 | }
|
40 | 42 |
|
| 43 | + static final class OptMapBean { |
| 44 | + public Map<String, Optional<?>> values; |
| 45 | + |
| 46 | + public OptMapBean(String key, Optional<?> v) { |
| 47 | + values = new LinkedHashMap<>(); |
| 48 | + values.put(key, v); |
| 49 | + } |
| 50 | + } |
| 51 | + |
41 | 52 | /*
|
42 | 53 | /**********************************************************
|
43 | 54 | /* Test methods
|
@@ -118,4 +129,19 @@ public void testSerPropInclusionNonEmpty() throws Exception
|
118 | 129 | assertEquals("{\"myData\":true}",
|
119 | 130 | mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
|
120 | 131 | }
|
| 132 | + |
| 133 | + public void testMapElementInclusion() throws Exception |
| 134 | + { |
| 135 | + ObjectMapper mapper = mapperWithModule().setDefaultPropertyInclusion( |
| 136 | + JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_ABSENT)); |
| 137 | + // first: Absent entry/-ies should NOT be included |
| 138 | + assertEquals("{\"values\":{}}", |
| 139 | + mapper.writeValueAsString(new OptMapBean("key", Optional.empty()))); |
| 140 | + // but non-empty should |
| 141 | + assertEquals("{\"values\":{\"key\":\"value\"}}", |
| 142 | + mapper.writeValueAsString(new OptMapBean("key", Optional.of("value")))); |
| 143 | + // and actually even empty |
| 144 | + assertEquals("{\"values\":{\"key\":\"\"}}", |
| 145 | + mapper.writeValueAsString(new OptMapBean("key", Optional.of("")))); |
| 146 | + } |
121 | 147 | }
|
0 commit comments