通过现场进行杰克逊多态反序列化

编程入门 行业动态 更新时间:2024-10-26 13:29:01
本文介绍了通过现场进行杰克逊多态反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

让我说一堂课

公共类A {私有UUID typeId;私人B资料;}公共抽象类B {私有字符串a;}公共课程BChildOne扩展了B {...一些变量}公共类BChildTwo扩展了B {...一些变量}

类B的类型正在更改,根据A的typeId,因此,如果A的typeId为"XXX",则数据字段的类型为BChildOne,而如果A的typeId为"YYY",则数据字段的类型为BChildTwo./p>

我该如何实现?

所以我尝试过;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY,getterVisibility =JsonAutoDetect.Visibility.NONE,setterVisibility =JsonAutoDetect.Visibility.NONE)@JsonIgnoreProperties(ignoreUnknown = true)@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,包括=JsonTypeInfo.As.EXTERNAL_PROPERTY,属性="typeId")@JsonSubTypes({@ JsonSubTypes.Type(值= BChildOne.class,名称="40ad2fe6-e672-4f0e-986e-619c7a1a3223)})公共抽象类B {

但是我遇到以下错误;

意外的令牌(END_OBJECT),预期的FIELD_NAME:缺少包含类型ID(用于B类)的属性"typeId"

这很明显,因为typeId字段在类A中而不在类B中.

解决方案

假设您的JSON文档类似:​​

{"type":"foo",数据": {"someCommonProperty":公共财产","fooProperty":特定于foo的属性"}}

{"type":"bar",数据": {"someCommonProperty":公共财产","barProperty":栏特定属性"}}

您可以使用:

公共类包装器{私有字符串类型;@JsonTypeInfo(使用= Id.NAME,属性=类型",包含= As.EXTERNAL_PROPERTY)@JsonSubTypes(value = {@ JsonSubTypes.Type(值= Foo.class,名称="foo"),@ JsonSubTypes.Type(值= Bar.class,名称="bar")})私有AbstractData数据;//取得者和设定者}

公共抽象类AbstractData {私有字符串someCommonProperty;//取得者和设定者}

公共类Foo扩展了AbstractData {私有字符串fooProperty;//取得者和设定者}

公共类Bar扩展AbstractData {private String barProperty;//取得者和设定者}

采用这种方法, @JsonTypeInfo 设置为使用 type 作为外部属性,以确定用于映射 data 属性的正确类.JSON文档可以反序列化如下:

ObjectMapper mapper = new ObjectMapper();包装器包装器= mapper.readValue(json,Wrapper.class);

let's say, i have a class

public class A{ private UUID typeId; private B data; } public abstract class B{ private String a; } public class BChildOne extends B{ ... some variables } public class BChildTwo extends B{ ... some variables }

type of class B is changing, according to A's typeId , so if typeId of A is "XXX", type of data field is BChildOne, and if typeId of A is "YYY", type of data field is BChildTwo.

how can i achive that?

so for i tried that;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE) @JsonIgnoreProperties(ignoreUnknown = true) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY , property = "typeId") @JsonSubTypes({ @JsonSubTypes.Type(value = BChildOne.class, name = "40ad2fe6-e672-4f0e- 986e- 619c7a1a3223") } ) public abstract class B{

but i got following error;

Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'typeId' that is to contain type id (for class B)

which is obvious, because typeId field is in class A not B.

解决方案

Assuming that your JSON documents are like:

{ "type": "foo", "data": { "someCommonProperty": "common property", "fooProperty": "foo specific property" } }

{ "type": "bar", "data": { "someCommonProperty": "common property", "barProperty": "bar specific property" } }

You can use:

public class Wrapper { private String type; @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY) @JsonSubTypes(value = { @JsonSubTypes.Type(value = Foo.class, name = "foo"), @JsonSubTypes.Type(value = Bar.class, name = "bar") }) private AbstractData data; // Getters and setters }

public abstract class AbstractData { private String someCommonProperty; // Getters and setters }

public class Foo extends AbstractData { private String fooProperty; // Getters and setters }

public class Bar extends AbstractData { private String barProperty; // Getters and setters }

In this approach, @JsonTypeInfo is set to use type as an external property to determine the right class to map the data property. The JSON document can be deserialized as following:

ObjectMapper mapper = new ObjectMapper(); Wrapper wrapper = mapper.readValue(json, Wrapper.class);

更多推荐

通过现场进行杰克逊多态反序列化

本文发布于:2023-08-04 05:55:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:杰克逊   现场   序列化   多态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!