jaimiex / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

JSONObject should use composition instead of extending HashMap #29

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
JSONObject extends HashMap. Effective Java by Joshua Bloch suggests that 
JSONObject should not extend HashMap. Instead JSONObject should use HashMap as 
a private field and be delegated to instead.

http://java.sun.com/docs/books/effective/toc.html
Item 16: Favor composition over inheritance.

Original issue reported on code.google.com by uvico...@gmail.com on 10 Jul 2010 at 5:08

GoogleCodeExporter commented 8 years ago
We describe explicitly that a JSON object in Java is a Map instead a HashMap. 
We leverage the popular existing Map implementation HashMap to make JSONObject 
simpler and let JSONObject focus on JSON encoding related stuff only. I think 
"Favor composition over inheritance" is generally correct and is the best 
practice for most cases, but I think in some special cases we can also use 
inheritance reasonably base on our needs.

On the other hand, currently we don't force user to use JSONObject and user can 
use any map implementation:

http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-3_-_Encode_
a_JSON_object_-_Using_Map

Original comment by fangyid...@gmail.com on 11 Jul 2010 at 4:11