This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb6b98a
commit b83c00e
Showing
9 changed files
with
146 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 31 additions & 5 deletions
36
src/main/java/com/fasterxml/jackson/datatype/guava/ser/GuavaOptionalBeanPropertyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...a/com/fasterxml/jackson/datatype/guava/ser/GuavaUnwrappingOptionalBeanPropertyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/java/com/fasterxml/jackson/datatype/guava/failing/OptionalUnwrappedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |