Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 4fa3fcd

Browse files
committed
Fix #66
1 parent 55f90fb commit 4fa3fcd

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

release-notes/VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Project: jackson-datatype-guava
66

77
2.6.0 (not yet released)
88

9+
#66: Add `GuavaModule.configureAbsentsAsNulls(boolean)` to change whether
10+
`Optional.absent()` is to be handled same as Java null during serialization
11+
(default: true) or not.
12+
913
2.5.4 (not yet released)
1014

1115
2.5.3 (24-Apr-2015)

src/main/java/com/fasterxml/jackson/datatype/guava/GuavaModule.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@
88
public class GuavaModule extends Module // can't use just SimpleModule, due to generic types
99
{
1010
private final String NAME = "GuavaModule";
11+
12+
/**
13+
* Configuration setting that determines whether `Optional.absent()` is
14+
* considered "same as null" for serialization purposes; that is, to be
15+
* filtered same as nulls are.
16+
* If enabled, absent values are treated like nulls; if disabled, they are not.
17+
* In either case, absent values are always considered "empty".
18+
*<p>
19+
* Default value is `true` for backwards compatibility (2.5 and prior
20+
* only had this behavior).
21+
*<p>
22+
* Note that this setting MUST be changed BEFORE registering the module:
23+
* changes after registration will have no effect.
24+
*/
25+
protected boolean _cfgHandleAbsentAsNull = true;
1126

1227
public GuavaModule() {
1328
super();
@@ -22,9 +37,31 @@ public void setupModule(SetupContext context)
2237
context.addDeserializers(new GuavaDeserializers());
2338
context.addSerializers(new GuavaSerializers());
2439
context.addTypeModifier(new GuavaTypeModifier());
25-
context.addBeanSerializerModifier(new GuavaBeanSerializerModifier());
40+
41+
// 28-Apr-2015, tatu: Allow disabling "treat Optional.absent() like Java nulls"
42+
if (_cfgHandleAbsentAsNull) {
43+
context.addBeanSerializerModifier(new GuavaBeanSerializerModifier());
44+
}
2645
}
2746

47+
/**
48+
* Configuration method that may be used to change configuration setting
49+
* {@link #_cfgHandleAbsentAsNull}: enabling means that `Optional.absent()` values
50+
* are handled like Java nulls (wrt filtering on serialization); disabling that
51+
* they are only treated as "empty" values, but not like native Java nulls.
52+
* Recommended setting for this value is `false`, for compatibility with other
53+
* "optional" values (like JDK 8 optionals); but the default is `true` for
54+
* backwards compatibility.
55+
*
56+
* @return This module instance, useful for chaining calls
57+
*
58+
* @since 2.6
59+
*/
60+
public GuavaModule configureAbsentsAsNulls(boolean state) {
61+
_cfgHandleAbsentAsNull = state;
62+
return this;
63+
}
64+
2865
@Override
2966
public int hashCode()
3067
{

src/main/java/com/fasterxml/jackson/datatype/guava/ser/GuavaBeanSerializerModifier.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
import java.util.List;
1010

11-
public class GuavaBeanSerializerModifier extends BeanSerializerModifier {
12-
11+
/**
12+
* {@link BeanSerializerModifier} needed to sneak in handler to exclude "absent"
13+
* optional values iff handling of "absent as nulls" is enabled.
14+
*/
15+
public class GuavaBeanSerializerModifier extends BeanSerializerModifier
16+
{
1317
@Override
1418
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
1519
BeanDescription beanDesc,

src/main/java/com/fasterxml/jackson/datatype/guava/ser/GuavaOptionalSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
8484

8585
@Override
8686
public void serialize(Optional<?> value, JsonGenerator jgen, SerializerProvider provider)
87-
throws IOException
87+
throws IOException
8888
{
8989
if (value.isPresent()) {
9090
if (_valueSerializer != null) {

src/test/java/com/fasterxml/jackson/datatype/guava/TestOptional.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TestOptional extends ModuleTestBase
1616
private final ObjectMapper MAPPER = mapperWithModule();
1717

1818
@JsonAutoDetect(fieldVisibility=Visibility.ANY)
19-
public static final class OptionalData{
19+
public static final class OptionalData {
2020
private Optional<String> myString;
2121
}
2222

@@ -122,14 +122,34 @@ public void testSerOptDefault() throws Exception {
122122
String value = mapperWithModule().setSerializationInclusion(JsonInclude.Include.ALWAYS).writeValueAsString(data);
123123
assertEquals("{\"myString\":null}", value);
124124
}
125-
125+
126126
public void testSerOptNull() throws Exception {
127127
OptionalData data = new OptionalData();
128128
data.myString = null;
129129
String value = mapperWithModule().setSerializationInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(data);
130130
assertEquals("{}", value);
131131
}
132132

133+
// for [dataformat-guava#66]
134+
public void testSerOptDisableAsNull() throws Exception {
135+
final OptionalData data = new OptionalData();
136+
data.myString = Optional.absent();
137+
138+
GuavaModule mod = new GuavaModule().configureAbsentsAsNulls(false);
139+
ObjectMapper mapper = new ObjectMapper()
140+
.registerModule(mod)
141+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
142+
143+
assertEquals("{\"myString\":null}", mapper.writeValueAsString(data));
144+
145+
// but do exclude with NON_EMPTY
146+
mapper = new ObjectMapper()
147+
.registerModule(mod)
148+
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
149+
150+
assertEquals("{}", mapper.writeValueAsString(data));
151+
}
152+
133153
public void testSerOptNonEmpty() throws Exception {
134154
OptionalData data = new OptionalData();
135155
data.myString = null;

0 commit comments

Comments
 (0)