fastjson 反序列化多态对象丢字段

tpgth1q7  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(366)

json字符串:

{
                "id":"start-b6159e58-202d-11ec-9c49-d776ef603a15",
                "@type":"startEvent",
                "type":"startEvent",
                "name":"发起",
                "startEvent":{
                    "id":"start-b6159e58-202d-11ec-9c49-d776ef603a15"
                }
        }

对象

@Data
    @JSONType(seeAlso = {EditTaskNode.class, StartEventNode.class,
            EndEventNode.class, InclusiveGatewayNode.class, ConditionTaskNode.class, ApproveTaskNode.class,
            MergeInclusiveGatewayNode.class},serialzeFeatures = {SerializerFeature.WriteClassName})
    public static abstract class Node {
        private String id;

        private String type;

        private String name;

        private Node childNode;

        private List<Node> branches;

    }

    @Data
    @JSONType(typeName = "startEvent", serialzeFeatures = {SerializerFeature.WriteClassName})
    public static class StartEventNode extends Node {
        private StartEventVO startEvent;

        public StartEventNode() {
        }
    }

反序列化代码

String json1 = "{\n" +
                "                \"id\":\"start-b6159e58-202d-11ec-9c49-d776ef603a15\",\n" +
                "                \"@type\":\"startEvent\",\n" +
                "                \"type\":\"startEvent\",\n" +
                "                \"name\":\"发起\",\n" +
                "                \"startEvent\":{\n" +
                "                    \"id\":\"start-b6159e58-202d-11ec-9c49-d776ef603a15\"\n" +
                "                }\n" +
                "        }";
        System.out.println(json1);

        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        {
            StartEventNode node = (StartEventNode) JSON.parseObject(json1, Node.class);
            System.out.println(JSON.toJSONString(node, SerializerFeature.PrettyFormat));

        }

调试查看对象

打印的字符串

{
	"@type":"startEvent",
	"name":"发起",
	"startEvent":{
		"id":"start-b6159e58-202d-11ec-9c49-d776ef603a15"
	},
	"type":"startEvent"
}

从上面可以看出id丢失了 但是如果建立一个普通对象 id就没有问题

使用的版本是1.2.78 1.2.75都有问题

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题