Maps with non-String keys throw ClassCastException
Brought to you by:
aalmiray
In json-lib 2.2.2, I was able to use a non-string key in a map (e.g. an Enum) However, json-lib 2.2.3 appears to have made this illegal and throws a ClassCastException.
Perhaps there should be an option in the JsonConfig that allows non-String keys. Enabling this would revert to the old behavior. Namely:
String key = (k instanceof String) ? (String) k : String.valueOf( k );
Allowing non-String keys was a mistake in the first place, however a backwards compatible flag does not sound that far fetched. Non-String keys will be disallowed by default though.
I'm getting this error even when my app thinks I'm passing keys that are strings. I'm going to monkey-patch locally for us so that JSONObject will toString any key that it thinks is not a string... it's an extremely simple patch, but let me know if you want the code snippet.
Added new flag allowNonStringKeys to JsonConfig. Keys will always be converted to Strings using String.valueOf(), this flag will prevent throwing a CCE when true, default setting is false.