Skip to content

Commit

Permalink
Fixed #1480
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 28, 2017
1 parent 9736c47 commit 3749d7c
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 52 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Project: jackson-databind
(suggested by PawelJagus@github)
#1454: Support `@JsonFormat.lenient` for `java.util.Date`, `java.util.Calendar`
#1474: Replace use of `Class.newInstance()` (deprecated in Java 9) with call via Constructor
#1480: Add support for serializing `boolean`/`Boolean` as number (0 or 1)
(suggested by jwilmoth@github)
#1520: Case insensitive enum deserialization feature.
(contributed by Ana-Eliza B)
#1544: EnumMapDeserializer assumes a pure EnumMap and does not support EnumMap derived classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import java.io.IOException;
import java.lang.reflect.Type;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;

/**
* Serializer used for primitive boolean, as well as java.util.Boolean
Expand All @@ -24,6 +28,7 @@
public final class BooleanSerializer
//In 2.9, removed use of intermediate type `NonTypedScalarSerializerBase`
extends StdScalarSerializer<Object>
implements ContextualSerializer
{
private static final long serialVersionUID = 1L;

Expand All @@ -38,26 +43,102 @@ public BooleanSerializer(boolean forPrimitive) {
_forPrimitive = forPrimitive;
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, Boolean.class);
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (shape.isNumeric()) {
return new AsNumber(_forPrimitive);
}
}
return this;
}

@Override
public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
g.writeBoolean(((Boolean) value).booleanValue());
g.writeBoolean(Boolean.TRUE.equals(value));
}

@Override
public final void serializeWithType(Object value, JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer) throws IOException
{
g.writeBoolean(((Boolean) value).booleanValue());
g.writeBoolean(Boolean.TRUE.equals(value));
}

@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("boolean", !_forPrimitive);
}

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
{
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
visitor.expectBooleanFormat(typeHint);
}

/**
* Alternate implementation that is used when values are to be serialized
* as numbers <code>0</code> (false) or <code>1</code> (true).
*
* @since 2.9
*/
final static class AsNumber
extends StdScalarSerializer<Object>
implements ContextualSerializer
{
private static final long serialVersionUID = 1L;

/**
* Whether type serialized is primitive (boolean) or wrapper
* (java.lang.Boolean); if true, former, if false, latter.
*/
protected final boolean _forPrimitive;

public AsNumber(boolean forPrimitive) {
super(forPrimitive ? Boolean.TYPE : Boolean.class, false);
_forPrimitive = forPrimitive;
}

@Override
public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
g.writeNumber((Boolean.FALSE.equals(value)) ? 0 : 1);
}

