Skip to content

Commit

Permalink
Use the Quarkus configured ObjectMapper in Redis client
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Dec 5, 2024
1 parent fa7bb99 commit 4629e85
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package io.quarkus.redis.deployment.client.datasource;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.UUID;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import io.quarkus.jackson.ObjectMapperCustomizer;
import io.quarkus.redis.datasource.RedisDataSource;
import io.quarkus.redis.datasource.hash.HashCommands;
import io.quarkus.redis.deployment.client.RedisTestResource;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.QuarkusTestResource;

@QuarkusTestResource(RedisTestResource.class)
public class QuarkusObjectMapperTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(CustomCodecTest.Jedi.class).addClass(
CustomCodecTest.Sith.class)
.addClass(CustomCodecTest.CustomJediCodec.class).addClass(CustomCodecTest.CustomSithCodec.class))
.overrideConfigKey("quarkus.redis.hosts", "${quarkus.redis.tr}");

@Inject
RedisDataSource ds;

@Test
public void test() {
String key = UUID.randomUUID().toString();
HashCommands<String, String, Person> hash = ds.hash(Person.class);
hash.hset(key, "test", new Person("foo", 100));
String stringRetrieved = ds.hash(String.class).hget(key, "test");
assertThat(stringRetrieved).isEqualTo("{\"nAmE\":\"foo\",\"aGe\":100}");
Person personRetrieved = hash.hget(key, "test");
assertThat(personRetrieved.getName()).isEqualTo("foo");
assertThat(personRetrieved.getAge()).isEqualTo(100);
}

// without a custom module, this could not be deserialized as there are 2 constructors
public static class Person {
private final String name;
private final int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

@SuppressWarnings("unused")
public Person(String name) {
this.name = name;
this.age = 0;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}

@Singleton
public static class PersonCustomizer implements ObjectMapperCustomizer {

@Override
public void customize(ObjectMapper objectMapper) {
SimpleModule module = new SimpleModule();
module.addDeserializer(Person.class, new PersonDeserializer());
module.addSerializer(Person.class, new PersonSerializer());
objectMapper.registerModule(module);
}
}

public static class PersonSerializer extends StdSerializer<Person> {

protected PersonSerializer() {
super(Person.class);
}

@Override
public void serialize(Person person, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("nAmE", person.getName());
jsonGenerator.writeNumberField("aGe", person.getAge());
}
}

public static class PersonDeserializer extends StdDeserializer<Person> {

protected PersonDeserializer() {
super(Person.class);
}

@Override
public Person deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String name = node.get("nAmE").asText();
int age = (Integer) node.get("aGe").numberValue();

return new Person(name, age);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.vertx.runtime.jackson.QuarkusJacksonJsonCodec;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.core.json.jackson.DatabindCodec;

public class Codecs {

Expand Down Expand Up @@ -61,7 +61,7 @@ public Type getType() {
};
this.clazz = null;
}
this.mapper = DatabindCodec.mapper();
this.mapper = QuarkusJacksonJsonCodec.mapper();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* The difference is that this class obtains the ObjectMapper from Arc in order to inherit the
* user-customized ObjectMapper.
*/
class QuarkusJacksonJsonCodec implements JsonCodec {
public class QuarkusJacksonJsonCodec implements JsonCodec {

private static volatile ObjectMapper mapper;
// we don't want to create this unless it's absolutely necessary (and it rarely is)
Expand All @@ -43,7 +43,7 @@ public static void reset() {
prettyMapper = null;
}

private static ObjectMapper mapper() {
public static ObjectMapper mapper() {
if (mapper == null) {
synchronized (QuarkusJacksonJsonCodec.class) {
if (mapper == null) {
Expand Down

0 comments on commit 4629e85

Please sign in to comment.