问题描述
我的JSON结构:
{
...
类型:后,//类型可以改变
项目:[] //项目的数组,每个产品的typeof(类型)
...
}
我如何反序列化,妥善我的包裹里的POJO 项目
清单:
公共类ItemsEnvelope {
私人字符串类型; @JsonTypeInfo(
使用= JsonTypeInfo.Id.NAME,
包括= JsonTypeInfo.As.EXTERNAL_PROPERTY,
财产=类型,
可见=真)
@JsonSubTypes({
@ JsonSubTypes.Type(值=的A.class,NAME =A),
@ JsonSubTypes.Type(值= B.class,NAME =B),
@ JsonSubTypes.Type(值= C.class,NAME =C)
})
私人列表<项目>项目; 接口扩展项目Parcelable {} A类项目实现{
// getter / setter方法和Parcelable方法串/构造
} B类项目实现{
// getter / setter方法和Parcelable方法串/构造
} C类实现项目{
// getter / setter方法和Parcelable方法串/构造
} // getter / setter方法和Parcelable方法串/构造
}
要包裹类型列表应提供 CREATOR
对象,该对象的接口,很明显,不能有。
我应该用一个抽象类,而不是接口的?
好吧,既然杰克逊预计,键入每个列表元素的信息,我不想写一个自定义解串器FOT这个POJO - 我解决了它的另一种方式
首先,我做了一个界面,我所有的亚型的项目必须实现。
@JsonTypeInfo(
使用= JsonTypeInfo.Id.NAME,
包括= JsonTypeInfo.As.EXTERNAL_PROPERTY,
财产=类型,
可见=真)
@JsonSubTypes({
@ JsonSubTypes.Type(值= PostFeedItem.class,名称= BaseFeedItem.TYPE_POST)
@ JsonSubTypes.Type(值= PhotoFeedItem.class,名称= BaseFeedItem.TYPE_PHOTO)
@ JsonSubTypes.Type(值= AudioFeedItem.class,名称= BaseFeedItem.TYPE_AUDIO)
@ JsonSubTypes.Type(值= VideoFeedItem.class,名称= BaseFeedItem.TYPE_VIDEO)
@ JsonSubTypes.Type(值= FriendFeedItem.class,名称= BaseFeedItem.TYPE_FRIEND)
})
公共接口FeedItem扩展Parcelable {
// ...
@ BaseFeedItem.Type
串的getType();
// ...
}
然后我做了一个基类,这我所有的亚型项目不得不延长。
公共抽象类BaseFeedItem实现FeedItem {
公共静态最后弦乐TYPE_POST =邮报;
公共静态最后弦乐TYPE_COMMUNITY_POST =group_post;
公共静态最后弦乐TYPE_PHOTO =照片;
公共静态最后弦乐TYPE_AUDIO =音频;
公共静态最后弦乐TYPE_VIDEO =视频;
公共静态最后弦乐TYPE_FRIEND =朋友; @Retention(RetentionPolicy.SOURCE)
@StringDef({TYPE_POST,TYPE_COMMUNITY_POST,TYPE_PHOTO,TYPE_AUDIO,TYPE_VIDEO,TYPE_FRIEND})
公共@interface类型{}
私人字符串类型; @类型
公共字符串的getType(){
返回类型;
}
// ...
}
最后,我的POJO类:
公共类NewsFeedEnvelope {
// ...
@JsonProperty(行)
私人列表< FeedItem>项目;
// ...
}
现在POJO成功自动杰克逊没有任何自定义解串器反序列化。
My JSON structure:
{
...
"type":"post", // Type could vary
"items":[] // Array of items, each item is typeOf("type")
...
}
How can i deserialize and properly parcel items
list inside my POJO:
public class ItemsEnvelope {
private String type;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = A.class, name = "A"),
@JsonSubTypes.Type(value = B.class, name = "B"),
@JsonSubTypes.Type(value = C.class, name = "C")
})
private List<Item> items;
interface Item extends Parcelable {}
class A implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
class B implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
class C implements Item {
// Bunch of getters/setters and Parcelable methods/constructor
}
// Bunch of getters/setters and Parcelable methods/constructor
}
To parcel typed list a CREATOR
object should be provided, which an interface, obviously, can't have.Should i use an abstract class instead of interface?
Well, since Jackson expects type information on each list element and i didn't want to write a custom deserializer fot this POJO - i solved it another way.
First of all i did an interface which all my subtype items had to implement.
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = PostFeedItem.class, name = BaseFeedItem.TYPE_POST),
@JsonSubTypes.Type(value = PhotoFeedItem.class, name = BaseFeedItem.TYPE_PHOTO),
@JsonSubTypes.Type(value = AudioFeedItem.class, name = BaseFeedItem.TYPE_AUDIO),
@JsonSubTypes.Type(value = VideoFeedItem.class, name = BaseFeedItem.TYPE_VIDEO),
@JsonSubTypes.Type(value = FriendFeedItem.class, name = BaseFeedItem.TYPE_FRIEND)
})
public interface FeedItem extends Parcelable {
// ...
@BaseFeedItem.Type
String getType();
// ...
}
Then i've made a base class, which all my subtype items had to extend.
public abstract class BaseFeedItem implements FeedItem {
public static final String TYPE_POST = "post";
public static final String TYPE_COMMUNITY_POST = "group_post";
public static final String TYPE_PHOTO = "photo";
public static final String TYPE_AUDIO = "audio";
public static final String TYPE_VIDEO = "video";
public static final String TYPE_FRIEND = "friend";
@Retention(RetentionPolicy.SOURCE)
@StringDef({TYPE_POST, TYPE_COMMUNITY_POST, TYPE_PHOTO, TYPE_AUDIO, TYPE_VIDEO, TYPE_FRIEND})
public @interface Type {}
private String type;
@Type
public String getType() {
return type;
}
// ...
}
Finally, my POJO class:
public class NewsFeedEnvelope {
// ...
@JsonProperty("rows")
private List<FeedItem> items;
// ...
}
Now POJO is successfully automatically deserialized by Jackson without any custom deserializers.
这篇关于JsonSubTypes,polymorpic对象和Parcelable列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!