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.
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>
All methods are static. Import the class and call methods directly — no instantiation needed.
import io.ljunggren.json.utils.JsonUtils;
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.
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
JsonProcessingExceptionif the input is not valid JSON or cannot be mapped to the target type.
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);
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
IllegalArgumentExceptionif the input is not a non-empty JSON array. ThrowsIOExceptionif 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
JsonProcessingExceptionif the input is not valid JSON.
git clone https://gitbucket.ljunggren.io/git/aljunggren/json-utils.git cd json-utils mvn clean install
mvn test
This project does not currently specify a license. Contact the maintainer for usage terms.