Skip to content

Adding currency deserialization support in maps #671

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
merged 1 commit into from
Jan 7, 2015
Merged
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 @@ -6,6 +6,7 @@
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class StdKeyDeserializer extends KeyDeserializer
public final static int TYPE_URI = 13;
public final static int TYPE_URL = 14;
public final static int TYPE_CLASS = 15;
public final static int TYPE_CURRENCY = 16;

final protected int _kind;
final protected Class<?> _keyClass;
Expand Down Expand Up @@ -104,6 +106,9 @@ public static StdKeyDeserializer forType(Class<?> raw)
} else if (raw == Locale.class) {
FromStringDeserializer<?> deser = FromStringDeserializer.findDeserializer(Locale.class);
return new StdKeyDeserializer(TYPE_LOCALE, raw, deser);
} else if (raw == Currency.class) {
FromStringDeserializer<?> deser = FromStringDeserializer.findDeserializer(Currency.class);
return new StdKeyDeserializer(TYPE_CURRENCY, raw, deser);
} else {
return null;
}
Expand Down Expand Up @@ -183,7 +188,12 @@ protected Object _parse(String key, DeserializationContext ctxt) throws Exceptio
} catch (IOException e) {
throw ctxt.weirdKeyException(_keyClass, key, "unable to parse key as locale");
}

case TYPE_CURRENCY:
try {
return _deser._deserialize(key, ctxt);
} catch (IOException e) {
throw ctxt.weirdKeyException(_keyClass, key, "unable to parse key as currency");
}
case TYPE_DATE:
return ctxt.parseDate(key);
case TYPE_CALENDAR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,19 @@ public void testLocaleKeyMap() throws Exception {
assertEquals(key, ob);
}

public void testCurrencyKeyMap() throws Exception {
Currency key = Currency.getInstance("USD");
String JSON = "{ \"" + key + "\":4}";
Map<Currency, Object> result = MAPPER.readValue(JSON, new TypeReference<Map<Currency, Object>>() {
});
assertNotNull(result);
assertEquals(1, result.size());
Object ob = result.keySet().iterator().next();
assertNotNull(ob);
assertEquals(Currency.class, ob.getClass());
assertEquals(key, ob);
}

// Test confirming that @JsonCreator may be used with Map Key types
public void testKeyWithCreator() throws Exception
{
Expand Down