fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

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

Open xiebin1998 opened this issue 8 months ago • 0 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;
    }

期待的正确结果

**能够正常取出

相关日志输出

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

附加信息

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

2、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 用的是这个序列化实现:com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer

xiebin1998 avatar May 13 '25 06:05 xiebin1998