Menu

#89 Maps with non-String keys throw ClassCastException

closed-fixed
aalmiray
None
5
2009-07-11
2009-05-22
Anonymous
No

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 );

Discussion

  • aalmiray

    aalmiray - 2009-06-09
    • assigned_to: nobody --> aalmiray
     
  • aalmiray

    aalmiray - 2009-06-09

    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.

     
  • Shifra

    Shifra - 2009-06-30

    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.

     
  • aalmiray

    aalmiray - 2009-07-11
    • status: open --> closed-fixed
     
  • aalmiray

    aalmiray - 2009-07-11

    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.

     

Log in to post a comment.

MongoDB Logo MongoDB