fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG]Spring RedisTemplate使用GenericFastJsonRedisSerializer后,对于put后的值,不能increment

Open the3rd opened this issue 2 years ago • 5 comments

问题描述

把GenericFastJsonRedisSerializer配置到RedisTemplate之后,如果put一个Long类型的值作为初始值,比如: 1L,随后执行increment,会报:ERR value is not an integer or out of range

期待的正确结果

使用GenericFastJsonRedisSerializer后,可以用Long去初始化redis的value,不会影响后继的increment操作。

附加信息

出错后,查看redis发现其值为字符串“1L”,而不是“1”,redis无法对“1L”进行处理。和jackson对比:

    public static void main(String[] args) throws JsonProcessingException {
        Long n = 1L;
        // fastjson2
        System.out.println(JSON.toJSONString(n, JSONWriter.Feature.WriteClassName));
        // jackson
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        System.out.println(objectMapper.writeValueAsString(n));
    }
    ===========
    // output:
    1L
    1

the3rd avatar Dec 02 '23 04:12 the3rd

fastjson什么版本?

wenshao avatar Dec 03 '23 04:12 wenshao

用上面的代码测试,2.0.0~2.0.6对Long序列化后是“1”,自2.0.7开始,一直到2.0.42,序列化后都是“1L”。

the3rd avatar Dec 03 '23 12:12 the3rd

用上面的代码测试,2.0.0~2.0.6对Long序列化后是“1”,自2.0.7开始,一直到2.0.42,序列化后都是“1L”。

JSONWriter.Feature.NotWriteNumberClassName可以试一下

oooopl avatar Dec 04 '23 03:12 oooopl

或者使用FastJsonRedisSerializer自定义

oooopl avatar Dec 04 '23 03:12 oooopl

用上面的代码测试,2.0.0~2.0.6对Long序列化后是“1”,自2.0.7开始,一直到2.0.42,序列化后都是“1L”。

JSONWriter.Feature.NotWriteNumberClassName可以试一下

这个方式好使。 需要换用FastJsonRedisSerializer,并设置一下config。

the3rd avatar Dec 04 '23 12:12 the3rd