Skip to content

Commit

Permalink
Fix #849
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 1, 2015
1 parent 19b897c commit 6172d75
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 25 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Project: jackson-databind
(contributed by Steve G, thezerobit@github)
#840: Change semantics of `@JsonPropertyOrder(alphabetic)` to only count `true` value
#848: Custom serializer not used if POJO has `@JsonValue`
#849: Possible problem with `NON_EMPTY` exclusion, `int`s, `Strings`
- Remove old cglib compatibility tests; cause problems in Eclipse

2.5.5 (not released)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static void addAll(Map<String, JsonSerializer<?>> allDeserializers)
protected abstract static class Base<T> extends StdScalarSerializer<T>
implements ContextualSerializer
{
protected final static Integer EMPTY_INTEGER = Integer.valueOf(0);

protected final JsonParser.NumberType _numberType;
protected final String _schemaType;
protected final boolean _isInt;
Expand Down Expand Up @@ -116,16 +118,22 @@ public JsonSerializer<?> createContextual(SerializerProvider prov,
@JacksonStdImpl
public final static class ShortSerializer extends Base<Short>
{
private final static Short EMPTY = (short) 0;
final static ShortSerializer instance = new ShortSerializer();

public ShortSerializer() { super(Short.class, JsonParser.NumberType.INT, "number"); }

@Override
public boolean isEmpty(SerializerProvider prov, Short value) {
return EMPTY.equals(value);
}

@Override
public void serialize(Short value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(value.shortValue());
}
}

/**
* This is the special serializer for regular {@link java.lang.Integer}s
* (and primitive ints)
Expand Down Expand Up @@ -153,6 +161,11 @@ public void serializeWithType(Object value, JsonGenerator gen,
// no type info, just regular serialization
serialize(value, gen, provider);
}

@Override
public boolean isEmpty(SerializerProvider prov, Object value) {
return EMPTY_INTEGER.equals(value);
}
}

/**
Expand All @@ -164,11 +177,16 @@ public void serializeWithType(Object value, JsonGenerator gen,
public final static class IntLikeSerializer extends Base<Number>
{
final static IntLikeSerializer instance = new IntLikeSerializer();

public IntLikeSerializer() {
super(Number.class, JsonParser.NumberType.INT, "integer");
}


@Override
public boolean isEmpty(SerializerProvider prov, Number value) {
return value.intValue() == 0;
}

@Override
public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(value.intValue());
Expand All @@ -178,26 +196,40 @@ public void serialize(Number value, JsonGenerator gen, SerializerProvider provid
@JacksonStdImpl
public final static class LongSerializer extends Base<Object>
{
private final static Long EMPTY = 0L;

final static LongSerializer instance = new LongSerializer();

public LongSerializer() { super(Long.class, JsonParser.NumberType.LONG, "number"); }


@Override
public boolean isEmpty(SerializerProvider prov, Object value) {
return EMPTY.equals(value);
}

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(((Long) value).longValue());
}
}

@JacksonStdImpl
public final static class FloatSerializer extends Base<Float>
public final static class FloatSerializer extends Base<Object>
{
private final static Float EMPTY = 0f;

final static FloatSerializer instance = new FloatSerializer();

public FloatSerializer() { super(Float.class, JsonParser.NumberType.FLOAT, "number"); }

@Override
public void serialize(Float value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(value.floatValue());
public boolean isEmpty(SerializerProvider prov, Object value) {
return EMPTY.equals(value);
}

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(((Float) value).floatValue());
}
}

Expand All @@ -211,10 +243,17 @@ public void serialize(Float value, JsonGenerator gen, SerializerProvider provide
@JacksonStdImpl
public final static class DoubleSerializer extends Base<Object>
{
private final static Double EMPTY = 0d;

final static DoubleSerializer instance = new DoubleSerializer();

public DoubleSerializer() { super(Double.class, JsonParser.NumberType.DOUBLE, "number"); }


@Override
public boolean isEmpty(SerializerProvider prov, Object value) {
return EMPTY.equals(value);
}

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(((Double) value).doubleValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
public class TestJsonSerialize2
extends BaseMapTest
{
// [JACKSON-480]

static class SimpleKey {
protected final String key;

Expand Down Expand Up @@ -107,6 +105,27 @@ public MapWrapperWithSerializer(String key, String value) {
}
}

static class NonEmptyString {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public String value;

public NonEmptyString(String v) { value = v; }
}

static class NonEmptyInt {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public int value;

public NonEmptyInt(int v) { value = v; }
}

static class NonEmptyDouble {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public double value;

public NonEmptyDouble(double v) { value = v; }
}

static class NullBean
{
@JsonSerialize(using=NullSerializer.class)
Expand Down Expand Up @@ -146,22 +165,19 @@ public void testSerializedAsListWithClassSerializer() throws IOException
assertEquals("[\"value foo\"]", m.writeValueAsString(list));
}

// [JACKSON-480], test annotations when applied to List property (getter, setter)
public void testSerializedAsListWithPropertyAnnotations() throws IOException
{
ListWrapperSimple input = new ListWrapperSimple("bar");
assertEquals("{\"values\":[{\"value\":\"bar\"}]}", MAPPER.writeValueAsString(input));
}

// [JACKSON-480], test Serialization annotation with Map
public void testSerializedAsMapWithClassSerializer() throws IOException
{
SimpleValueMapWithSerializer map = new SimpleValueMapWithSerializer();
map.put(new SimpleKey("abc"), new ActualValue("123"));
assertEquals("{\"key abc\":\"value 123\"}", MAPPER.writeValueAsString(map));
}

// [JACKSON-480], test annotations when applied to Map property (getter, setter)
public void testSerializedAsMapWithPropertyAnnotations() throws IOException
{
MapWrapperSimple input = new MapWrapperSimple("a", "b");
Expand All @@ -181,17 +197,11 @@ public void testSerializedAsMapWithPropertyAnnotations2() throws IOException
assertEquals("{\"values\":{\"key foo\":\"value b\"}}", MAPPER.writeValueAsString(input));
}

// [JACKSON-602]: Include.NON_EMPTY
public void testEmptyInclusion() throws IOException
public void testEmptyInclusionContainers() throws IOException
{
ObjectMapper defMapper = MAPPER;
ObjectMapper inclMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

StringWrapper str = new StringWrapper("");
assertEquals("{\"str\":\"\"}", defMapper.writeValueAsString(str));
assertEquals("{}", inclMapper.writeValueAsString(str));
assertEquals("{}", inclMapper.writeValueAsString(new StringWrapper()));

ListWrapper<String> list = new ListWrapper<String>();
assertEquals("{\"list\":[]}", defMapper.writeValueAsString(list));
assertEquals("{}", inclMapper.writeValueAsString(list));
Expand All @@ -208,7 +218,32 @@ public void testEmptyInclusion() throws IOException
assertEquals("{}", inclMapper.writeValueAsString(new ArrayWrapper<Integer>(null)));
}

// [JACKSON-699]
public void testEmptyInclusionScalars() throws IOException
{
ObjectMapper defMapper = MAPPER;
ObjectMapper inclMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

// First, Strings
StringWrapper str = new StringWrapper("");
assertEquals("{\"str\":\"\"}", defMapper.writeValueAsString(str));
assertEquals("{}", inclMapper.writeValueAsString(str));
assertEquals("{}", inclMapper.writeValueAsString(new StringWrapper()));

assertEquals("{\"value\":\"x\"}", defMapper.writeValueAsString(new NonEmptyString("x")));
assertEquals("{}", defMapper.writeValueAsString(new NonEmptyString("")));

// Then numbers
assertEquals("{\"value\":12}", defMapper.writeValueAsString(new NonEmptyInt(12)));
assertEquals("{}", defMapper.writeValueAsString(new NonEmptyInt(0)));

assertEquals("{\"value\":1.25}", defMapper.writeValueAsString(new NonEmptyDouble(1.25)));
assertEquals("{}", defMapper.writeValueAsString(new NonEmptyDouble(0.0)));

IntWrapper zero = new IntWrapper(0);
assertEquals("{\"i\":0}", defMapper.writeValueAsString(zero));
assertEquals("{}", inclMapper.writeValueAsString(zero));
}

public void testNullSerializer() throws Exception
{
String json = MAPPER.writeValueAsString(new NullBean());
Expand Down

0 comments on commit 6172d75

Please sign in to comment.