fastjson在反序列化時,解析對象中的繼承,抽象類處理

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.htmlhtml

相關文章
相關標籤/搜索