Created
November 28, 2022 15:32
-
-
Save chrishowell/f8ed0d2315b20dc5025b22db0d27f03a to your computer and use it in GitHub Desktop.
Getting Micronaut Serde working with Redisson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.micronaut.serde.ObjectMapper; | |
import jakarta.inject.Singleton; | |
import lombok.RequiredArgsConstructor; | |
import org.redisson.client.protocol.Decoder; | |
import org.redisson.client.protocol.Encoder; | |
import org.redisson.codec.JsonCodec; | |
import static io.netty.buffer.Unpooled.wrappedBuffer; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
@Singleton | |
@RequiredArgsConstructor | |
public class SerdeCodec<T> implements JsonCodec<T> { | |
private final ObjectMapper objectMapper; | |
private final Class<T> type; | |
// Required for Redisson framework | |
public SerdeCodec(ClassLoader classLoader, SerdeCodec<T> codec) { | |
this(codec.objectMapper, codec.type); | |
} | |
@Override | |
public Encoder getValueEncoder() { | |
return in -> wrappedBuffer(objectMapper.writeValueAsString(in).getBytes(UTF_8)); | |
} | |
@Override | |
public Decoder<Object> getValueDecoder() { | |
return (buf, state) -> objectMapper.readValue(buf.toString(UTF_8), type); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment