Skip to content

Commit

Permalink
Fix #33
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 20, 2017
1 parent 4cadfc8 commit 44d2128
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public final void serializeAsField(Object bean, JsonGenerator gen, SerializerPro
}
// Null (etc) handling; copied from super-class impl
if (value == null) {
if (!_suppressNulls) {
if (_nullSerializer != null) {
gen.writeFieldName(_fastName);
_nullSerializer.serialize(null, gen, prov);
} else if (!_suppressNulls) {
gen.writeFieldName(_fastName);
prov.defaultSerializeNull(gen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public final void serializeAsField(Object bean, JsonGenerator gen, SerializerPro
}
// Null (etc) handling; copied from super-class impl
if (value == null) {
if (!_suppressNulls) {
if (_nullSerializer != null) {
gen.writeFieldName(_fastName);
_nullSerializer.serialize(null, gen, prov);
} else if (!_suppressNulls) {
gen.writeFieldName(_fastName);
prov.defaultSerializeNull(gen);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.fasterxml.jackson.module.afterburner.ser;

import java.io.*;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;

// Copied from [com.fasterxml.jackson.databind.ser.filter]
public class NullSerializationTest extends AfterburnerTestBase
{
static class NullSerializer extends JsonSerializer<Object>
{
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
gen.writeString("foobar");
}
}

static class Bean1 {
public String name = null;
}

static class Bean2 {
public String type = null;
}

@SuppressWarnings("serial")
static class MyNullProvider extends DefaultSerializerProvider
{
public MyNullProvider() { super(); }
public MyNullProvider(MyNullProvider base, SerializationConfig config, SerializerFactory jsf) {
super(base, config, jsf);
}

// not really a proper impl, but has to do
@Override
public DefaultSerializerProvider copy() {
return this;
}

@Override
public DefaultSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) {
return new MyNullProvider(this, config, jsf);
}

@Override
public JsonSerializer<Object> findNullValueSerializer(BeanProperty property)
throws JsonMappingException
{
if ("name".equals(property.getName())) {
return new NullSerializer();
}
return super.findNullValueSerializer(property);
}
}

static class BeanWithNullProps
{
@JsonSerialize(nullsUsing=NullSerializer.class)
public String a = null;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = mapperWithModule();

public void testSimple() throws Exception
{
assertEquals("null", MAPPER.writeValueAsString(null));
}

public void testOverriddenDefaultNulls() throws Exception
{
DefaultSerializerProvider sp = new DefaultSerializerProvider.Impl();
sp.setNullValueSerializer(new NullSerializer());
ObjectMapper m = new ObjectMapper();
m.setSerializerProvider(sp);
assertEquals("\"foobar\"", m.writeValueAsString(null));
}

public void testCustomPOJONullsViaProvider() throws Exception
{
ObjectMapper m = new ObjectMapper();
m.setSerializerProvider(new MyNullProvider());
assertEquals("{\"name\":\"foobar\"}", m.writeValueAsString(new Bean1()));
assertEquals("{\"type\":null}", m.writeValueAsString(new Bean2()));
}

public void testCustomTreeNullsViaProvider() throws Exception
{
ObjectNode root = MAPPER.createObjectNode();
root.putNull("a");

// by default, null is... well, null
assertEquals("{\"a\":null}", MAPPER.writeValueAsString(root));

// but then we can customize it:
DefaultSerializerProvider prov = new MyNullProvider();
prov.setNullValueSerializer(new NullSerializer());
ObjectMapper m = new ObjectMapper();
m.setSerializerProvider(prov);
assertEquals("{\"a\":\"foobar\"}", m.writeValueAsString(root));
}

public void testNullSerializeViaPropertyAnnotation() throws Exception
{
assertEquals("{\"a\":\"foobar\"}", MAPPER.writeValueAsString(new BeanWithNullProps()));
}
}
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Modules:
#30: (afterburner) `IncompatibleClassChangeError` deserializing interface
methods with default impl
(reported by Shawn S)
#33: (afterburner) `@JsonSerialize` with `nullUsing` option not working for `String` properties

2.8.10 (24-Aug-2017)
2.8.9 (12-Jun-2017)
Expand Down

0 comments on commit 44d2128

Please sign in to comment.