question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonFormat Support for locale specific number format

See original GitHub issue

Describe 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 4, 2022

@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 passed BeanProperty 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.

0reactions
emiatej9commented, May 4, 2022

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 class DoubleDeserializer in NumberDeserializers. I think the annotations the _field property is holding should be passed to DoubleDeserializer#deserialize(p, ctxt) at this point so as to exploit locale and pattern 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 from JsonDeserialize class.

Could you give me some hints or guides to fix this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found