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.

Support for BCP 47 `java.util.Locale` serialization/deserialization

See original GitHub issue

Describe 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. The x-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() and forLanguageTag() 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:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 30, 2021

@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:

  • for deserialization, it’s FromStringDeserializer
  • for serialization, basic ToStringSerializer is used (which as per name only calls Locale.toString())

So, it may be necessary to have separate serializer implementation if deserializer-only handling can not be made to work.

0reactions
cowtowncodercommented, Sep 8, 2021

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.

Read more comments on GitHub >

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

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