Convert Figma logo to code with AI

stleary logoJSON-java

A reference implementation of a JSON package in Java.

4,511
2,557
4,511
37

Top Related Projects

23,230

A Java serialization/deserialization library to convert Java Objects into JSON and back

9,031

Main Portal page for the Jackson project

25,713

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.

1,505

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go

9,685

A modern JSON library for Kotlin and Java.

Quick Overview

The JSON-java project is a Java library that provides a simple way to parse, generate, and manipulate JSON data. It is a well-established and widely-used library for working with JSON in Java applications.

Pros

  • Simplicity: The library has a straightforward and easy-to-use API, making it accessible for developers of all skill levels.
  • Flexibility: The library supports a wide range of JSON features, including arrays, objects, numbers, strings, booleans, and null values.
  • Performance: The library is designed to be efficient and fast, with a focus on performance and scalability.
  • Widespread Adoption: The JSON-java library is widely used in the Java community, with a large and active user base.

Cons

  • Limited Functionality: While the library is powerful, it may not provide all the advanced features and functionality that some developers might require, especially for more complex JSON use cases.
  • Lack of Active Maintenance: The project has not been actively maintained in recent years, which could be a concern for some users who require the latest features and bug fixes.
  • Dependency Management: The library has a few dependencies, which may need to be managed carefully in some projects.
  • Limited Documentation: The project's documentation, while generally good, could be more comprehensive and up-to-date.

Code Examples

Here are a few examples of how to use the JSON-java library:

  1. Parsing JSON:
import org.json.JSONObject;

String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject jsonObject = new JSONObject(jsonString);

String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");

System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
  1. Generating JSON:
import org.json.JSONObject;

JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
jsonObject.put("city", "New York");

String jsonString = jsonObject.toString();
System.out.println(jsonString);
  1. Modifying JSON:
import org.json.JSONObject;

String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject jsonObject = new JSONObject(jsonString);

jsonObject.put("age", 31);
jsonObject.remove("city");
jsonObject.put("country", "USA");

String updatedJsonString = jsonObject.toString();
System.out.println(updatedJsonString);
  1. Handling JSON Arrays:
import org.json.JSONArray;
import org.json.JSONObject;

String jsonString = "{\"name\":\"John\",\"hobbies\":[\"reading\",\"swimming\",\"hiking\"]}";
JSONObject jsonObject = new JSONObject(jsonString);

JSONArray hobbiesArray = jsonObject.getJSONArray("hobbies");
for (int i = 0; i < hobbiesArray.length(); i++) {
    String hobby = hobbiesArray.getString(i);
    System.out.println("Hobby: " + hobby);
}

Getting Started

To get started with the JSON-java library, you can follow these steps:

  1. Add the library to your project's dependencies. You can download the latest release from the GitHub repository or use a dependency management tool like Maven or Gradle.

  2. Import the necessary classes from the org.json package in your Java code.

  3. Use the provided classes and methods to parse, generate, and manipulate JSON data in your application.

Here's an example of how to get started with the library:

import org.json.JSONObject;

// Parse a JSON string
String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject jsonObject = new JSONObject(jsonString);

// Access JSON

Competitor Comparisons

23,230

A Java serialization/deserialization library to convert Java Objects into JSON and back

Pros of google/gson

  • Simplicity: Gson provides a straightforward and easy-to-use API for serializing and deserializing JSON data.
  • Flexibility: Gson supports a wide range of data types and can handle complex JSON structures with ease.
  • Performance: Gson is generally faster than the JSON-java library, especially for large JSON payloads.

Cons of google/gson

  • Dependency Management: Gson is a standalone library, which means it needs to be included as a dependency in your project, adding to the overall project size.
  • Limited Customization: Gson has fewer customization options compared to JSON-java, which may limit its flexibility in certain use cases.
  • Lack of Streaming API: Gson does not provide a streaming API, which can be useful for processing large JSON datasets.

Code Comparison

JSON-java:

JSONObject obj = new JSONObject();
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("email", "john.doe@example.com");

Gson:

Gson gson = new Gson();
JsonObject obj = new JsonObject();
obj.addProperty("name", "John Doe");
obj.addProperty("age", 30);
obj.addProperty("email", "john.doe@example.com");
9,031

Main Portal page for the Jackson project

Pros of Jackson

  • Supports a wider range of JSON features, including advanced data types and annotations.
  • Provides a more comprehensive set of utilities and tools for working with JSON data.
  • Offers better performance and scalability compared to JSON-java.

Cons of Jackson

  • Has a steeper learning curve due to its more complex API and feature set.
  • May be overkill for simple JSON parsing tasks, where JSON-java could be a more lightweight option.
  • Requires additional dependencies, which can increase the overall project size.

Code Comparison

JSON-java:

JSONObject obj = new JSONObject();
obj.put("name", "John Doe");
obj.put("age", 30);

Jackson:

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> data = new HashMap<>();
data.put("name", "John Doe");
data.put("age", 30);
String json = mapper.writeValueAsString(data);
25,713

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.

Pros of Alibaba/fastjson

  • Performance: Fastjson is known for its exceptional performance, often outperforming other JSON parsing libraries in terms of speed and efficiency.
  • Comprehensive Feature Set: Fastjson provides a wide range of features, including support for custom serializers/deserializers, annotations, and advanced data binding capabilities.
  • Active Development: The Fastjson project is actively maintained and regularly updated, ensuring compatibility with the latest JSON standards and addressing any issues or vulnerabilities.

Cons of Alibaba/fastjson

  • Security Concerns: Fastjson has faced some security vulnerabilities in the past, which have raised concerns about its overall security posture.
  • Licensing: Fastjson is licensed under the Apache License 2.0, which may not be compatible with all project requirements or preferences.
  • Ecosystem Integration: Compared to JSON-java, Fastjson may have a smaller ecosystem of integrations and third-party libraries, which could limit its adoption in certain environments.

Code Comparison

JSON-java:

JSONObject obj = new JSONObject();
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("email", "john.doe@example.com");
System.out.println(obj.toString());

Fastjson:

JSONObject obj = new JSONObject();
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("email", "john.doe@example.com");
System.out.println(JSON.toJSONString(obj));

The code snippets demonstrate the basic usage of both JSON-java and Fastjson for creating a JSON object, adding key-value pairs, and serializing the object to a JSON string. The main difference is in the serialization method, where Fastjson uses the JSON.toJSONString() function, while JSON-java uses the toString() method.

1,505

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go

Pros of json-iterator/java

  • Faster performance compared to stleary/JSON-java, with claims of up to 2x faster parsing and serialization.
  • Supports a wider range of data types, including BigDecimal, BigInteger, and AtomicInteger.
  • Provides a more flexible and customizable API, allowing for easier integration with different use cases.

Cons of json-iterator/java

  • Smaller community and fewer contributors compared to the well-established stleary/JSON-java project.
  • May have a steeper learning curve due to its more complex and feature-rich API.
  • Potential compatibility issues with existing code that relies on the stleary/JSON-java library.

Code Comparison

stleary/JSON-java:

JSONObject obj = new JSONObject();
obj.put("name", "John");
obj.put("age", 30);
obj.put("city", "New York");
System.out.println(obj.toString());

json-iterator/java:

JsonObject obj = JsonObject.builder()
    .add("name", "John")
    .add("age", 30)
    .add("city", "New York")
    .build();
System.out.println(obj.toString());
9,685

A modern JSON library for Kotlin and Java.

Pros of Moshi

  • Annotation-based configuration: Moshi provides a more flexible and extensible approach to JSON serialization and deserialization, allowing developers to customize the process using annotations.
  • Kotlin support: Moshi has first-class support for Kotlin, making it a great choice for Kotlin-based projects.
  • Adapter-based architecture: Moshi's adapter-based architecture allows for easy integration with other libraries and custom data types.

Cons of Moshi

  • Larger dependency footprint: Moshi has a larger dependency footprint compared to JSON-java, which may be a concern for projects with strict size requirements.
  • Steeper learning curve: Moshi's more advanced features and annotation-based configuration may have a steeper learning curve for developers who are new to the library.

Code Comparison

JSON-java:

JSONObject obj = new JSONObject();
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("email", "john.doe@example.com");
System.out.println(obj.toString());

Moshi:

val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(Person::class.java)

val person = Person("John Doe", 30, "john.doe@example.com")
val json = jsonAdapter.toJson(person)
println(json)

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Json-Java logo

image credit: Ismael Pérez Ortiz

JSON in Java [package org.json]

Maven Central Java CI with Maven CodeQL

Click here if you just want the latest release jar file.

Overview

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 - 21

The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL.

If you would like to contribute to this project

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.

Build Instructions

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);
    }
}

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

Notes

For more information, please see NOTES.md

Files

For more information on files, please see FILES.md

Release history:

For the release history, please see RELEASES.md