Support for BCP 47 `java.util.Locale` serialization/deserialization
See original GitHub issueDescribe the bug
Locale with BCP 47 extensions are not serialized / de-serialized as expected using ObjectMapper
.
Version information
Which Jackson version(s) was this for? All Jackson versions.
To Reproduce
If you have a way to reproduce this with:
Brief code sample/snippet: include here in pre-formatted/code section
Locale locale = Locale.forLanguageTag("en-US-x-debug");
// ^^^ above locale will have x-debug as extensions
// This locale is a BCP 47 compliant locale.
ObjectMapper objectMapper = new ObjectMapper();
String serializedLocale = objectMapper.writeValueAsString(locale);
// ^^^ Will be en_US-#x-debug. Conforms to toString() method of Locale.
// This is not correct. There is a rogue # is added before extensions.
Locale deserializedLocale = objectMapper.readValue(serializedLocale, Locale.class);
// ^^^ Due to the rogue # before the extensions, when we de-serialize it, the extensions get added as a Variant instead of extensions.
Longer example stored somewhere else (diff repo, snippet), add a link
N/A. Reproducible with the above piece of code.
Textual explanation:
forLanguageTag
locale is used to create a locale object from a BCP 47 compliant locale string. Thex-debug
gets added as extensions in the Locale.- However, when we use ObjectMapper to serialize the string, objectMapper uses
toString()
method of locale to convert it to String. - As Per Locale Javadocs, for BCP 47 locales, we must be using
toLanguageTag()
andforLanguageTag()
instead to convert to / from String. - Due to this, when we try to de-serialize this malformed Locale, the
x-debug
gets added as a Variant instead of extensions, which is not expected.
Expected behavior
- BCP 47 extensions should be added as extensions and not as Variants when serializing and de-serializing.
Additional Details
- Due to this drawback, we are forced to write our own serializer / de-serializer. It would be nice if ObjectMapper provides us a Module which does this for us.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
BCP 47 Extensions - Setting the Locale
This internationalization Java tutorial describes setting locale, isolating locale-specific data, formatting data, internationalized domain name and ...
Read more >Security update for jackson-databind, jackson-dataformats ...
... + Support for BCP 47 'java.util.Locale' serialization/deserialization + String property deserializes null as "null" for JsonTypeInfo.
Read more >subject:"commit jackson\-databind for openSUSE\:Factory"
... #3259: Support for BCP 47 'java.util.Locale' + serialization/deserialization ++ #3271: String property deserializes null as "null" for + JsonTypeInfo.
Read more >Table of Contents - Micronaut Documentation
It supports serializing and deserializing Java types (including Java 17 records) to ... Locales can be configured in the "en_GB" format, or in...
Read more >java/util/Locale.java - platform/prebuilts/fullsdk ... - Google Git
for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode. * Locale Data Markup Language") BCP 47-compatible extensions for locale data.
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
@praneethjreddy Sure. I do think we have custom (de)serializer already, but it might be part of a multi-type (de)serializer… let me look.
Ok, so:
FromStringDeserializer
ToStringSerializer
is used (which as per name only callsLocale.toString()
)So, it may be necessary to have separate serializer implementation if deserializer-only handling can not be made to work.
Uhhhh. Should have remember to link #1600 here; Jackson 3.0 already serializes using “toLanguageTag()”.
So 3.0 (
master
) will probably need bit different deserializer as all new tests are now failing.