Skip to content

Commit 7fced7e

Browse files
committed
Adding currency deserialization support in maps
1 parent 560bd80 commit 7fced7e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URI;
77
import java.net.URL;
88
import java.util.Calendar;
9+
import java.util.Currency;
910
import java.util.Date;
1011
import java.util.Locale;
1112
import java.util.UUID;
@@ -46,6 +47,7 @@ public class StdKeyDeserializer extends KeyDeserializer
4647
public final static int TYPE_URI = 13;
4748
public final static int TYPE_URL = 14;
4849
public final static int TYPE_CLASS = 15;
50+
public final static int TYPE_CURRENCY = 16;
4951

5052
final protected int _kind;
5153
final protected Class<?> _keyClass;
@@ -104,6 +106,9 @@ public static StdKeyDeserializer forType(Class<?> raw)
104106
} else if (raw == Locale.class) {
105107
FromStringDeserializer<?> deser = FromStringDeserializer.findDeserializer(Locale.class);
106108
return new StdKeyDeserializer(TYPE_LOCALE, raw, deser);
109+
} else if (raw == Currency.class) {
110+
FromStringDeserializer<?> deser = FromStringDeserializer.findDeserializer(Currency.class);
111+
return new StdKeyDeserializer(TYPE_CURRENCY, raw, deser);
107112
} else {
108113
return null;
109114
}
@@ -183,7 +188,12 @@ protected Object _parse(String key, DeserializationContext ctxt) throws Exceptio
183188
} catch (IOException e) {
184189
throw ctxt.weirdKeyException(_keyClass, key, "unable to parse key as locale");
185190
}
186-
191+
case TYPE_CURRENCY:
192+
try {
193+
return _deser._deserialize(key, ctxt);
194+
} catch (IOException e) {
195+
throw ctxt.weirdKeyException(_keyClass, key, "unable to parse key as currency");
196+
}
187197
case TYPE_DATE:
188198
return ctxt.parseDate(key);
189199
case TYPE_CALENDAR:

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,19 @@ public void testLocaleKeyMap() throws Exception {
463463
assertEquals(key, ob);
464464
}
465465

466+
public void testCurrencyKeyMap() throws Exception {
467+
Currency key = Currency.getInstance("USD");
468+
String JSON = "{ \"" + key + "\":4}";
469+
Map<Currency, Object> result = MAPPER.readValue(JSON, new TypeReference<Map<Currency, Object>>() {
470+
});
471+
assertNotNull(result);
472+
assertEquals(1, result.size());
473+
Object ob = result.keySet().iterator().next();
474+
assertNotNull(ob);
475+
assertEquals(Currency.class, ob.getClass());
476+
assertEquals(key, ob);
477+
}
478+
466479
// Test confirming that @JsonCreator may be used with Map Key types
467480
public void testKeyWithCreator() throws Exception
468481
{

0 commit comments

Comments
 (0)