-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(3259): Support deserialization of Locales created using BCP 47 format #3265
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
Merged
cowtowncoder
merged 2 commits into
FasterXML:2.13
from
praneethjreddy:feature/BCP47_locale_serialization
Sep 8, 2021
+237
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
src/test/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.BaseTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.Locale; | ||
|
||
public class FromStringDeserializerTest extends BaseTest { | ||
|
||
private final Locale[] LOCALES = new Locale[] | ||
{Locale.CANADA, Locale.ROOT, Locale.GERMAN, Locale.CHINESE, Locale.KOREA, Locale.TAIWAN}; | ||
private final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public void testLocaleDeserializeNonBCPFormat() throws JsonProcessingException { | ||
Locale locale = new Locale("en", "US"); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = new Locale("en"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = new Locale("en", "US", "VARIANT"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = new Locale("en", "", "VARIANT"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = new Locale("", "US", "VARIANT"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = new Locale("", "US", ""); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
} | ||
|
||
public void testLocaleDeserializeWithScript() throws JsonProcessingException { | ||
Locale locale = new Locale.Builder().setLanguage("en").setRegion("GB").setVariant("VARIANT") | ||
.setScript("Latn").build(); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("en").setScript("Latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("IN").setScript("Latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("fr").setRegion("CA").setScript("Latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("CA").setVariant("VARIANT").setScript("Latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("it").setVariant("VARIANT").setScript("Latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
} | ||
|
||
public void testLocaleDeserializeWithExtension() throws JsonProcessingException { | ||
Locale locale = new Locale.Builder().setLanguage("en").setRegion("GB").setVariant("VARIANT") | ||
.setExtension('x', "dummy").build(); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithExtension(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("en").setExtension('x', "dummy").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("IN").setExtension('x', "dummy").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("fr").setRegion("CA").setExtension('x', "dummy").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("CA").setVariant("VARIANT").setExtension('x', "dummy").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("it").setVariant("VARIANT").setExtension('x', "dummy").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocaleWithScript(locale, deSerializedLocale); | ||
} | ||
|
||
public void testLocaleDeserializeWithScriptAndExtension() throws JsonProcessingException { | ||
Locale locale = new Locale.Builder().setLanguage("en").setRegion("GB").setVariant("VARIANT") | ||
.setExtension('x', "dummy").setScript("latn").build(); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("en").setExtension('x', "dummy").setScript("latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("IN").setExtension('x', "dummy").setScript("latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("fr").setRegion("CA") | ||
.setExtension('x', "dummy").setScript("latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setRegion("CA").setVariant("VARIANT") | ||
.setExtension('x', "dummy").setScript("latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale.Builder().setLanguage("it").setVariant("VARIANT") | ||
.setExtension('x', "dummy").setScript("latn").build(); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
} | ||
|
||
public void testLocaleDeserializeWithLanguageTag() throws JsonProcessingException { | ||
Locale locale = Locale.forLanguageTag("en-US-x-debug"); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = Locale.forLanguageTag("en-US-x-lvariant-POSIX"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-AbcDef"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
|
||
locale = Locale.forLanguageTag("ar-aao"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = Locale.forLanguageTag("en-abc-def-us"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
} | ||
|
||
public void testIllFormedVariant() throws JsonProcessingException { | ||
Locale locale = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def"); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertBaseValues(locale, deSerializedLocale); | ||
} | ||
|
||
public void testLocaleDeserializeWithLocaleConstants() throws JsonProcessingException { | ||
for (Locale locale: LOCALES) { | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
} | ||
} | ||
|
||
public void testSpecialCases() throws JsonProcessingException { | ||
Locale locale = new Locale("ja", "JP", "JP"); | ||
Locale deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
|
||
locale = new Locale("th", "TH", "TH"); | ||
deSerializedLocale = MAPPER.readValue(MAPPER.writeValueAsString(locale), Locale.class); | ||
assertLocale(locale, deSerializedLocale); | ||
} | ||
|
||
private void assertBaseValues(Locale expected, Locale actual) { | ||
assertEquals(expected.getLanguage(), actual.getLanguage()); | ||
assertEquals(expected.getCountry(), actual.getCountry()); | ||
assertEquals(expected.getVariant(), actual.getVariant()); | ||
} | ||
|
||
private void assertLocaleWithScript(Locale expected, Locale actual) { | ||
assertBaseValues(expected, actual); | ||
assertEquals(expected.getScript(), actual.getScript()); | ||
} | ||
|
||
private void assertLocaleWithExtension(Locale expected, Locale actual) { | ||
assertBaseValues(expected, actual); | ||
assertEquals(expected.getExtension('x'), actual.getExtension('x')); | ||
} | ||
|
||
private void assertLocale(Locale expected, Locale actual) { | ||
assertBaseValues(expected, actual); | ||
assertEquals(expected.getExtension('x'), actual.getExtension('x')); | ||
assertEquals(expected.getScript(), actual.getScript()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this separator/marker documented somewhere? Would be great to have a link to explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#toString()