Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Fix #64, at least for "new" mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 26, 2015
1 parent eb6b98a commit b83c00e
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 25 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Guava (http://code.google.com/p/guava-libraries/) types (currently mostly just c
<properties>
<version.jackson>2.6.0-rc3</version.jackson>

<!-- More modern guava versions require 1.7? -->
<javac.src.version>1.7</javac.src.version>
<javac.target.version>1.7</javac.target.version>

<version.guava>15.0</version.guava>
<version.guava.osgi>${version.guava}.0</version.guava.osgi>

Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Project: jackson-datatype-guava
#69: Add support for `JsonInclude.Include.NON_ABSENT`, to compensate for #66
#70: Change OSGi manifest entries to import guava 15.0 or greater
(reported by sprynter@github)
- Upgrade JDK baseline to 1.7

2.5.4 (not yet released)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public JsonSerializer<?> findMapLikeSerializer(SerializationConfig config,
if (Multimap.class.isAssignableFrom(type.getRawClass())) {
final AnnotationIntrospector intr = config.getAnnotationIntrospector();
Object filterId = intr.findFilterId((Annotated)beanDesc.getClassInfo());
String[] ignored = intr.findPropertiesToIgnore(beanDesc.getClassInfo());
String[] ignored = intr.findPropertiesToIgnore(beanDesc.getClassInfo(), true);
HashSet<String> ignoredEntries = (ignored == null || ignored.length == 0)
? null : ArrayBuilders.arrayToSet(ignored);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
package com.fasterxml.jackson.datatype.guava.ser;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.util.NameTransformer;
import com.google.common.base.Optional;

public class GuavaOptionalBeanPropertyWriter extends BeanPropertyWriter {

public class GuavaOptionalBeanPropertyWriter extends BeanPropertyWriter
{
protected GuavaOptionalBeanPropertyWriter(BeanPropertyWriter base) {
super(base);
}

protected GuavaOptionalBeanPropertyWriter(BeanPropertyWriter base, PropertyName newName) {
super(base, newName);
}

// !!! TODO: in 2.7, no need to override
@Override
public BeanPropertyWriter rename(NameTransformer transformer) {
String newName = transformer.transform(_name.getValue());
if (newName.equals(_name.toString())) {
return this;
}
return _new(PropertyName.construct(newName));
}

// NOTE:
// @Override
protected BeanPropertyWriter _new(PropertyName newName) {
return new GuavaOptionalBeanPropertyWriter(this, newName);
}

@Override
public BeanPropertyWriter unwrappingWriter(NameTransformer unwrapper) {
return new GuavaUnwrappingOptionalBeanPropertyWriter(this, unwrapper);
}

@Override
public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov) throws Exception
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
if (_nullSerializer == null) {
Object value = get(bean);
if (value == null || Optional.absent().equals(value)) {
return;
}
}
super.serializeAsField(bean, jgen, prov);
super.serializeAsField(bean, gen, prov);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,13 @@ protected static JavaType _valueType(JavaType optionalType) {
protected final JsonSerializer<Object> _findSerializer(SerializerProvider provider, Class<?> type)
throws JsonMappingException
{
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers
.findAndAddPrimarySerializer(type, provider, _property);
if (_dynamicSerializers != result.map) {
_dynamicSerializers = result.map;
}
JsonSerializer<Object> ser = result.serializer;
// 26-Jun-2015, tatu: Sub-optimal if we do not cache unwrapped instance; but on plus side
// construction is a cheap operation, so won't add huge overhead
if (_unwrapper != null) {
ser = ser.unwrappingSerializer(_unwrapper);
JsonSerializer<Object> ser = _dynamicSerializers.serializerFor(type);
if (ser == null) {
ser = provider.findPrimaryPropertySerializer(type, _property);
if (_unwrapper != null) {
ser = ser.unwrappingSerializer(_unwrapper);
}
_dynamicSerializers = _dynamicSerializers.newWith(type, ser);
}
return ser;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.fasterxml.jackson.datatype.guava.ser;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter;
import com.fasterxml.jackson.databind.util.NameTransformer;
import com.google.common.base.Optional;

public class GuavaUnwrappingOptionalBeanPropertyWriter extends UnwrappingBeanPropertyWriter
{
public GuavaUnwrappingOptionalBeanPropertyWriter(BeanPropertyWriter base,
NameTransformer transformer) {
super(base, transformer);
System.err.println("Unwrap/opt: ctor 1");
}

protected GuavaUnwrappingOptionalBeanPropertyWriter(UnwrappingBeanPropertyWriter base,
NameTransformer transformer, SerializedString name) {
// 26-Jun-2015, tatu: TODO! call this ctor instead:
// super(base, transformer, name);
super(base, transformer);
}

// TODO: In 2.7, should not need to override this method, just _new(...)
@Override
public UnwrappingBeanPropertyWriter rename(NameTransformer transformer)
{
String oldName = _name.getValue();
String newName = transformer.transform(oldName);

// important: combine transformers:
transformer = NameTransformer.chainedTransformer(transformer, _nameTransformer);

return _new(transformer, new SerializedString(newName));
}

// NOTE: was added in one of later 2.6.0 RCs; uncomment once available
// @Override
protected UnwrappingBeanPropertyWriter _new(NameTransformer transformer, SerializedString newName)
{
return new GuavaUnwrappingOptionalBeanPropertyWriter(this, transformer, newName);
}

@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception
{
if (_nullSerializer == null) {
Object value = get(bean);
if (value == null || Optional.absent().equals(value)) {
return;
}
}
super.serializeAsField(bean, gen, prov);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
Set<String> ignored = _ignoredEntries;
boolean sortKeys = false;
if (intr != null && propertyAcc != null) {
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc);
String[] moreToIgnore = intr.findPropertiesToIgnore(propertyAcc, true);
if (moreToIgnore != null) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : moreToIgnore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.common.base.Optional;

/**
* Unit test for #64.
* Unit test for #64, in new mode.
*/
public class OptionalUnwrappedTest extends ModuleTestBase
{
Expand All @@ -25,15 +25,12 @@ static class OptionalParent {
public Optional<Child> child = Optional.of(new Child());
}

// 'false' -> do NOT consider Absents as null-equivalents; avoid registration of BeanPropertyWriter
private final ObjectMapper MAPPER = mapperWithModule(false);

public void testUntyped() throws Exception
// Test for "new" settings of absent != nulls, available on 2.6 and later
public void testUntypedWithOptionalsNotNulls() throws Exception
{
ObjectWriter w = MAPPER.writer();
// String jsonExp = w.writeValueAsString(new Parent());
final ObjectMapper mapper = mapperWithModule(false);
String jsonExp = aposToQuotes("{'XX.name':'Bob'}");
String jsonAct = w.writeValueAsString(new OptionalParent());
String jsonAct = mapper.writeValueAsString(new OptionalParent());
assertEquals(jsonExp, jsonAct);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.fasterxml.jackson.datatype.guava.failing;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;

import com.google.common.base.Optional;

import com.fasterxml.jackson.datatype.guava.*;

/**
* Unit test for remaining part of #64.
*/
public class OptionalUnwrappedTest extends ModuleTestBase
{
static class Child {
public String name = "Bob";
}

static class Parent {
private Child child = new Child();

@JsonUnwrapped
public Child getChild() { return child; }
}

static class OptionalParent {
@JsonUnwrapped(prefix="XX.")
public Optional<Child> child = Optional.of(new Child());
}

// Test for "old" settings (2.5 and earlier only option; available on later too)
public void testUntypedWithNullEqOptionals() throws Exception
{
final ObjectMapper mapper = mapperWithModule(true);
String jsonExp = aposToQuotes("{'XX.name':'Bob'}");
String jsonAct = mapper.writeValueAsString(new OptionalParent());
assertEquals(jsonExp, jsonAct);
}
}

0 comments on commit b83c00e

Please sign in to comment.