Redission 支持GsonCodec

GsonCodec.class

class GsonCodec extends BaseCodec {
  private static final Gson gson = new Gson();

  private final Encoder encoder = new Encoder() {
    public ByteBuf encode(Object in) throws IOException {
      try {
        return Unpooled.wrappedBuffer(gson.toJson(in).getBytes("UTF-8"));
      } catch (Exception e){
        throw new RuntimeException(e);
      }
    }
  };
  private final Decoder<Object> decoder = new Decoder<Object>() {
    public Object decode(ByteBuf buf, State state) throws IOException {
      return gson.fromJson(new JsonReader(new InputStreamReader(new ByteBufInputStream(buf),"UTF-8")), new TypeToken<List<Object>>() {}.getType());
    }
  };

  public GsonCodec() {

  }

  public Decoder<Object> getValueDecoder() {
    return decoder;
  }

  public Encoder getValueEncoder() {
    return encoder;
  }
}

简单使用

RBucket<Object> ok = redisson.getBucket("ok",new GsonCodec());
posted @ 2021-10-28 16:44  热咖啡与白猫  阅读(128)  评论(0编辑  收藏  举报