Skip to content

Commit 1e80057

Browse files
committed
README.md added
1 parent f02a6bf commit 1e80057

File tree

7 files changed

+147
-13
lines changed

7 files changed

+147
-13
lines changed

.classpath

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
4-
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
3+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/classes" path="src/main/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
615
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
726
<classpathentry kind="output" path="target/classes"/>
827
</classpath>

.project

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
<arguments>
1111
</arguments>
1212
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
1318
</buildSpec>
1419
<natures>
20+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
1521
<nature>org.eclipse.jdt.core.javanature</nature>
1622
</natures>
1723
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8

.settings/org.eclipse.jdt.core.prefs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate
88
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
99
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
1010
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
1112
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+

pom.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.json</groupId>
66
<artifactId>json</artifactId>
7-
<version>20111006</version>
7+
<version>20121205</version>
88
<packaging>jar</packaging>
99
<name>json.org</name>
1010
<description>A reference implementation of a JSON package in Java.</description>
@@ -14,9 +14,9 @@
1414
<url>https://github.com/douglascrockford/JSON-java/issues</url>
1515
</issueManagement>
1616
<scm>
17-
<connection>scm:git:https://github.com/stefanhoth/JSON-java.git</connection>
18-
<developerConnection>scm:git:ssh://github.com/stefanhoth/JSON-java.git</developerConnection>
19-
<url>https://github.com/stefanhoth/JSON-java</url>
17+
<connection>scm:git:https://github.com/samarjit/JSON-java.git</connection>
18+
<developerConnection>scm:git:ssh://github.com/samarjit/JSON-java.git</developerConnection>
19+
<url>https://github.com/samarjit/JSON-java</url>
2020
</scm>
2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -28,12 +28,6 @@
2828
<version>4.4</version>
2929
<scope>test</scope>
3030
</dependency>
31-
<dependency>
32-
<groupId>net.sf.json-lib</groupId>
33-
<artifactId>json-lib</artifactId>
34-
<version>2.4</version>
35-
<classifier>jdk15</classifier>
36-
</dependency>
3731
</dependencies>
3832
<build>
3933
<defaultGoal>install</defaultGoal>

0 commit comments

Comments
 (0)