@Override
public final void serializeWithType(Object value, JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer) throws IOException
{
// 27-Mar-2017, tatu: Actually here we CAN NOT serialize as number without type,
// since with natural types that would map to number, not boolean. So choice
// comes to between either add type id, or serialize as boolean. Choose
// latter at this point
g.writeBoolean(Boolean.TRUE.equals(value));
}

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
// 27-Mar-2017, tatu: As usual, bit tricky but... seems like we should call
// visitor for actual representation
visitIntFormat(visitor, typeHint, NumberType.INT);
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, Boolean.class);
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (!shape.isNumeric()) {
return new BooleanSerializer(_forPrimitive);
}
}
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ public static EnumSerializer construct(Class<?> enumClass, SerializationConfig c
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
if (property != null) {
JsonFormat.Value format = findFormatOverrides(serializers,
property, handledType());
if (format != null) {
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(property.getType().getRawClass(),
format, false, _serializeAsIndex);
if (serializeAsIndex != _serializeAsIndex) {
return new EnumSerializer(_values, serializeAsIndex);
}
JsonFormat.Value format = findFormatOverrides(serializers,
property, handledType());
if (format != null) {
Class<?> type = handledType();
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(type,
format, false, _serializeAsIndex);
if (serializeAsIndex != _serializeAsIndex) {
return new EnumSerializer(_values, serializeAsIndex);
}
}
return this;
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import static org.junit.Assert.*;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
Expand Down Expand Up @@ -41,10 +39,8 @@ public String getSchemaType() {
protected static class BooleanWrapper {
public Boolean b;

@JsonCreator
public BooleanWrapper() { }
public BooleanWrapper(Boolean value) { b = value; }

@JsonValue public Boolean value() { return b; }
}

protected static class IntWrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import java.util.*;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@SuppressWarnings("serial")
public class TestGenericCollectionDeser
extends BaseMapTest
{
/*
/**********************************************************
/* Test classes, enums
/**********************************************************
*/

static class ListSubClass extends ArrayList<StringWrapper> { }

/**
Expand All @@ -24,15 +20,18 @@ static class ListSubClass extends ArrayList<StringWrapper> { }
@JsonDeserialize(contentAs=StringWrapper.class)
static class AnnotatedStringList extends ArrayList<Object> { }

@JsonDeserialize(contentAs=BooleanWrapper.class)
@JsonDeserialize(contentAs=BooleanElement.class)
static class AnnotatedBooleanList extends ArrayList<Object> { }

/*
/**********************************************************
/* Test methods
/**********************************************************
*/
protected static class BooleanElement {
public Boolean b;

@JsonCreator
public BooleanElement(Boolean value) { b = value; }

@JsonValue public Boolean value() { return b; }
}

/*
/**********************************************************
/* Tests for sub-classing
Expand Down Expand Up @@ -76,7 +75,7 @@ public void testAnnotatedBooleanList() throws Exception
AnnotatedBooleanList result = mapper.readValue("[ false ]", AnnotatedBooleanList.class);
assertEquals(1, result.size());
Object ob = result.get(0);
assertEquals(BooleanWrapper.class, ob.getClass());
assertFalse(((BooleanWrapper) ob).b);
assertEquals(BooleanElement.class, ob.getClass());
assertFalse(((BooleanElement) ob).b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fasterxml.jackson.databind.format;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

// [databind#1480]
public class BooleanFormatTest extends BaseMapTest
{
@JsonPropertyOrder({ "b1", "b2", "b3" })
static class BeanWithBoolean
{
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public boolean b1;

@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public Boolean b2;

public boolean b3;

public BeanWithBoolean() { }
public BeanWithBoolean(boolean b1, Boolean b2, boolean b3) {
this.b1 = b1;
this.b2 = b2;
this.b3 = b3;
}
}

static class AltBoolean extends BooleanWrapper
{
public AltBoolean() { }
public AltBoolean(Boolean b) { super(b); }
}

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

private final static ObjectMapper MAPPER = new ObjectMapper();

public void testShapeViaDefaults() throws Exception
{
assertEquals(aposToQuotes("{'b':true}"),
MAPPER.writeValueAsString(new BooleanWrapper(true)));
ObjectMapper m = new ObjectMapper();
m.configOverride(Boolean.class)
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
assertEquals(aposToQuotes("{'b':1}"),
m.writeValueAsString(new BooleanWrapper(true)));
}

public void testShapeOnProperty() throws Exception
{
assertEquals(aposToQuotes("{'b1':1,'b2':0,'b3':true}"),
MAPPER.writeValueAsString(new BeanWithBoolean(true, false, true)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
@SuppressWarnings("serial")
public class TestPropertyTypeInfo extends BaseMapTest
{
/*
/**********************************************************
/* Helper types
/**********************************************************
*/
protected static class BooleanValue {
public Boolean b;

@JsonCreator
public BooleanValue(Boolean value) { b = value; }

@JsonValue public Boolean value() { return b; }
}

static class FieldWrapperBean
{
Expand Down Expand Up @@ -120,16 +123,16 @@ public void testSimpleListMethod() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
MethodWrapperBeanList list = new MethodWrapperBeanList();
list.add(new MethodWrapperBean(new BooleanWrapper(true)));
list.add(new MethodWrapperBean(new BooleanValue(true)));
list.add(new MethodWrapperBean(new StringWrapper("x")));
list.add(new MethodWrapperBean(new OtherBean()));
String json = mapper.writeValueAsString(list);
MethodWrapperBeanList result = mapper.readValue(json, MethodWrapperBeanList.class);
assertNotNull(result);
assertEquals(3, result.size());
MethodWrapperBean bean = result.get(0);
assertEquals(BooleanWrapper.class, bean.value.getClass());
assertEquals(((BooleanWrapper) bean.value).b, Boolean.TRUE);
assertEquals(BooleanValue.class, bean.value.getClass());
assertEquals(((BooleanValue) bean.value).b, Boolean.TRUE);
bean = result.get(1);
assertEquals(StringWrapper.class, bean.value.getClass());
assertEquals(((StringWrapper) bean.value).str, "x");
Expand All @@ -141,15 +144,15 @@ public void testSimpleArrayField() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
FieldWrapperBeanArray array = new FieldWrapperBeanArray(new
FieldWrapperBean[] { new FieldWrapperBean(new BooleanWrapper(true)) });
FieldWrapperBean[] { new FieldWrapperBean(new BooleanValue(true)) });
String json = mapper.writeValueAsString(array);
FieldWrapperBeanArray result = mapper.readValue(json, FieldWrapperBeanArray.class);
assertNotNull(result);
FieldWrapperBean[] beans = result.beans;
assertEquals(1, beans.length);
FieldWrapperBean bean = beans[0];
assertEquals(BooleanWrapper.class, bean.value.getClass());
assertEquals(((BooleanWrapper) bean.value).b, Boolean.TRUE);
assertEquals(BooleanValue.class, bean.value.getClass());
assertEquals(((BooleanValue) bean.value).b, Boolean.TRUE);
}

public void testSimpleArrayMethod() throws Exception
Expand Down Expand Up @@ -187,15 +190,15 @@ public void testSimpleMapMethod() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
MethodWrapperBeanMap map = new MethodWrapperBeanMap();
map.put("xyz", new MethodWrapperBean(new BooleanWrapper(true)));
map.put("xyz", new MethodWrapperBean(new BooleanValue(true)));
String json = mapper.writeValueAsString(map);
MethodWrapperBeanMap result = mapper.readValue(json, MethodWrapperBeanMap.class);
assertNotNull(result);
assertEquals(1, result.size());
MethodWrapperBean bean = result.get("xyz");
assertNotNull(bean);
Object ob = bean.value;
assertEquals(BooleanWrapper.class, ob.getClass());
assertEquals(((BooleanWrapper) ob).b, Boolean.TRUE);
assertEquals(BooleanValue.class, ob.getClass());
assertEquals(((BooleanValue) ob).b, Boolean.TRUE);
}
}

0 comments on commit 3749d7c

Please sign in to comment.