Skip to content

Commit 07b6d66

Browse files
committed
Fix #1344
1 parent e979bc5 commit 07b6d66

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

release-notes/VERSION

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Project: jackson-databind
1010
(reported by MichaelChambers@github)
1111
#1332: Fixed ArrayIndexOutOfBoundException for enum by index deser
1212
(reported by Max D)
13+
#1344: Deserializing locale assumes JDK separator (underscore), does not
14+
accept RFC specified (hyphen)
15+
(reported by Jim M)
1316

1417
2.7.6 (23-Jul-2016)
1518

src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ protected Object _deserialize(String value, DeserializationContext ctxt) throws
228228
return Pattern.compile(value);
229229
case STD_LOCALE:
230230
{
231-
int ix = value.indexOf('_');
231+
int ix = _firstHyphenOrUnderscore(value);
232232
if (ix < 0) { // single argument
233233
return new Locale(value);
234234
}
235235
String first = value.substring(0, ix);
236236
value = value.substring(ix+1);
237-
ix = value.indexOf('_');
237+
ix = _firstHyphenOrUnderscore(value);
238238
if (ix < 0) { // two pieces
239239
return new Locale(first, value);
240240
}
@@ -287,5 +287,17 @@ protected Object _deserializeFromEmptyString() throws IOException {
287287
}
288288
return super._deserializeFromEmptyString();
289289
}
290+
291+
292+
protected int _firstHyphenOrUnderscore(String str)
293+
{
294+
for (int i = 0, end = str.length(); i < end; ++i) {
295+
char c = str.charAt(i);
296+
if (c == '_' || c == '-') {
297+
return i;
298+
}
299+
}
300+
return -1;
301+
}
290302
}
291303
}

src/test/java/com/fasterxml/jackson/databind/deser/TestJdkTypes.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public BooleanBean(@JsonProperty("ctor") Boolean foo) {
6868
}
6969
}
7070

71-
// [Issue#429]
71+
// [databind#429]
7272
static class StackTraceBean {
7373
public final static int NUM = 13;
7474

@@ -145,6 +145,9 @@ public void testLocale() throws IOException
145145
assertEquals(new Locale("es", "ES"), MAPPER.readValue(quote("es_ES"), Locale.class));
146146
assertEquals(new Locale("FI", "fi", "savo"),
147147
MAPPER.readValue(quote("fi_FI_savo"), Locale.class));
148+
assertEquals(new Locale("en", "US"),
149+
MAPPER.readValue(quote("en-US"), Locale.class));
150+
148151
// [databind#1123]
149152
Locale loc = MAPPER.readValue(quote(""), Locale.class);
150153
assertSame(Locale.ROOT, loc);

src/test/java/com/fasterxml/jackson/databind/ser/TestJdkTypes.java

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public void testLocale() throws IOException
7474
assertEquals(quote("es_ES"), MAPPER.writeValueAsString(new Locale("es", "ES")));
7575
assertEquals(quote("fi_FI_savo"), MAPPER.writeValueAsString(new Locale("FI", "fi", "savo")));
7676

77+
assertEquals(quote("en_US"), MAPPER.writeValueAsString(Locale.US));
78+
7779
// [databind#1123]
7880
assertEquals(quote(""), MAPPER.writeValueAsString(Locale.ROOT));
7981
}

0 commit comments

Comments
 (0)