Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# compiled files
target
target
src/test/java/IdctXMLTest.java
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JSON-java_stefanhoth</name>
<comment>A reference implementation of a JSON package in Java. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.6
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#This JSON library for java, is a fork from
#Original [package org.json] Douglas Crockford

##Version 20121208
I finally made these changes, no reason for not having done before.
* JSONObject implements Map
* JSONArray implements List
* Added separate tests for Ognl and Freemarker. Yet to include them in regression test.

##Version 20121205
The key changes are:
##JSONObject is extended from LinkedHashMap

* Ensures order of elements
* Enables the normal HashMap methods to be available although most of them were overridden in earlier implementation like keys() and keySet()
* Main reason to do this is to enable libraries which works on HashMap and ArrayLists to be able to work on JSONObject
* json-lib project implements Map which is probably an even better approach. But it also gets the same benefits as above point.

> Known Issue:
> *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.
> 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.
> In JDK6 and lower versions the order is maintained however this is a feature not mentioned in specifications of jdk.

##JSONArray extends from ArrayList
* 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>>.


##XML to JSON Serialization
* Attributes are marked with a '@' prepended to the key
* Content element is marked with a '#' prepended to the key
* Since the json keeps order of the original xml elements it can be deserialized back to same xml.
* Type inference is removed, every value is treated as string

#### Earlier version of JSON below refers to release version 20090211 or before.

XML to JSON to XML conversion
```java
//This version 20121205 on wards
<root><person fname="samarjit" lname="samanta" >normal text</person><version>1.0</version></root>

//This version XML -> JSON
{"root":{"person":{"@fname":"samarjit","@lname":"samanta","#content":"normal text"},"version":"1.0"}}
//This version JSON -> XML
<root><person fname="samarjit" lname="samanta">normal text</person><version>1.0</version></root>


//## Earlier version json ##
<root><person fname="samarjit" lname="samanta" >normal text</person><version>1.0</version></root>
//Earlier version XML->JSON
{"root":{"person":{"content":"normal text","lname":"samanta","fname":"samarjit"},"version":1}}
//Earlier version JSON-> XML
<root><person>normal text<lname>samanta</lname><fname>samarjit</fname></person><version>1.0</version></root>

```

####Example of libraries that uses model as combination of Map<String,Object> and List<Object> and arbitrary java beans.

### Freemarker
```java
String templateExpression = "Hi ${ddd} hello ${ar[0]} your home is ${USER_HOME}";

Template t = new Template("name", new StringReader(templateExpression), new Configuration());
StringWriter out = new StringWriter();

JSONObject jobj1 = new JSONObject();
jobj1.put("ddd","jsss");
JSONArray ar = new JSONArray("['jhaldia','jdob']");
jobj1.put("ar", ar);
jobj1.put("USER_HOME", System.getProperty("user.home").replace("\\","/"));

t.process(jobj1, out );

String ret = out.toString();

System.out.println(ret);
assertEquals("Hi jsss hello jhaldia your home is "+System.getProperty("user.home").replace("\\","/"),ret);
```

```java
Result
//This version produces
Hi jsss hello jhaldia your home is C:/Users/Samarjit

//Earlier version json:
Hi jsss hello [ your home is C:/Users/Samarjit
```

### Ognl
```java
Map rootObject = new HashMap();
Map context = new HashMap();
JSONObject jobj1 = new JSONObject();
jobj1.put("ddd","jsss");
JSONArray ar = new JSONArray("['jhaldia','jdob']");
jobj1.put("ar", ar);
context.put("someBean", jobj1);
System.out.println(Ognl.getValue("ar[0]", rootObject , jobj1 ));
assertEquals("jhaldia",Ognl.getValue("ar[0]", rootObject , jobj1 ));
Object obj2 = Ognl.getValue("someBean.ddd",context);
System.out.println(obj2);
assertEquals("jsss", obj2);
```


```java
Result
//This version produces
jhaldia
dddd

//Earlier version json
ognl.NoSuchPropertyException: org.json.JSONObject.ar
```


> Please refer to the original README for other functions.

22 changes: 17 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20111006</version>
<version>20121208</version>
<packaging>jar</packaging>
<name>json.org</name>
<description>A reference implementation of a JSON package in Java.</description>
Expand All @@ -14,9 +14,9 @@
<url>https://github.com/douglascrockford/JSON-java/issues</url>
</issueManagement>
<scm>
<connection>scm:git:https://github.com/stefanhoth/JSON-java.git</connection>
<developerConnection>scm:git:ssh://github.com/stefanhoth/JSON-java.git</developerConnection>
<url>https://github.com/stefanhoth/JSON-java</url>
<connection>scm:git:https://github.com/samarjit/JSON-java.git</connection>
<developerConnection>scm:git:ssh://github.com/samarjit/JSON-java.git</developerConnection>
<url>https://github.com/samarjit/JSON-java</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -28,7 +28,19 @@
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
Expand Down
Loading