[alibaba/fastjson]1.2.62 反序列化org.springframework.data.geo.Point报错

2024-10-16 733 views
9

image


添加 ParserConfig.getGlobalInstance().setAutoTypeSupport(true); 或者 ParserConfig.getGlobalInstance().addAccept("org.springframework.data.geo.Point"); 均无效

回答

3

把你的json发出来

1

code:

public static void main(String[] args) {
        ParserConfig.getGlobalInstance().addAccept("org.springframework.data.geo.Point");
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteClassName);

        JSON.parseObject(
                "{\"@type\":\"org.springframework.data.geo.Point\",\"x\":1.2,\"y\":0.3}".getBytes(StandardCharsets.UTF_8),
                StandardCharsets.UTF_8,
                Object.class,
                fastJsonConfig.getParserConfig(),
                fastJsonConfig.getParseProcess(),
                JSON.DEFAULT_PARSER_FEATURE,
                fastJsonConfig.getFeatures()
        );
    }

json:

{"@type":"org.springframework.data.geo.Point","x":1.2,"y":0.3}
3

发现这样也会报同样的错(FastJsonRedisSerializer里copy的)

public static void main(String[] args) {
        ParserConfig.getGlobalInstance().addAccept("org.springframework.data.geo.Point");
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteClassName);

        byte[] bytes = JSON.toJSONBytes(
                fastJsonConfig.getCharset(),
                new Point(1.2, 0.3),
                fastJsonConfig.getSerializeConfig(),
                fastJsonConfig.getSerializeFilters(),
                fastJsonConfig.getDateFormat(),
                JSON.DEFAULT_GENERATE_FEATURE,
                fastJsonConfig.getSerializerFeatures()
        );

        JSON.parseObject(
                bytes,
                fastJsonConfig.getCharset(),
                Object.class,
                fastJsonConfig.getParserConfig(),
                fastJsonConfig.getParseProcess(),
                JSON.DEFAULT_PARSER_FEATURE,
                fastJsonConfig.getFeatures()
        );
    }