|
| 1 | +#This JSON library for java is a fork from in Java [package org.json] Douglas Crockford |
| 2 | + |
| 3 | +The key changes are: |
| 4 | +##JSONObject is extended from LinkedHashMap ## |
| 5 | + |
| 6 | +* Ensures order of elements |
| 7 | +* Enables the normal HashMap methods to be available although most of them were overridden in earlier implementation like keys() and keySet() |
| 8 | +* Main reason to do this is to enable libraries which works on HashMap and ArrayLists to be able to work on JSONObject |
| 9 | +* json-lib project implements Map which is probably an even better approach. But it also gets the same benefits as above point. |
| 10 | + |
| 11 | +> Known Issue: |
| 12 | +> *In JDK7 The order of elements serialized from a bean or local ResourceBundle provided through a class will not maintian the order of elements in JSONObject. |
| 13 | +> This is because in JDK 7 getDeclaredMethods() does not retrieve the methods in order it is random and can vary from time to time. A bug was logged for jdk but was rejected as wont fix. |
| 14 | +> In JDK6 and lower versions the order is maintained however this is a feature not mentioned in specifications of jdk. |
| 15 | +
|
| 16 | +##JSONArray extends from ArrayList## |
| 17 | +* This in combination with JSONObjects extends from LinkedHashMap enables the whole library to be usable in a generic way where other libraries which uses model data in any combination of Map<String, Object> and List<Object> for example Map<String,List<Object>>. |
| 18 | + |
| 19 | + |
| 20 | +##XML to JSON Serialization## |
| 21 | +* Attributes are marked with a '@' prepended to the key |
| 22 | +* Content element is marked with a '#' prepended to the key |
| 23 | +* Since the json keeps order of the original xml elements it can be deserialized back to same xml. |
| 24 | + |
| 25 | + |
| 26 | +An xml of format |
| 27 | + |
| 28 | + <root><person fname="samarjit" lname="samanta" >normal text</person></root> |
| 29 | + |
| 30 | +will serialize to |
| 31 | + |
| 32 | + {"root":{"person":{"@fname":"samarjit","@lname":"samanta","#content":"normal text"}}} |
| 33 | + |
| 34 | + |
| 35 | +In earlier version it used to serialize to |
| 36 | + |
| 37 | + {"root":{"person":{"fname":"samarjit","lname":"samanta","content":"normal text"}}} |
| 38 | + |
| 39 | +##JSON to XML Serialization## |
| 40 | + |
| 41 | +Since we have the attribute marked in the json, we can create back the exact xml |
| 42 | +if input xml is |
| 43 | + |
| 44 | + {"root":{"person":{"@fname":"samarjit","@lname":"samanta","#content":"normal text"}}} |
| 45 | + |
| 46 | +this will produce |
| 47 | + |
| 48 | + <root><person fname="samarjit" lname="samanta">normal text</person></root> |
| 49 | + |
| 50 | +old version of JSON eg. release version 20090211 or before will produce |
| 51 | + |
| 52 | +Since the json produced is |
| 53 | + {"root":{"person":{"content":"normal text","lname":"samanta","fname":"samarjit"}}} |
| 54 | +It will produce |
| 55 | + <root><person>normal text<lname>samanta</lname><fname>samarjit</fname></person></root> |
| 56 | + |
| 57 | + |
| 58 | +Example of libraries that uses model as combination of Map<String,Object> and List<Object> and arbitrary java beans. |
| 59 | + |
| 60 | +### Freemarker ### |
| 61 | + String templateExpression = "Hi ${ddd} hello ${ar[0]} your home is ${USER_HOME}"; |
| 62 | + |
| 63 | + Template t = new Template("name", new StringReader(templateExpression), new Configuration()); |
| 64 | + StringWriter out = new StringWriter(); |
| 65 | + |
| 66 | + JSONObject jobj1 = new JSONObject(); |
| 67 | + jobj1.put("ddd","jsss"); |
| 68 | + JSONArray ar = new JSONArray("['jhaldia','jdob']"); |
| 69 | + jobj1.put("ar", ar); |
| 70 | + jobj1.put("USER_HOME", System.getProperty("user.home").replace("\\","/")); |
| 71 | + |
| 72 | + t.process(jobj1, out ); |
| 73 | + |
| 74 | + String ret = out.toString(); |
| 75 | + |
| 76 | + System.out.println(ret); |
| 77 | +Result |
| 78 | +This version produces |
| 79 | + Hi jsss hello jhaldia your home is C:/Users/Samarjit |
| 80 | + |
| 81 | +Earlier version json: |
| 82 | + Hi jsss hello [ your home is C:/Users/Samarjit |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### Ognl ### |
| 87 | + JSONObject jobj1 = new JSONObject(); |
| 88 | + jobj1.put("ddd","jsss"); |
| 89 | + JSONArray ar = new JSONArray("['jhaldia','jdob']"); |
| 90 | + jobj1.put("ar", ar); |
| 91 | + System.out.println(Ognl.getValue("ar[0]", rootObject , jobj1 )); |
| 92 | + Object obj2 = Ognl.getValue("someBean.sss",context); |
| 93 | + System.out.println(obj2); |
| 94 | + |
| 95 | +Result |
| 96 | +This version produces |
| 97 | + jhaldia |
| 98 | + dddd |
| 99 | + |
| 100 | +Earlier version json |
| 101 | + ognl.NoSuchPropertyException: org.json.JSONObject.ar |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +Please refer to the original README for other functions. |
| 106 | + |
0 commit comments