All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rxyor.spring.boot.redisson.FastJsonCodec Maven / Gradle / Ivy

There is a newer version: 1.0.14.17
Show newest version
package com.github.rxyor.spring.boot.redisson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.rxyor.common.util.string.CharSequenceUtil;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;

/**
 *

* *

* * @author liuyang * @date 2019/12/31 周二 00:13:00 * @since 1.0.0 */ public class FastJsonCodec extends BaseCodec { public static final FastJsonCodec INSTANCE = new FastJsonCodec(); private final Encoder encoder = in -> { String json = JSON.toJSONString(in, SerializerFeature.WriteClassName, SerializerFeature.DisableCircularReferenceDetect); CharBuffer charBuffer = CharBuffer.wrap(CharSequenceUtil.string2CharArray(json)); return ByteBufUtil.encodeString(ByteBufAllocator.DEFAULT, charBuffer, Charset.forName("utf-8")); }; private final Decoder decoder = (buf, state) -> { byte[] dst = new byte[buf.capacity()]; buf.readBytes(dst); String json = bytes2String(dst); return JSON.parse(json); }; @Override public Encoder getValueEncoder() { return encoder; } @Override public Decoder getValueDecoder() { return decoder; } private String bytes2String(byte[] bytes) { return (bytes == null || bytes.length == 0) ? null : new String(bytes, Charset.forName("utf-8")); } }