Skip to content

Commit

Permalink
Merge pull request #671 from aleqi200/master
Browse files Browse the repository at this point in the history
Adding currency deserialization support in maps
  • Loading branch information
cowtowncoder committed Jan 7, 2015
2 parents 8c36f1c + 7fced7e commit 3e0d5d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit 3e0d5d8

Please sign in to comment.