@JsonFormat Support for locale specific number format
See original GitHub issueDescribe the bug @JsonFormat does not work as expected when converting string to double and vice versa.
Version information 2.11.3
To Reproduce I have an entity with a double property. Challenge is to read an external API that sends the capacity value as string in German Format.
@JsonIgnoreProperties(ignoreUnknown = true)
data class Event(
@JsonProperty("NUMCapacity")
@JsonFormat(shape = Shape.STRING, locale = "de_DE", pattern = "#.#")
var capacity: Double,
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `double` from String "158,1": not a valid Double value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `double` from String "158,1": not a valid Double value
Expected behavior Expected behaviour is, when given the German locale, the comma is understood as the decimal separator.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Guide to @JsonFormat in Jackson - Baeldung
@JsonFormat is a Jackson annotation that we use to specify how to format fields and/or properties for JSON output.
Read more >Jackson JSON - Using @JsonFormat to format Date and Enum
@JsonFormat is a general purpose annotation which can be used to serialize a particular type into a specific format.
Read more >JsonFormat (Jackson-annotations 2.8.0 API) - Javadoc.io
Locale to use for serialization (if needed). String · pattern. Datatype-specific additional piece of configuration that may be used to further refine formatting...
Read more >Date format Mapping to JSON Jackson - java - Stack Overflow
Since Jackson v2.0, you can use @JsonFormat annotation directly on Object members; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ...
Read more >NumberFormat (Java SE 12 & JDK 12 ) - Oracle Help Center
To format a number for a different Locale, specify it in the call to getInstance . NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);. If the locale ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@emiatej9 Typically all handling of format annotations should be done during construction or contextualization phases and specifically should never be passed (and rarely if ever used) accessed during actual deserialization calls.
Contextualization refers to handling in method
createContextual()
, which is called after construction but before any actual use for deserialization. It will be passedBeanProperty
through which annotations (property and class annotations) are accessible.You don’t need to check
FieldProperty
methods: this is not the place handling should occur (since these are deserializer-independent entities).Instead,
createContextual()
method – you may want to see how other standard deserializers use it – usually checks for various configuration settings, annotations, and so on, and then creates (and returns) a differently configured instance as necessary.Searching code related to this issue, I found below lines in
deserializeAndSet
method:https://github.com/FasterXML/jackson-databind/blob/ada34dcf6676aa5c5869791c23143a65a7bf56c2/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java#L135-L140
In line 138
_valueDeserializer
have reference of static classDoubleDeserializer
inNumberDeserializers
. I think the annotations the_field
property is holding should be passed toDoubleDeserializer#deserialize(p, ctxt)
at this point so as to exploitlocale
andpattern
before parsing the given text.https://github.com/FasterXML/jackson-databind/blob/ada34dcf6676aa5c5869791c23143a65a7bf56c2/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java#L31-L36
However, It seems very difficult to modify parameters of
deserialize(Jsonparser p, DeserializationContext ctxt)
method which is inherited fromJsonDeserialize
class.Could you give me some hints or guides to fix this issue?