Skip to content

Instantly share code, notes, and snippets.

@chrishowell
Created November 28, 2022 15:32
Show Gist options
  • Save chrishowell/f8ed0d2315b20dc5025b22db0d27f03a to your computer and use it in GitHub Desktop.
Save chrishowell/f8ed0d2315b20dc5025b22db0d27f03a to your computer and use it in GitHub Desktop.
Getting Micronaut Serde working with Redisson
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