# json-utils

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.

## 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
<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.

```java
import io.ljunggren.json.utils.JsonUtils;
```

---

### Serialization

**Object to JSON**

```java
String json = JsonUtils.objectToJson(myObject);
```

**Object to pretty-printed JSON**

Accepts either a Java object or an existing JSON string.

```java
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**

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

With a custom `ObjectMapper`:

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

**JSON to parameterized (generic) object**

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

With a custom `ObjectMapper`:

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

**JSON to List**

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

With a custom `ObjectMapper`:

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

**JSON to Set**

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

**JSON to Array**

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

**JSON to Map**

```java
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.

```java
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.

```java
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.

```java
String csv = JsonUtils.toCSV(json);
```

With a custom delimiter:

```java
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**

```java
String yaml = JsonUtils.toYAML(json);
```

**JSON to TOML**

```java
String toml = JsonUtils.toTOML(json);
```

**JSON to XML**

```java
String xml = JsonUtils.toXML(json);
```

> 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.
