diff --git a/README.md b/README.md index 774f96a..1eec57b 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,199 @@ -## JSON Utils ## +# json-utils -A collection of useful JSON utilities. +A lightweight Java utility library that provides static helper methods for common JSON operations. Built on top of [Jackson](https://github.com/FasterXML/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. -Object to JSON +## Requirements + +- Java 8 or higher +- Maven (for building or consuming as a dependency) + +## Installation + +Add the following repository and dependency to your `pom.xml`: + +```xml + + + nexus + https://nexus.ljunggren.io/repository/maven-releases/ + + + + + io.ljunggren + json-utils + 0.0.1-SNAPSHOT + +``` + +## Usage + +All methods are static. Import the class and call methods directly — no instantiation needed. + +```java +import io.ljunggren.json.utils.JsonUtils; +``` + +--- + +### Serialization + +**Object to JSON** ```java String json = JsonUtils.objectToJson(myObject); ``` -Object to pretty printed JSON +**Object to pretty-printed JSON** + +Accepts either a Java object or an existing JSON string. ```java -String prettyPrinted = JsonUtils.prettyPrint(myObject); +String pretty = JsonUtils.prettyPrint(myObject); +String pretty = JsonUtils.prettyPrint(jsonString); ``` -JSON to Object +**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** ```java -MyClass myObject = JsonUtils.jsonToObject(json, MyClass.class); +MyClass obj = JsonUtils.jsonToObject(json, MyClass.class); ``` -JSON to Parameterized Object +With a custom `ObjectMapper`: ```java -MyClass myObject = JsonUtils.jsonToParameterizedObject(json, MyClass.class, User.class); +MyClass obj = JsonUtils.jsonToObject(json, MyClass.class, mapper); ``` -JSON to List +**JSON to parameterized (generic) object** ```java -List myObjects = JsonUtils.jsonToList(json, MyClass.class); +MyClass obj = JsonUtils.jsonToParameterizedObject(json, MyClass.class, User.class); ``` -JSON to Set +With a custom `ObjectMapper`: ```java -Set myObjects = JsonUtils.jsonToSet(json, MyClass.class); +MyClass obj = JsonUtils.jsonToParameterizedObject(json, mapper, MyClass.class, User.class); ``` -JSON to Array +**JSON to List** ```java -MyClass[] myObjects = JsonUtils.jsonToArray(json, MyClass.class); +List list = JsonUtils.jsonToList(json, MyClass.class); ``` -JSON to HashMap +With a custom `ObjectMapper`: + +```java +List list = JsonUtils.jsonToList(json, MyClass.class, mapper); +``` + +**JSON to Set** + +```java +Set set = JsonUtils.jsonToSet(json, MyClass.class); +``` + +**JSON to Array** + +```java +MyClass[] array = JsonUtils.jsonToArray(json, MyClass.class); +``` + +**JSON to Map** ```java Map map = JsonUtils.jsonToHashMap(json, String.class, MyClass.class); ``` -Check if String is a valid JSON +> 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. ```java -JsonUtils.isValid(json); +boolean valid = JsonUtils.isValid(json); ``` -Determine if two JSON Strings are equal +**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. ```java -JsonUtils.areEqual(json1, json2); +boolean equal = JsonUtils.areEqual(json1, json2); ``` -**Note:** Ignores carriage returns, line feeds, tabs, etc. -Convert to CSV +--- + +### 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. ```java -String csv = JsonUtils.toCSV(json); +String csv = JsonUtils.toCSV(json); ``` -**Note:** JSON must be an array of objects. - -Convert to CSV with defined delimiter +With a custom delimiter: ```java -String csv = JsonUtils.toCSV(json, '|'); +String csv = JsonUtils.toCSV(json, '|'); ``` -**Note:** JSON must be an array of objects and delimiter must be a character. +> Throws `IllegalArgumentException` if the input is not a non-empty JSON array. +> Throws `IOException` if the input cannot be parsed. -JSON to YAML +**JSON to YAML** ```java String yaml = JsonUtils.toYAML(json); ``` -JSON to TOML +**JSON to TOML** ```java String toml = JsonUtils.toTOML(json); ``` -JSON to XML +**JSON to XML** ```java String xml = JsonUtils.toXML(json); -``` \ No newline at end of file +``` + +> The conversion methods above throw `JsonProcessingException` if the input is not valid JSON. + +--- + +## Building from Source + +```bash +git clone https://gitbucket.ljunggren.io/git/aljunggren/json-utils.git +cd json-utils +mvn clean install +``` + +## Running Tests + +```bash +mvn test +``` + +## License + +This project does not currently specify a license. Contact the maintainer for usage terms.