Hjson (Human JSON) configuration file format library for Java providing parsing and generation capabilities with human-friendly features.
—
Comprehensive parsing capabilities for both Hjson (Human JSON) and standard JSON formats with error handling, encoding support, and configurable options.
Parse Hjson text directly from a string with automatic format detection and error reporting.
/**
* Reads a Hjson value from the given string
* @param text the string that contains the Hjson value
* @return the JsonValue that has been read
* @throws ParseException if the input is not valid Hjson
*/
static JsonValue readHjson(String text);Usage Examples:
import org.hjson.JsonValue;
import org.hjson.JsonObject;
// Parse Hjson with comments and unquoted strings
JsonValue value = JsonValue.readHjson("""
{
// Configuration for the application
name: My Application
version: "1.0.0"
debug: true
servers: [
localhost:8080
prod.example.com
]
}
""");
JsonObject config = value.asObject();
String name = config.getString("name", "");
boolean debug = config.getBoolean("debug", false);Parse Hjson from any Reader (file, stream, etc.) with automatic buffering for optimal performance.
/**
* Reads a Hjson value from the given reader
* Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
* an additional BufferedReader does not improve reading performance
* @param reader the reader to read the Hjson value from
* @return the JsonValue that has been read
* @throws IOException if an I/O error occurs in the reader
* @throws ParseException if the input is not valid Hjson
*/
static JsonValue readHjson(Reader reader) throws IOException;Usage Examples:
import org.hjson.JsonValue;
import java.io.FileReader;
import java.io.IOException;
// Parse from file
try (FileReader reader = new FileReader("config.hjson")) {
JsonValue config = JsonValue.readHjson(reader);
// Process configuration...
} catch (IOException e) {
System.err.println("Failed to read file: " + e.getMessage());
} catch (ParseException e) {
System.err.println("Invalid Hjson at line " + e.getLine() +
", column " + e.getColumn() + ": " + e.getMessage());
}
// Parse from any Reader
StringReader stringReader = new StringReader("{ name: test, value: 42 }");
JsonValue result = JsonValue.readHjson(stringReader);Parse Hjson with custom configuration options for specialized parsing behavior.
/**
* Reads a Hjson value from the given string with custom options
* @param text the string that contains the Hjson value
* @param options the Hjson parsing options
* @return the JsonValue that has been read
* @throws ParseException if the input is not valid Hjson
*/
static JsonValue readHjson(String text, HjsonOptions options);
/**
* Reads a Hjson value from the given reader with custom options
* @param reader the reader to read the Hjson value from
* @param options the Hjson parsing options
* @return the JsonValue that has been read
* @throws IOException if an I/O error occurs in the reader
* @throws ParseException if the input is not valid Hjson
*/
static JsonValue readHjson(Reader reader, HjsonOptions options) throws IOException;Usage Examples:
import org.hjson.JsonValue;
import org.hjson.HjsonOptions;
import org.hjson.HjsonDsf;
// Configure options for specialized parsing
HjsonOptions options = new HjsonOptions();
options.setDsfProviders(new IHjsonDsfProvider[] {
HjsonDsf.math(), // Support Inf, -Inf, NaN, -0
HjsonDsf.hex(true) // Support 0x prefixed hex numbers
});
options.setParseLegacyRoot(true); // Allow root objects without braces
// Parse with math DSF support
JsonValue mathResult = JsonValue.readHjson("""
{
infinity: +Inf
notANumber: NaN
hexValue: 0xFF
}
""", options);
// Parse legacy format (object without root braces)
JsonValue legacyResult = JsonValue.readHjson("""
name: Legacy Config
port: 8080
enabled: true
""", options);Parse standard JSON text with strict validation and comprehensive error reporting.
/**
* Reads a JSON value from the given string
* @param text the string that contains the JSON value
* @return the JsonValue that has been read
* @throws ParseException if the input is not valid JSON
*/
static JsonValue readJSON(String text);Usage Examples:
import org.hjson.JsonValue;
import org.hjson.JsonArray;
// Parse standard JSON
JsonValue value = JsonValue.readJSON("""
{
"name": "Standard JSON",
"version": "1.0.0",
"features": ["parsing", "validation", "serialization"]
}
""");
JsonArray features = value.asObject().get("features").asArray();
System.out.println("Feature count: " + features.size());Parse standard JSON from any Reader with automatic buffering and encoding detection.
/**
* Reads a JSON value from the given reader
* Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
* an additional BufferedReader does not improve reading performance
* @param reader the reader to read the JSON value from
* @return the JsonValue that has been read
* @throws IOException if an I/O error occurs in the reader
* @throws ParseException if the input is not valid JSON
*/
static JsonValue readJSON(Reader reader) throws IOException;Usage Examples:
import org.hjson.JsonValue;
import java.io.FileReader;
import java.net.URL;
import java.io.InputStreamReader;
// Parse JSON from file
try (FileReader reader = new FileReader("data.json")) {
JsonValue data = JsonValue.readJSON(reader);
// Process data...
}
// Parse JSON from URL
URL url = new URL("https://api.example.com/data.json");
try (InputStreamReader reader = new InputStreamReader(url.openStream())) {
JsonValue apiData = JsonValue.readJSON(reader);
// Process API response...
}All parsing methods throw ParseException for invalid input, providing detailed error information:
class ParseException extends RuntimeException {
/**
* Gets the character offset where the error occurred
* @return the character offset (0-based)
*/
int getOffset();
/**
* Gets the line number where the error occurred
* @return the line number (1-based)
*/
int getLine();
/**
* Gets the column number where the error occurred
* @return the column number (1-based)
*/
int getColumn();
}Error Handling Examples:
try {
JsonValue invalid = JsonValue.readHjson("{ name: unclosed");
} catch (ParseException e) {
System.err.printf("Parse error at line %d, column %d (offset %d): %s%n",
e.getLine(), e.getColumn(), e.getOffset(), e.getMessage());
// Output: Parse error at line 1, column 15 (offset 14): Expected '}'
}
try {
JsonValue invalidJson = JsonValue.readJSON("{ 'single-quotes': 'not allowed' }");
} catch (ParseException e) {
System.err.println("Invalid JSON: " + e.getMessage());
// Output: Invalid JSON: Expected '"' at line 1, column 3
}Configure parsing behavior for Hjson input:
class HjsonOptions {
/**
* Creates default Hjson parsing options
*/
HjsonOptions();
/**
* Gets the array of Domain Specific Format providers
* @return array of DSF providers (may be null)
*/
IHjsonDsfProvider[] getDsfProviders();
/**
* Sets the Domain Specific Format providers for specialized value parsing
* @param providers array of DSF providers
*/
void setDsfProviders(IHjsonDsfProvider[] providers);
/**
* Gets whether to parse legacy root format (object without braces)
* @return true if legacy root parsing is enabled
*/
boolean getParseLegacyRoot();
/**
* Sets whether to parse legacy root format (object without braces)
* @param value true to enable legacy root parsing
*/
void setParseLegacyRoot(boolean value);
}Use built-in DSF providers for specialized value parsing:
class HjsonDsf {
/**
* Returns math DSF provider supporting mathematical constants
* Supports: +Inf, -Inf, Inf, +NaN, NaN, -NaN, -0
* @return math DSF provider
*/
static IHjsonDsfProvider math();
/**
* Returns hex DSF provider supporting hexadecimal numbers
* Supports: 0x prefixed hexadecimal numbers (e.g., 0xFF, 0x123)
* @param stringify whether to stringify numbers back to hex format
* @return hex DSF provider
*/
static IHjsonDsfProvider hex(boolean stringify);
}Configure the end-of-line character(s) used globally for JSON/Hjson output formatting.
/**
* Gets the current end-of-line character(s) used for output formatting
* @return the EOL string (default is system line separator)
*/
static String getEol();
/**
* Sets the end-of-line character(s) used for output formatting
* @param value the EOL string to use (e.g., "\n", "\r\n", "\r")
*/
static void setEol(String value);Usage Examples:
import org.hjson.JsonValue;
import org.hjson.JsonObject;
import org.hjson.Stringify;
// Check current EOL setting
String currentEol = JsonValue.getEol();
System.out.println("Current EOL: " + currentEol.replace("\n", "\\n").replace("\r", "\\r"));
// Set Unix-style line endings
JsonValue.setEol("\n");
// Set Windows-style line endings
JsonValue.setEol("\r\n");
// Create formatted JSON with configured EOL
JsonObject obj = new JsonObject()
.add("name", "Test")
.add("value", 42);
String formatted = obj.toString(Stringify.FORMATTED);
System.out.println("Formatted with custom EOL:");
System.out.println(formatted);
// Reset to system default
JsonValue.setEol(System.lineSeparator());Install with Tessl CLI
npx tessl i tessl/maven-org-hjson--hjson