JSON and Generics in Java - Type safety warning

后端 未结 7 1425
逝去的感伤
逝去的感伤 2020-12-10 05:05

I have some data stored in Java elements and I need to return it in a given format - JSONObject. While my implementation works fine, I\'m still getting a warning message fro

相关标签:
7条回答
  • 2020-12-10 05:43

    If you can't switch to another library or modify the code of this library to make it generic, the only other option would be to write a wrapper around this library which uses it, and properly supports generics.

    So you would have your own JSONObject class which would contain an org.json.simple.JSONObject, would extend HashMap<String, Object> and implement Map<String, Object>, and would contain forwarding methods for all the methods of org.json.simple.JSONObject.

    You would still have to put @SuppressWarnings("unchecked") in this class, but it would be limited to this class, and all the rest of your code could be free of generic warnings or the suppression of them.

    0 讨论(0)
提交回复
热议问题