Skip to content

Commit

Permalink
Add (failing) test for 2230
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 4, 2019
1 parent 38a61e0 commit b4dea3c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -55,7 +56,10 @@ static class BigIntegerAsString {

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

public BigDecimalAsString() { this(BigDecimal.valueOf(0.25)); }
public BigDecimalAsString(BigDecimal v) { value = v; }
}

static class NumberWrapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fasterxml.jackson.failing;

import java.math.BigDecimal;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.core.StreamWriteFeature;

import com.fasterxml.jackson.databind.*;

public class BigDecimalPlain2230Test extends BaseMapTest
{
static class BigDecimalAsString {
@JsonFormat(shape=JsonFormat.Shape.STRING)
public BigDecimal value;

public BigDecimalAsString() { this(BigDecimal.valueOf(0.25)); }
public BigDecimalAsString(BigDecimal v) { value = v; }
}

private final ObjectMapper MAPPER = objectMapper();

public void testBigIntegerAsPlainTest() throws Exception
{
final String NORM_VALUE = "0.0000000005";
final BigDecimal BD_VALUE = new BigDecimal(NORM_VALUE);
final BigDecimalAsString INPUT = new BigDecimalAsString(BD_VALUE);
// by default, use the default `toString()`
assertEquals("{\"value\":\""+BD_VALUE.toString()+"\"", MAPPER.writeValueAsString(INPUT));

// but can force to "plain" notation
final ObjectMapper m = jsonMapperBuilder()
.enable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN)
.build();
assertEquals("{\"value\":\""+NORM_VALUE+"\"", m.writeValueAsString(INPUT));
}
}

0 comments on commit b4dea3c

Please sign in to comment.