fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] 在版本2.0.50中,Object[]数组对象序列化后存入redis缓存,不能取出

Open xiebin1998 opened this issue 1 year ago • 5 comments

问题描述

我们配置了redis的hash存储的GenericFastJsonRedisSerializer序列化,我们在存入缓存的时候可以通过这个序列化存入,但是取出来的时候只能拿到一个@value

环境信息

本地调试

  • OS信息: windows11 cpu=4 men=16GB
  • JDK信息:1.8
  • 版本信息:Fastjson2 2.0.50

重现步骤

存入的内容格式,如下:

{
  "@type": "[com.xxx.ProtocolParamEntity",
  "@value": [
    {
      "id": "1",
      "bit": "0"
    },
    {
      "id": "2",
      "bit": "1"
    },
    null,
    null
  ]
}

数据存入到redis之后,通过hash取出来,到obj对象,但是这个对象的值是@value,根本无法转换成ProtocolParamEntity[]

private ProtocolParamEntity[] getHash(String protocolId, String address) {
        String hash = PhnixConstants.CACHE_PROTOCAL_PARAM_PREFIX + protocolId;
        Object obj = redisDataBase.db0().getOpsForHashValue(hash, address);
        if (null == obj) {
            return new ProtocolParamEntity[0];
        }
        return (ProtocolParamEntity[])obj;
    }

期待的正确结果

**能够正常取出

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

xiebin1998 avatar Sep 03 '24 11:09 xiebin1998

我们引入的是兼容fastjson1的依赖:Fastjson1 Compatible » 2.0.50

xiebin1998 avatar Sep 03 '24 11:09 xiebin1998

我们引入的是兼容fastjson1的依赖:Fastjson1 Compatible » 2.0.50

我们配置了redis的hash存储的GenericFastJsonRedisSerializer序列化

可否发一下配置RedisSerializer这的代码?

MoshiCoCo avatar Sep 05 '24 02:09 MoshiCoCo

取出报什么错误了?

wenshao avatar Sep 15 '24 09:09 wenshao

@MoshiCoCo,@wenshao redis客户端配置代码

RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericFastJsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        return template;

我存入的时候正常,redis的格式是:

{
  "@type": "[com.xxx.ProtocolParamEntity",
  "@value": [
    {
      "id": "1",
      "bit": "0"
    },
    {
      "id": "2",
      "bit": "1"
    },
    null,
    null
  ]
}

取出来不正常,也不报错,就是只取出了个@value

xiebin1998 avatar Sep 23 '24 00:09 xiebin1998

用的是这个序列化实现:com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer

xiebin1998 avatar Sep 23 '24 00:09 xiebin1998

@MoshiCoCo 有注意到么,,能确定是什么问题么

xiebin1998 avatar Oct 28 '24 07:10 xiebin1998

可参考第五节fastjson2_autotype_cn 如果是 redisson 的话

参考代码

public  Object  decode(ByteBuf buf, State  state){
return JSON.perseObject(new ByteBufInputStream(buf).readAllBytes(),Object.class,JSONReader.autoTypeFilter("com.xxx","com.xxx.classname
"));
}

jilliss avatar Jul 11 '25 08:07 jilliss