Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,10 @@ public Locale read(JsonReader in) throws IOException {
if (tokenizer.hasMoreElements()) {
variant = tokenizer.nextToken();
}
if (country == null && variant == null) {
if (language == null) {
return Locale.ROOT;
}
if (country == null) {
return new Locale(language);
} else if (variant == null) {
return new Locale(language, country);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ public void testLocaleDeserializationWithLanguage() {
assertThat(locale.getLanguage()).isEqualTo("en");
}

@Test
public void testLocaleDeserializationRoot() {
// Gson serializes Locale.ROOT to "", so it must read "" back to Locale.ROOT.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness it would probably be good to have also a serialization test which shows that Locale.ROOT is actually serialized as "\"\"".

String json = "\"\"";
Locale locale = gson.fromJson(json, Locale.class);
assertThat(locale).isEqualTo(Locale.ROOT);
}

@Test
public void testLocaleSerializationRoot() {
// Locale.ROOT round-trips: it is serialized as an empty string.
assertThat(gson.toJson(Locale.ROOT)).isEqualTo("\"\"");
}

@Test
public void testLocaleSerializationWithLanguageCountry() {
Locale target = Locale.CANADA_FRENCH;
Expand Down