added json module
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m33s

This commit is contained in:
Jorge Bornhausen 2024-10-18 23:47:10 +02:00
parent 7d01a51fc8
commit 054aa67d34
6 changed files with 166 additions and 0 deletions

View file

@ -18,6 +18,8 @@ that can be used by Quarkus applications.
The modules are:
* `quarkus-clock-service`
* `quarkus-json-service`
* `quarkus-message-digest-service`
* `quarkus-random-number-generator`
* `quarkus-uuid-generator`

View file

@ -10,6 +10,7 @@
<modules>
<module>quarkus-clock-service</module>
<module>quarkus-json-service</module>
<module>quarkus-message-digest-service</module>
<module>quarkus-random-number-generator</module>
<module>quarkus-uuid-generator</module>

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ch.phoenixtechnologies.quarkus</groupId>
<artifactId>quarkus-commons</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>quarkus-json-service</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,13 @@
package ch.phoenixtechnologies.quarkus.commons.json;
import com.fasterxml.jackson.core.type.TypeReference;
public interface JsonService {
String toJson(Object object);
<T> T fromJson(String json, Class<T> clazz);
<T> T fromJson(String json, TypeReference<T> typeReference);
}

View file

@ -0,0 +1,48 @@
package ch.phoenixtechnologies.quarkus.commons.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.arc.DefaultBean;
import jakarta.enterprise.context.ApplicationScoped;
@DefaultBean
@ApplicationScoped
class JsonServiceImpl implements JsonService {
private final ObjectMapper objectMapper;
JsonServiceImpl(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public String toJson(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to write object as json String: " + object, e);
}
}
@Override
public <T> T fromJson(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to read object of class [" + clazz.getName()
+ "] from json String: " + json, e);
}
}
@Override
public <T> T fromJson(String json, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(json, typeReference);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Unable to read object of type [" + typeReference.getType().getTypeName()
+ "] from json String: " + json, e);
}
}
}

View file

@ -0,0 +1,80 @@
package ch.phoenixtechnologies.quarkus.commons.json;
import com.fasterxml.jackson.core.type.TypeReference;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@QuarkusTest
class JsonServiceImplTest {
public static final TypeReference<List<TestRecord>> TYPE_REFERENCE = new TypeReference<>() {
};
record TestRecord(String name, int age) {
}
@Inject
JsonServiceImpl jsonService;
@Test
void toJson() {
var expected = "{\"name\":\"John Doe\",\"age\":30}";
var actual = jsonService.toJson(new TestRecord("John Doe", 30));
assertThat(actual)
.as("Json should match expected value")
.isEqualTo(expected);
}
@Test
void fromJsonWithClass() {
var json = "{\"name\":\"John Doe\",\"age\":30}";
var expected = new TestRecord("John Doe", 30);
var actual = jsonService.fromJson(json, TestRecord.class);
assertThat(actual)
.as("Deserialized object should match expected value")
.isEqualTo(expected);
}
@Test
void fromJsonWithClassWhenInputIsInvalid() {
var json = "{\"name\":\"John Doe\",\"age\":\"30\"";
assertThatThrownBy(() -> jsonService.fromJson(json, TestRecord.class))
.as("Should throw IllegalArgumentException when input is invalid")
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unable to read object of class [ch.phoenixtechnologies.quarkus.commons.json.JsonServiceImplTest$TestRecord] from json String: {\"name\":\"John Doe\",\"age\":\"30\"");
}
@Test
void fromJsonWithTypeReference() {
var json = "[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":25}]";
var expected = List.of(new TestRecord("John Doe", 30), new TestRecord("Jane Doe", 25));
var actual = jsonService.fromJson(json, TYPE_REFERENCE);
Assertions.assertThat(actual)
.as("Deserialized object should match expected value")
.isEqualTo(expected);
}
@Test
void fromJsonWithTypeReferenceWhenInputIsInvalid() {
var json = "{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":\"25\"}";
assertThatThrownBy(() -> jsonService.fromJson(json, TYPE_REFERENCE))
.as("Should throw IllegalArgumentException when input is invalid")
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Unable to read object of type [java.util.List<ch.phoenixtechnologies.quarkus.commons.json.JsonServiceImplTest$TestRecord>] from json String: {\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":\"25\"}");
}
}