Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRITE_BIGDECIMAL_AS_PLAIN is ignored if @JsonFormat is used #2230

Closed
pacher opened this issue Jan 22, 2019 · 7 comments
Closed

WRITE_BIGDECIMAL_AS_PLAIN is ignored if @JsonFormat is used #2230

pacher opened this issue Jan 22, 2019 · 7 comments
Milestone

Comments

@pacher
Copy link

pacher commented Jan 22, 2019

I am trying to serialize BigDecimal as json string while avoiding scientific notation (kotlin):

data class Test(
    @JsonFormat(shape= JsonFormat.Shape.STRING)
    val value: BigDecimal
)

fun main() {
    val mapper = jacksonObjectMapper()
        .configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true)
    val test = Test(0.0000000005.toBigDecimal())
    println(mapper.writeValueAsString(test))
}

output {"value":"5.0E-10"}
If JsonFormat is removed, then WRITE_BIGDECIMAL_AS_PLAIN works and output is {"value":0.00000000050} (json number, not string), but trying to make it json string with JsonFormat results in WRITE_BIGDECIMAL_AS_PLAIN being ignored.

Using latest version, jackson-bom:2.9.8

@cowtowncoder
Copy link
Member

Ok: I notice that code is not in plan Java but in... Kotlin? If so, it'd be good to ensure that behavior does not vary between Java and Kotlin. I can modify code to ensure it, but going forward it'd be good to isolate this first: reason being that sometimes Kotlin module has additional problems (for example annotation might be discovered in different way), so it is good to know where problem occurs.

But for now I assume this is reproducible with just Java.

@pacher
Copy link
Author

pacher commented Jan 23, 2019

Hi @cowtowncoder , thanks for looking into this.

Sorry, I was under impression that it is generally acceptable to provide snippets in kotlin these days.

Yes, it is reproducible with just Java

public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        var mapper = new ObjectMapper().configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        var test = new Test(new BigDecimal("0.0000000005"));
        System.out.println(mapper.writeValueAsString(test));
    }
}

class Test {
    private final BigDecimal value;
    Test(BigDecimal value) {
        this.value = value;
    }

    @JsonFormat(shape= JsonFormat.Shape.STRING)
    public BigDecimal getValue() {
        return value;
    }
}

Same outputs, {"value":"5E-10"} with @JsonFormat and {"value":0.0000000005} without

@cowtowncoder
Copy link
Member

Use of Kotlin is ok for demonstrating many aspects, but I can't include it directly (Jackson does not use Kotlin runtime so it can't run, and I don't want to add dependency just for tests). And at times Kotlin module adds special handling so it's often easier to use basic Java -- unless we have Kotlin-specific problem.

Thank you again for reporting the problem & helping even more useful reproduction. I hope to work on this issue soon.

@GedMarc
Copy link

GedMarc commented Jan 24, 2019

must admit, I honestly never considered Kotlin as "the norm", especially not for test cases xD
And I've never heard of it referenced as such
It's the recommended for android yes, but not for standard use as far as I'm aware, so that's a pretty bold statement to make

@GedMarc
Copy link

GedMarc commented Jan 24, 2019

@cowtowncoder I have a similar scenario my side, will see what I can do today

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Feb 4, 2019
@cowtowncoder
Copy link
Member

I can reproduce this fine from Java, so there is nothing Kotlin-specific.

The reason for problem is that when forcing "serialize as String", most types will use default ToStringSerializer, which will then not do any additional processing on value.

I'll see if I can fix this easily, or need to defer.

@cowtowncoder cowtowncoder changed the title WRITE_BIGDECIMAL_AS_PLAIN is ignored if @JsonFormat is used WRITE_BIGDECIMAL_AS_PLAIN is ignored if @JsonFormat is used Feb 5, 2019
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Feb 5, 2019
@cowtowncoder
Copy link
Member

Ok. Well, that was... more work than I thought, partially since there is actually one DoS aspect to consider with plain strings too. But it is implemented for 2.10; will not backport to 2.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants