Newer
Older
json-utils / README.md

json-utils

A lightweight Java utility library that provides static helper methods for common JSON operations. Built on top of Jackson, it covers serialization, deserialization, validation, equality checks, and format conversion (CSV, YAML, TOML, XML) — all through a single JsonUtils class with no setup required.

Requirements

  • Java 8 or higher
  • Maven (for building or consuming as a dependency)

Installation

Add the following repository and dependency to your pom.xml:

<repositories>
    <repository>
        <id>nexus</id>
        <url>https://nexus.ljunggren.io/repository/maven-releases/</url>
    </repository>
</repositories>

<dependency>
    <groupId>io.ljunggren</groupId>
    <artifactId>json-utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Usage

All methods are static. Import the class and call methods directly — no instantiation needed.

import io.ljunggren.json.utils.JsonUtils;

Serialization

Object to JSON

String json = JsonUtils.objectToJson(myObject);

Object to pretty-printed JSON

Accepts either a Java object or an existing JSON string.

String pretty = JsonUtils.prettyPrint(myObject);
String pretty = JsonUtils.prettyPrint(jsonString);

Custom ObjectMapper

objectToJson uses a shared default ObjectMapper. If you need custom configuration (e.g., date formats, custom serializers), pass your own mapper using the overload available internally — or pre-serialize with your mapper and pass the result as a string.


Deserialization

JSON to object

MyClass obj = JsonUtils.jsonToObject(json, MyClass.class);

With a custom ObjectMapper:

MyClass obj = JsonUtils.jsonToObject(json, MyClass.class, mapper);

JSON to parameterized (generic) object

MyClass<User> obj = JsonUtils.jsonToParameterizedObject(json, MyClass.class, User.class);

With a custom ObjectMapper:

MyClass<User> obj = JsonUtils.jsonToParameterizedObject(json, mapper, MyClass.class, User.class);

JSON to List

List<MyClass> list = JsonUtils.jsonToList(json, MyClass.class);

With a custom ObjectMapper:

List<MyClass> list = JsonUtils.jsonToList(json, MyClass.class, mapper);

JSON to Set

Set<MyClass> set = JsonUtils.jsonToSet(json, MyClass.class);

JSON to Array

MyClass[] array = JsonUtils.jsonToArray(json, MyClass.class);

JSON to Map

Map<String, MyClass> map = JsonUtils.jsonToHashMap(json, String.class, MyClass.class);

Most deserialization methods throw JsonProcessingException if the input is not valid JSON or cannot be mapped to the target type.


Validation and Equality

Check whether a string is valid JSON

Returns true if the input parses successfully, false otherwise. Does not throw.

boolean valid = JsonUtils.isValid(json);

Check whether two JSON strings are semantically equal

Compares the parsed tree structures, so whitespace and key ordering differences are ignored. Returns false if either input is invalid JSON.

boolean equal = JsonUtils.areEqual(json1, json2);

Format Conversion

JSON to CSV

The input must be a non-empty JSON array of objects. Column headers are derived from the field names of the first element. The default delimiter is a comma.

String csv = JsonUtils.toCSV(json);

With a custom delimiter:

String csv = JsonUtils.toCSV(json, '|');

Throws IllegalArgumentException if the input is not a non-empty JSON array. Throws IOException if the input cannot be parsed.

JSON to YAML

String yaml = JsonUtils.toYAML(json);

JSON to TOML

String toml = JsonUtils.toTOML(json);

JSON to XML

String xml = JsonUtils.toXML(json);

The conversion methods above throw JsonProcessingException if the input is not valid JSON.


Building from Source

git clone https://gitbucket.ljunggren.io/git/aljunggren/json-utils.git
cd json-utils
mvn clean install

Running Tests

mvn test

License

This project does not currently specify a license. Contact the maintainer for usage terms.