JSON-Java (org.json)

repository·master·Indexed 26 days ago

https://github.com/stleary/json-java

A lightweight, dependency-free reference implementation for handling JSON in Java. It provides tools for parsing JSON into Java objects, generating JSON from Java classes, and converting between JSON and other formats such as XML, HTTP headers, Cookies, and CDL. Designed for Java versions 1.6 through 25, it includes key classes like JSONObject, JSONArray, and JSONPointer for RFC 6901 compliance.

Tokens
2K
Snippets
5
Records
22
Agent score
82%

What's inside JSON-Java

  1. Overview of JSON-Java (org.json)

    master

    JSON-Java is a reference implementation for parsing JSON documents into Java objects and generating new JSON documents from Java classes. It provides encoders and decoders and supports conversions between JSON and XML, HTTP headers, Cookies, and CDL.

    Key features:

    • No external dependencies.
    • Designed for Java versions 1.6 through 25.
    • Fast execution and low memory footprint.
    • Adheres to the JSON specification.
  2. Import the org.json signing key

    master

    To verify that artifacts are authentic, you can import the project's PGP signing key from the Ubuntu keyserver using GPG. After importing, verify that the fingerprint matches the canonical identifier: FB35 C8D0 2B47 24DA DA23 DE0A FD11 6C19 69FC CFF3.

    # Import the key
    gpg --keyserver hkps://keyserver.ubuntu.com \
        --recv-keys FB35C8D02B4724DADA23DE0AFD116C1969FCCFF3
    
    # Confirm the fingerprint
    gpg --fingerprint FB35C8D02B4724DADA23DE0AFD116C1969FCCFF3
  3. Verify an org.json artifact signature

    master

    All releases of org.json:json published to Maven Central are signed with PGP. To verify an artifact (such as a .jar or .pom file), download both the artifact and its detached .asc signature, then use gpg --verify. A successful verification will report Good signature from ... and match the published fingerprint.

    # Example for version 20251224
    curl -O https://repo1.maven.org/maven2/org/json/json/20251224/json-20251224.jar
    curl -O https://repo1.maven.org/maven2/org/json/json/20251224/json-20251224.jar.asc
    gpg --verify json-20251224.jar.asc json-20251224.jar
  4. Ensure consistency when using JSONArray.putAll()

    master

    The putAll method on JSONArray does not call JSONObject.wrap for items added. This can result in a mixed JSONArray where some items are wrapped in JSONObjects and others are not, depending on how the array was initially constructed.

    To maintain a consistent structure, avoid mixing the JSONArray(Object[]) constructor with putAll(Object[]). Instead, use one of the following patterns:

    Option 1: Use putAll for all additions

    SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
    JSONArray jArr = new JSONArray();
    // these will not be wrapped
    jArr.putAll(myArr);
    // these will not be wrapped
    jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });

    Option 2: Wrap all additions in JSONArray first

    SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
    // these will be wrapped
    JSONArray jArr = new JSONArray(myArr);
    // these will be wrapped
    jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
    SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
    // these will be wrapped
    JSONArray jArr = new JSONArray(myArr);
    // these will be wrapped
    jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
  5. Compile and run a program using the JSON-Java JAR

    master
    To compile and execute a Java program that depends on the json-java.jar, include the JAR in your classpath using the -cp flag. Note the syntax difference between Windows (using ;) and Unix-based systems (using :).
  6. Configure Gradle dependency verification for org.json

    master

    If using Gradle's dependency verification feature, add the project's signing key fingerprint to your gradle/verification-metadata.xml file to ensure the integrity of the org.json:json dependency.

    <trusted-key id="FB35C8D02B4724DADA23DE0AFD116C1969FCCFF3" group="org.json" name="json"/>
  7. Verify JSON-Java installation with a Test example

    master

    Use the following code to verify that your json-java.jar is working correctly. This example initializes a JSONObject with a JSON string and prints it to the console.

    import org.json.JSONObject;
    
    public class Test {
        public static void main(String args[]){
           JSONObject jo = new JSONObject("{ "abc" : "def" }");
           System.out.println(jo);
        }
    }

    Expected output:

    {"abc":"def"}
  8. Convert JSON to and from other formats

    master

    The library provides several utilities for format conversion:

    • CDL: Convert between JSON and comma delimited lists.
    • Cookie / CookieList: Convert between JSON and cookies or cookie lists.
    • HTTP / HTTPTokener: Convert between JSON and HTTP headers (with HTTPTokener specifically for parsing headers).
    • XML / XMLTokener: Convert between JSON and XML (with XMLTokener specifically for parsing XML text).
    • JSONML: Convert between JSONML and XML.