CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-hjson--hjson

Hjson (Human JSON) configuration file format library for Java providing parsing and generation capabilities with human-friendly features.

Pending
Overview
Eval results
Files

json-objects.mddocs/

JSON Object Operations

Create, modify, and query JSON objects with type-safe accessors, fluent interface support, and comprehensive member management capabilities.

Capabilities

Object Creation

Create JSON objects with constructors and static factory methods.

/**
 * Creates an empty JSON object
 */
JsonObject();

/**
 * Creates a JSON object as a copy of another object
 * @param object the JSON object to copy
 */
JsonObject(JsonObject object);

/**
 * Creates an unmodifiable view of the given JSON object
 * @param object the JSON object to wrap
 * @return unmodifiable JSON object view
 */
static JsonObject unmodifiableObject(JsonObject object);

Usage Examples:

import org.hjson.JsonObject;
import org.hjson.JsonValue;

// Create empty object
JsonObject emptyObj = new JsonObject();

// Create object with initial data
JsonObject person = new JsonObject()
    .add("name", "John Doe")
    .add("age", 30)
    .add("active", true);

// Copy constructor
JsonObject personCopy = new JsonObject(person);

// Unmodifiable view
JsonObject readOnlyPerson = JsonObject.unmodifiableObject(person);

Adding Members (Fluent Interface)

Add new members to JSON objects with type-specific convenience methods.

/**
 * Adds a new member with the specified name and JSON value
 * If a member with this name already exists, the new member is appended
 * @param name the name of the member to add
 * @param value the value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, JsonValue value);

/**
 * Adds a new member with the specified name and string value
 * @param name the name of the member to add
 * @param value the string value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, String value);

/**
 * Adds a new member with the specified name and int value
 * @param name the name of the member to add
 * @param value the int value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, int value);

/**
 * Adds a new member with the specified name and long value
 * @param name the name of the member to add
 * @param value the long value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, long value);

/**
 * Adds a new member with the specified name and float value
 * @param name the name of the member to add
 * @param value the float value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, float value);

/**
 * Adds a new member with the specified name and double value
 * @param name the name of the member to add
 * @param value the double value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, double value);

/**
 * Adds a new member with the specified name and boolean value
 * @param name the name of the member to add
 * @param value the boolean value of the member to add
 * @return the JSON object itself, to enable method chaining
 */
JsonObject add(String name, boolean value);

Usage Examples:

import org.hjson.JsonObject;
import org.hjson.JsonArray;
import org.hjson.JsonValue;

// Fluent interface for building objects
JsonObject config = new JsonObject()
    .add("appName", "My Application")
    .add("version", "1.2.0")
    .add("port", 8080)
    .add("debugMode", false)
    .add("maxConnections", 1000L)
    .add("timeout", 30.5)
    .add("nullValue", JsonValue.NULL);

// Adding complex nested structures
JsonArray servers = new JsonArray()
    .add("server1.example.com")
    .add("server2.example.com");

JsonObject database = new JsonObject()
    .add("host", "localhost")
    .add("port", 5432)
    .add("name", "myapp_db");

config.add("servers", servers)
      .add("database", database);

Setting/Replacing Members

Replace existing members or add new ones with set methods.

/**
 * Sets the value of the member with the specified name to the specified JSON value
 * If the member does not exist, it is added. If it exists, its value is replaced
 * @param name the name of the member to set
 * @param value the value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, JsonValue value);

/**
 * Sets the value of the member with the specified name to the specified string value
 * @param name the name of the member to set
 * @param value the string value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, String value);

/**
 * Sets the value of the member with the specified name to the specified int value
 * @param name the name of the member to set
 * @param value the int value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, int value);

/**
 * Sets the value of the member with the specified name to the specified long value
 * @param name the name of the member to set
 * @param value the long value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, long value);

/**
 * Sets the value of the member with the specified name to the specified float value
 * @param name the name of the member to set
 * @param value the float value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, float value);

/**
 * Sets the value of the member with the specified name to the specified double value
 * @param name the name of the member to set
 * @param value the double value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, double value);

/**
 * Sets the value of the member with the specified name to the specified boolean value
 * @param name the name of the member to set
 * @param value the boolean value to set the member to
 * @return the JSON object itself, to enable method chaining
 */
JsonObject set(String name, boolean value);

Usage Examples:

import org.hjson.JsonObject;

// Create object with initial values
JsonObject user = new JsonObject()
    .add("name", "John")
    .add("age", 25)
    .add("active", true);

// Update existing values
user.set("age", 26)           // Replace existing age
    .set("email", "john@example.com")  // Add new email field
    .set("active", false);    // Replace existing active status

// Conditional updates
if (someCondition) {
    user.set("lastLogin", System.currentTimeMillis());
}

Accessing Members

Retrieve member values with type-safe accessors and default value support.

/**
 * Returns the value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @return the value of the member with the specified name, or null if not found
 */
JsonValue get(String name);

/**
 * Returns the string value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a string
 * @return the string value of the member, or the default value
 */
String getString(String name, String defaultValue);

/**
 * Returns the int value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a number
 * @return the int value of the member, or the default value
 */
int getInt(String name, int defaultValue);

/**
 * Returns the long value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a number
 * @return the long value of the member, or the default value
 */
long getLong(String name, long defaultValue);

/**
 * Returns the float value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a number
 * @return the float value of the member, or the default value
 */
float getFloat(String name, float defaultValue);

/**
 * Returns the double value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a number
 * @return the double value of the member, or the default value
 */
double getDouble(String name, double defaultValue);

/**
 * Returns the boolean value of the member with the specified name
 * @param name the name of the member whose value is to be returned
 * @param defaultValue the value to be returned if the member is not found or not a boolean
 * @return the boolean value of the member, or the default value
 */
boolean getBoolean(String name, boolean defaultValue);

Usage Examples:

import org.hjson.JsonObject;
import org.hjson.JsonValue;
import org.hjson.JsonArray;

JsonObject config = JsonValue.readHjson("""
    {
        appName: "My App"
        port: 8080
        debugMode: true
        maxRetries: 3
        timeout: 30.5
        servers: ["server1", "server2"]
    }
    """).asObject();

// Safe access with defaults
String appName = config.getString("appName", "Unknown App");
int port = config.getInt("port", 3000);
boolean debug = config.getBoolean("debugMode", false);
double timeout = config.getDouble("timeout", 10.0);

// Access complex values
JsonValue serversValue = config.get("servers");
if (serversValue != null && serversValue.isArray()) {
    JsonArray servers = serversValue.asArray();
    System.out.println("Server count: " + servers.size());
}

// Handle missing values gracefully
String missingValue = config.getString("nonexistent", "default");
int missingNumber = config.getInt("nonexistent", -1);

Member Management

Remove members and query object structure.

/**
 * Removes the member with the specified name from this object
 * @param name the name of the member to remove
 * @return the JSON object itself, to enable method chaining
 */
JsonObject remove(String name);

/**
 * Returns the number of members in this object
 * @return the number of members
 */
int size();

/**
 * Returns true if this object contains no members
 * @return true if this object is empty
 */
boolean isEmpty();

/**
 * Returns a list of the names of all members in this object
 * The list is a copy and modifications do not affect the original object
 * @return list of member names
 */
List<String> names();

Usage Examples:

import org.hjson.JsonObject;
import java.util.List;

JsonObject obj = new JsonObject()
    .add("name", "Test")
    .add("value", 42)
    .add("temp", "temporary");

// Query object structure
System.out.println("Size: " + obj.size()); // Output: Size: 3
System.out.println("Empty: " + obj.isEmpty()); // Output: Empty: false

// Get all member names
List<String> memberNames = obj.names();
System.out.println("Members: " + memberNames); // Output: Members: [name, value, temp]

// Remove temporary data
obj.remove("temp");
System.out.println("After removal: " + obj.size()); // Output: After removal: 2

// Check if empty after clearing
JsonObject empty = new JsonObject();
System.out.println("New object empty: " + empty.isEmpty()); // Output: New object empty: true

Iteration

Iterate over object members with enhanced for-loop support.

/**
 * Returns an iterator over the members in this object
 * @return iterator over Member objects
 */
Iterator<JsonObject.Member> iterator();

/**
 * Represents a member of a JSON object (name/value pair)
 */
static class Member {
    /**
     * Returns the name of this member
     * @return the member name
     */
    String getName();
    
    /**
     * Returns the value of this member
     * @return the member value
     */
    JsonValue getValue();
}

Usage Examples:

import org.hjson.JsonObject;
import org.hjson.JsonObject.Member;

JsonObject person = new JsonObject()
    .add("firstName", "John")
    .add("lastName", "Doe")
    .add("age", 30)
    .add("active", true);

// Enhanced for-loop iteration
for (Member member : person) {
    String name = member.getName();
    JsonValue value = member.getValue();
    System.out.println(name + " = " + value);
}

// Iterator-based iteration
Iterator<Member> iterator = person.iterator();
while (iterator.hasNext()) {
    Member member = iterator.next();
    if (member.getValue().isString()) {
        System.out.println("String member: " + member.getName() + 
                          " = " + member.getValue().asString());
    }
}

// Process specific member types
for (Member member : person) {
    JsonValue value = member.getValue();
    if (value.isNumber()) {
        System.out.println("Number: " + member.getName() + " = " + value.asInt());
    } else if (value.isBoolean()) {
        System.out.println("Boolean: " + member.getName() + " = " + value.asBoolean());
    }
}

Type Checking and Conversion

JsonObject inherits type checking and conversion methods from JsonValue:

// Type checking (always returns true for JsonObject)
boolean isObject();
JsonType getType(); // Returns JsonType.OBJECT

// Safe conversion (returns this)
JsonObject asObject();

// Object equality and hashing
boolean equals(Object obj);
int hashCode();

Thread Safety and Immutability

  • Thread Safety: JsonObject is not thread-safe and requires external synchronization for concurrent access
  • Mutability: JsonObject instances are mutable - methods like add(), set(), and remove() modify the object
  • Defensive Copying: Use the copy constructor or unmodifiableObject() for safe sharing
  • Member Immutability: Individual JsonValue members are immutable once added

Best Practices

  1. Fluent Interface: Chain method calls for readable object construction
  2. Default Values: Always provide sensible defaults when accessing members
  3. Null Checking: Check if get() returns null before using the value
  4. Type Safety: Use type-specific getters (getString(), getInt(), etc.) instead of manual casting
  5. Unmodifiable Views: Use unmodifiableObject() when passing objects to untrusted code
  6. Member Names: Use consistent naming conventions for member names
  7. Error Handling: Handle potential type conversion errors gracefully

Install with Tessl CLI

npx tessl i tessl/maven-org-hjson--hjson

docs

configuration.md

index.md

json-arrays.md

json-objects.md

parsing.md

serialization.md

types-exceptions.md

tile.json