Skip to content

ISmillex/jsonic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Jsonic

Jsonic is a lightweight JSON parsing and generation library for Java. It provides a simple, object-oriented API for working with JSON data without external runtime dependencies.

Features

Jsonic offers a straightforward approach to JSON manipulation with the following capabilities:

JSON Parsing - Parse JSON from strings or Reader objects with a clean, type-safe API

JSON Generation - Build JSON structures programmatically using a fluent builder pattern

Type Safety - Access JSON values with type-specific methods like asObject(), asArray(), asString()

Minimal Dependencies - Zero runtime dependencies, keeping your project lean

Simple API - Intuitive methods that make JSON manipulation straightforward

Requirements

Java 21 or higher

Maven 3.x for building

Installation

Clone the repository and build with Maven:

git clone https://github.com/ISmillex/jsonic.git
cd jsonic
mvn clean install

Usage

Parsing JSON

Parse JSON from a string and access values:

import org.smilex.jsonic.Jsonic;
import org.smilex.jsonic.JValue;

String jsonString = """
    {
        "name": "John Doe",
        "age": 30,
        "active": true
    }
    """;

JValue json = Jsonic.parse(jsonString);
String name = json.asObject().get("name").asString();
int age = json.asObject().get("age").asNumber().intValue();
boolean active = json.asObject().get("active").asLiteral().booleanValue();

Building JSON

Create JSON structures programmatically:

import org.smilex.jsonic.Jsonic;
import org.smilex.jsonic.JObject;
import org.smilex.jsonic.JArray;

JObject person = Jsonic.object()
    .add("name", "Jane Smith")
    .add("email", "jane@example.com")
    .add("roles", Jsonic.array()
        .add("admin")
        .add("user"));

String jsonString = person.toString();

Working with Arrays

JArray items = Jsonic.array();
items.add(Jsonic.object()
    .add("id", 1)
    .add("product", "Widget")
    .add("price", 19.99));
items.add(Jsonic.object()
    .add("id", 2)
    .add("product", "Gadget")
    .add("price", 29.99));

// Access array elements
for (JValue item : items) {
    String product = item.asObject().get("product").asString();
    double price = item.asObject().get("price").asNumber().doubleValue();
}

Parsing from Reader

try (Reader reader = new FileReader("data.json")) {
    JValue json = Jsonic.parse(reader);
    // Process JSON data
}

API Overview

Core Classes

Jsonic - Main factory class with static methods for creating and parsing JSON

JValue - Abstract base class for all JSON values

JObject - Represents JSON objects (key-value pairs)

JArray - Represents JSON arrays

JString - Represents JSON strings

JNumber - Represents JSON numbers

JLiteral - Represents JSON literals (null, true, false)

Key Methods

Jsonic.parse(String) - Parse JSON from a string

Jsonic.parse(Reader) - Parse JSON from a Reader

Jsonic.object() - Create a new JSON object

Jsonic.array() - Create a new JSON array

JValue.asObject() - Cast value to JObject

JValue.asArray() - Cast value to JArray

JValue.asString() - Cast value to JString

JValue.asNumber() - Cast value to JNumber

JValue.asLiteral() - Cast value to JLiteral

Building from Source

The project uses Maven for build management. To build:

mvn clean compile

To run tests:

mvn test

To create a JAR file:

mvn package

License

This project is open source. Please check the LICENSE file for details.

Contributing

Contributions are welcome. Please submit pull requests or open issues for bugs and feature requests.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages