image credit: Ismael Pérez Ortiz
As part of https://omeda.atlassian.net/browse/ARCH-6973 we needed to upgrade our org.json library to a more recent version while also limiting the potential negative impact on our applications. Since Omeda adopted the original library, there have been several changes to validate Strings as well as duplicate keys inside json objects and arrays. Our application in various places uses .getString("key") very loosely. It does not require that an actual string was placed in the object, we just want back a String representation of the data. For example, one part of our code may do object.put("transactionId", 12345) and then later do String transactionId = object.getString("transactionId"). With the older versions of org.json this is fine, with newer ones it throws an exception. We want it to work as it always has. The second piece is duplicate keys. For our purposes we don't care if a duplicate key is set on an object - we just want whatever was last set for a key to be the assigned value. So if I do object.set("key1", "val1") then I do object.set("key1","val2"). object.get("key1") should equal "val2". For us it is particuarly important because we have a large number of client processes using our webservices that rely on this library. More stringent validation on Strings and duplicate keys could break those API calls.
Click here if you just want the latest release jar file.
JSON is a light-weight language-independent data interchange format.
The JSON-Java package is a reference implementation that demonstrates how to parse JSON documents into Java objects and how to generate new JSON documents from the Java classes.
Project goals include:
- Reliable and consistent results
- Adherence to the JSON specification
- Easy to build, use, and include in other projects
- No external dependencies
- Fast execution and low memory footprint
- Maintain backward compatibility
- Designed and tested to use on Java versions 1.6 - 1.11
The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL.
For more information on contributions, please see CONTRIBUTING.md
Bug fixes, code improvements, and unit test coverage changes are welcome! Because this project is currently in the maintenance phase, the kinds of changes that can be accepted are limited. For more information, please read the FAQ.
The org.json package can be built from the command line, Maven, and Gradle. The unit tests can be executed from Maven, Gradle, or individually in an IDE e.g. Eclipse.
Building from the command line
Build the class files from the package root directory src/main/java
javac org/json/*.java
Create the jar file in the current directory
jar cf json-java.jar org/json/*.class
Compile a program that uses the jar (see example code below)
javac -cp .;json-java.jar Test.java (Windows)
javac -cp .:json-java.jar Test.java (Unix Systems)
Test file contents
import org.json.JSONObject;
public class Test {
public static void main(String args[]){
JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
System.out.println(jo.toString());
}
}
Execute the Test file
java -cp .;json-java.jar Test (Windows)
java -cp .:json-java.jar Test (Unix Systems)
Expected output
{"abc":"def"}
Tools to build the package and execute the unit tests
Execute the test suite with Maven:
mvn clean test
Execute the test suite with Gradlew:
gradlew clean build test
For more information, please see NOTES.md
For more information on files, please see FILES.md
For the release history, please see RELEASES.md