Skip to content

Commit

Permalink
Fix #1911
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 27, 2018
1 parent cb24c3b commit b30fa09
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Project: jackson-databind
=== Releases ===
------------------------------------------------------------------------

2.9.5 (not released yet)

#1911: Allow serialization of `BigDecimal` as String, using
`@JsonFormat(shape=Shape.String)`, config overrides
(suggested by cen1@github)

2.9.4 (24-Jan-2018)

#1382: `@JsonProperty(access=READ_ONLY)` unxepected behaviour with `Collections`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.math.BigDecimal;
import java.math.BigInteger;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;

/**
* As a fallback, we may need to use this serializer for other
Expand All @@ -20,6 +22,7 @@
@SuppressWarnings("serial")
public class NumberSerializer
extends StdScalarSerializer<Number>
implements ContextualSerializer
{
/**
* Static instance that is only to be used for {@link java.lang.Number}.
Expand All @@ -37,6 +40,21 @@ public NumberSerializer(Class<? extends Number> rawType) {
_isInt = (rawType == BigInteger.class);
}

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
if (format != null) {
switch (format.getShape()) {
case STRING:
return ToStringSerializer.instance;
default:
}
}
return this;
}

@Override
public void serialize(Number value, JsonGenerator g, SerializerProvider provider) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ public class NumberSerTest extends BaseMapTest

static class IntWrapper {
public int i;

public IntWrapper(int value) { i = value; }
}


static class DoubleWrapper {
public double value;
public DoubleWrapper(double v) { value = v; }
}

static class BigDecimalWrapper {
public BigDecimal value;
public BigDecimalWrapper(BigDecimal v) { value = v; }
}

static class IntAsString {
@JsonFormat(shape=JsonFormat.Shape.STRING)
@JsonProperty("value")
Expand All @@ -39,6 +48,16 @@ static class DoubleAsString {
public double value = -0.5;
}

static class BigIntegerAsString {
@JsonFormat(shape=JsonFormat.Shape.STRING)
public BigInteger value = BigInteger.valueOf(123456L);
}

static class BigDecimalAsString {
@JsonFormat(shape=JsonFormat.Shape.STRING)
public BigDecimal value = BigDecimal.valueOf(0.25);
}

static class NumberWrapper {
// ensure it will use `Number` as statically force type, when looking for serializer
@JsonSerialize(as=Number.class)
Expand Down Expand Up @@ -87,15 +106,26 @@ public void testNumbersAsString() throws Exception
assertEquals(aposToQuotes("{'value':'3'}"), MAPPER.writeValueAsString(new IntAsString()));
assertEquals(aposToQuotes("{'value':'4'}"), MAPPER.writeValueAsString(new LongAsString()));
assertEquals(aposToQuotes("{'value':'-0.5'}"), MAPPER.writeValueAsString(new DoubleAsString()));
assertEquals(aposToQuotes("{'value':'0.25'}"), MAPPER.writeValueAsString(new BigDecimalAsString()));
assertEquals(aposToQuotes("{'value':'123456'}"), MAPPER.writeValueAsString(new BigIntegerAsString()));
}

public void testConfigOverridesForNumbers() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.configOverride(Integer.TYPE) // for `int`
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
mapper.configOverride(Double.TYPE) // for `double`
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));
mapper.configOverride(BigDecimal.class)
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));

assertEquals(aposToQuotes("{'i':'3'}"),
mapper.writeValueAsString(new IntWrapper(3)));
assertEquals(aposToQuotes("{'value':'0.75'}"),
mapper.writeValueAsString(new DoubleWrapper(0.75)));
assertEquals(aposToQuotes("{'value':'-0.5'}"),
mapper.writeValueAsString(new BigDecimalWrapper(BigDecimal.valueOf(-0.5))));
}

public void testNumberType() throws Exception
Expand Down

0 comments on commit b30fa09

Please sign in to comment.