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

Allow serialization of BigDecimal as String, using @JsonFormat(shape=Shape.String), config overrides #1911

Closed
cen1 opened this issue Jan 26, 2018 · 8 comments
Milestone

Comments

@cen1
Copy link

cen1 commented Jan 26, 2018

When REST API is consumed by JavaScript, in order to prevent the loss of precision, BigDecimal is normally passed as a string. While adding a custom serializer is easy, I think this is a very common case and warrants an out of the box SerializationFeature.

@cowtowncoder
Copy link
Member

This is bit too granular thing, at this point, as I would hope to avoid adding new non-general SerializationFeatures. But there is @JsonFormat already, with shape property (Shape.STRING) that should work.

It may be used on specific properties, but also via "configOverrides" for type; something like

mapper.configOverrides(BigDecimal.class).
   .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING));

If this does not yet work (it requires support from serializer), it should be added.
I'll leave this open for verification if anyone has time.

@cen1
Copy link
Author

cen1 commented Jan 26, 2018

That seems reasonable enough. I'll test on monday and come back.

@cowtowncoder
Copy link
Member

@cen1 Thanks. And thank you for reporting this; I agree that this is important use case for javascript clients.

@cowtowncoder
Copy link
Member

Ok while conversion does work for primitive types, it is not yet supported for BigInteger or BigDecimal. Needs to be added.

@cowtowncoder cowtowncoder changed the title FEATURE: Add SerializationFeature.WRITE_BIGDECIMAL_AS_STRING Allow serialization of BigDecimal as String, using @JsonFormat(shape=Shape.String), config overrides Jan 26, 2018
@cowtowncoder cowtowncoder added this to the 2.9.5 milestone Jan 26, 2018
@hashhar
Copy link

hashhar commented Sep 2, 2019

@cowtowncoder I cannot see anywhere what method is called on the BigDecimal to convert it to a string? Is it toPlainString(), toString() or toEngineeringString()?

@cowtowncoder
Copy link
Member

@hashhar what are you trying to achieve, specifically? Method will be either injackson-core (at low-level JsonGenerator implementation), or within jackson-databind. In latter case, it'd be in BigDecimalAsStringSerializer defined as inner class of NumberSerializer.

@hashhar
Copy link

hashhar commented Sep 5, 2019

I want to control how the BigDecimal gets converted into a string when using the JsonFormat.Shape support. For some BigDecimals I might want to use toPlainString() (think while dumping into a CSV) while for some I might be okay with toString()/toEngineeringString().

I found the code and it looks like we even have a config for it as StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN.

For other people who come across this, see the code at https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializer.java#L127

@cowtowncoder
Copy link
Member

Ok. Yes, at some point it may make sense to write a custom serializer: with createContextual() you have access to configuration and annotation information, including format settings.

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