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

Commit b83c00e

Browse files
committed
Fix #64, at least for "new" mode.
1 parent eb6b98a commit b83c00e

File tree

9 files changed

+146
-25
lines changed

9 files changed

+146
-25
lines changed

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Guava (http://code.google.com/p/guava-libraries/) types (currently mostly just c
3131
<properties>
3232
<version.jackson>2.6.0-rc3</version.jackson>
3333

34+
<!-- More modern guava versions require 1.7? -->
35+
<javac.src.version>1.7</javac.src.version>
36+
<javac.target.version>1.7</javac.target.version>
37+
3438
<version.guava>15.0</version.guava>
3539
<version.guava.osgi>${version.guava}.0</version.guava.osgi>
3640

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Project: jackson-datatype-guava
1616
#69: Add support for `JsonInclude.Include.NON_ABSENT`, to compensate for #66
1717
#70: Change OSGi manifest entries to import guava 15.0 or greater
1818
(reported by sprynter@github)
19+
- Upgrade JDK baseline to 1.7
1920

2021
2.5.4 (not yet released)
2122

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public JsonSerializer<?> findMapLikeSerializer(SerializationConfig config,
8484
if (Multimap.class.isAssignableFrom(type.getRawClass())) {
8585
final AnnotationIntrospector intr = config.getAnnotationIntrospector();
8686
Object filterId = intr.findFilterId((Annotated)beanDesc.getClassInfo());
87-
String[] ignored = intr.findPropertiesToIgnore(beanDesc.getClassInfo());
87+
String[] ignored = intr.findPropertiesToIgnore(beanDesc.getClassInfo(), true);
8888
HashSet<String> ignoredEntries = (ignored == null || ignored.length == 0)
8989
? null : ArrayBuilders.arrayToSet(ignored);
9090

Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
package com.fasterxml.jackson.datatype.guava.ser;
22

33
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.PropertyName;
45
import com.fasterxml.jackson.databind.SerializerProvider;
56
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
7+
import com.fasterxml.jackson.databind.util.NameTransformer;
68
import com.google.common.base.Optional;
79

8-
public class GuavaOptionalBeanPropertyWriter extends BeanPropertyWriter {
9-
10+
public class GuavaOptionalBeanPropertyWriter extends BeanPropertyWriter
11+
{
1012
protected GuavaOptionalBeanPropertyWriter(BeanPropertyWriter base) {
1113
super(base);
1214
}
1315

16+
protected GuavaOptionalBeanPropertyWriter(BeanPropertyWriter base, PropertyName newName) {
17+
super(base, newName);
18+
}
19+
20+
// !!! TODO: in 2.7, no need to override
21+
@Override
22+
public BeanPropertyWriter rename(NameTransformer transformer) {
23+
String newName = transformer.transform(_name.getValue());
24+
if (newName.equals(_name.toString())) {
25+
return this;
26+
}
27+
return _new(PropertyName.construct(newName));
28+
}
29+
30+
// NOTE:
31+
// @Override
32+
protected BeanPropertyWriter _new(PropertyName newName) {
33+
return new GuavaOptionalBeanPropertyWriter(this, newName);
34+
}
35+
36+
@Override
37+
public BeanPropertyWriter unwrappingWriter(NameTransformer unwrapper) {
38+
return new GuavaUnwrappingOptionalBeanPropertyWriter(this, unwrapper);
39+
}
40+
1441
@Override
15-
public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov) throws Exception
42+
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
1643
{
1744
if (_nullSerializer == null) {
1845
Object value = get(bean);
1946
if (value == null || Optional.absent().equals(value)) {
2047
return;
2148
}
2249
}
23-
super.serializeAsField(bean, jgen, prov);
50+
super.serializeAsField(bean, gen, prov);
2451
}
25-
2652
}

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,13 @@ protected static JavaType _valueType(JavaType optionalType) {
211211
protected final JsonSerializer<Object> _findSerializer(SerializerProvider provider, Class<?> type)
212212
throws JsonMappingException
213213
{
214-
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers
215-
.findAndAddPrimarySerializer(type, provider, _property);
216-
if (_dynamicSerializers != result.map) {
217-
_dynamicSerializers = result.map;
218-
}
219-
JsonSerializer<Object> ser = result.serializer;
220-
// 26-Jun-2015, tatu: Sub-optimal if we do not cache unwrapped instance; but on plus side
221-
// construction is a cheap operation, so won't add huge overhead
222-
if (_unwrapper != null) {
223-
ser = ser.unwrappingSerializer(_unwrapper);
214+
JsonSerializer<Object> ser = _dynamicSerializers.serializerFor(type);
215+
if (ser == null) {
216+
ser = provider.findPrimaryPropertySerializer(type, _property);
217+
if (_unwrapper != null) {
218+
ser = ser.unwrappingSerializer(_unwrapper);
219+
}
220+
_dynamicSerializers = _dynamicSerializers.newWith(type, ser);
224221
}
225222
return ser;
226223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.datatype.guava.ser;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.core.io.SerializedString;
5+
import com.fasterxml.jackson.databind.SerializerProvider;
6+
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
7+
import com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter;
8+
import com.fasterxml.jackson.databind.util.NameTransformer;
9+
import com.google.common.base.Optional;
10+
11+
public class GuavaUnwrappingOptionalBeanPropertyWriter extends UnwrappingBeanPropertyWriter
12+
{
13+
public GuavaUnwrappingOptionalBeanPropertyWriter(BeanPropertyWriter base,
14+
NameTransformer transformer) {
15+
super(base, transformer);
16+
System.err.println("Unwrap/opt: ctor 1");
17+
}
18+
19+
protected GuavaUnwrappingOptionalBeanPropertyWriter(UnwrappingBeanPropertyWriter base,
20+
NameTransformer transformer, SerializedString name) {
21+
// 26-Jun-2015, tatu: TODO! call this ctor instead:
22+
// super(base, transformer, name);
23+
super(base, transformer);
24+
}
25+
26+
// TODO: In 2.7, should not need to override this method, just _new(...)
27+
@Override
28+
public UnwrappingBeanPropertyWriter rename(NameTransformer transformer)
29+
{
30+
String oldName = _name.getValue();
31+
String newName = transformer.transform(oldName);
32+
33+
// important: combine transformers:
34+
transformer = NameTransformer.chainedTransformer(transformer, _nameTransformer);
35+
36+
return _new(transformer, new SerializedString(newName));
37+
}
38+
39+
// NOTE: was added in one of later 2.6.0 RCs; uncomment once available
40+
// @Override
41+
protected UnwrappingBeanPropertyWriter _new(NameTransformer transformer, SerializedString newName)
42+
{
43+
return new GuavaUnwrappingOptionalBeanPropertyWriter(this, transformer, newName);
44+
}
45+
46+
@Override
47+
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
48+
{
49+
if (_nullSerializer == null) {
50+
Object value = get(bean);
51+
if (value == null || Optional.absent().equals(value)) {
52+
return;
53+
}
54+
}
55+
super.serializeAsField(bean, gen, prov);
56+
}
57+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
197197
Set<String> ignored = _ignoredEntries;
198198
boolean sortKeys = false;
199199
if (intr != null && propertyAcc != null) {
200-
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc);
200+
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc, true);
201201
if (moreToIgnore != null) {
202202
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
203203
for (String str : moreToIgnore) {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.google.common.base.Optional;
66

77
/**
8-
* Unit test for #64.
8+
* Unit test for #64, in new mode.
99
*/
1010
public class OptionalUnwrappedTest extends ModuleTestBase
1111
{
@@ -25,15 +25,12 @@ static class OptionalParent {
2525
public Optional<Child> child = Optional.of(new Child());
2626
}
2727

28-
// 'false' -> do NOT consider Absents as null-equivalents; avoid registration of BeanPropertyWriter
29-
private final ObjectMapper MAPPER = mapperWithModule(false);
30-
31-
public void testUntyped() throws Exception
28+
// Test for "new" settings of absent != nulls, available on 2.6 and later
29+
public void testUntypedWithOptionalsNotNulls() throws Exception
3230
{
33-
ObjectWriter w = MAPPER.writer();
34-
// String jsonExp = w.writeValueAsString(new Parent());
31+
final ObjectMapper mapper = mapperWithModule(false);
3532
String jsonExp = aposToQuotes("{'XX.name':'Bob'}");
36-
String jsonAct = w.writeValueAsString(new OptionalParent());
33+
String jsonAct = mapper.writeValueAsString(new OptionalParent());
3734
assertEquals(jsonExp, jsonAct);
3835
}
3936
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fasterxml.jackson.datatype.guava.failing;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import com.fasterxml.jackson.databind.*;
5+
6+
import com.google.common.base.Optional;
7+
8+
import com.fasterxml.jackson.datatype.guava.*;
9+
10+
/**
11+
* Unit test for remaining part of #64.
12+
*/
13+
public class OptionalUnwrappedTest extends ModuleTestBase
14+
{
15+
static class Child {
16+
public String name = "Bob";
17+
}
18+
19+
static class Parent {
20+
private Child child = new Child();
21+
22+
@JsonUnwrapped
23+
public Child getChild() { return child; }
24+
}
25+
26+
static class OptionalParent {
27+
@JsonUnwrapped(prefix="XX.")
28+
public Optional<Child> child = Optional.of(new Child());
29+
}
30+
31+
// Test for "old" settings (2.5 and earlier only option; available on later too)
32+
public void testUntypedWithNullEqOptionals() throws Exception
33+
{
34+
final ObjectMapper mapper = mapperWithModule(true);
35+
String jsonExp = aposToQuotes("{'XX.name':'Bob'}");
36+
String jsonAct = mapper.writeValueAsString(new OptionalParent());
37+
assertEquals(jsonExp, jsonAct);
38+
}
39+
}

0 commit comments

Comments
 (0)