代码改变世界

fastjson在反序列化时,解析对象中的继承,抽象类处理

2018-01-06 19:53  Loull  阅读(8726)  评论(1编辑  收藏  举报
LimitActionConfig是ActionConfig的子类,RuleConfig的有个属性是ActionConfig,需要反序列化成LimitActionConfig
        ParserConfig parserConfig = new ParserConfig() {
            @Override
            public ObjectDeserializer getDeserializer(Type type) {
                if (type == ActionConfig.class) {
                    return super.getDeserializer(LimitActionConfig.class);
                }
                return super.getDeserializer(type);
            }
        };

        RuleConfig ruleConfig = new RuleConfig();
        ruleConfig.setDesc("xxx");
        LimitActionConfig limitActionConfig = new LimitActionConfig();
        limitActionConfig.setResponseContent("ddd");
        ruleConfig.setActionConfig(limitActionConfig);

        String text = JSON.toJSONString(ruleConfig);
        System.out.println(text);

        RuleConfig ruleConfig2 = JSON.parseObject(text, RuleConfig.class, parserConfig);
        System.out.println(ruleConfig2);

 

参考:https://www.cnblogs.com/whitewolf/p/4355541.html