Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JsonSubTypes、多病态对象列表和可包裹_Android_Json_Jackson - Fatal编程技术网

Android JsonSubTypes、多病态对象列表和可包裹

Android JsonSubTypes、多病态对象列表和可包裹,android,json,jackson,Android,Json,Jackson,我的JSON结构: { ... "type":"post", // Type could vary "items":[] // Array of items, each item is typeOf("type") ... } 如何在POJO中反序列化并正确打包项目列表: public class ItemsEnvelope { private String type; @JsonTypeInfo(

我的JSON结构:

{  
     ...
     "type":"post", // Type could vary
     "items":[]     // Array of items, each item is typeOf("type") 
     ...
}  
如何在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
}
公共类项目开发{
私有字符串类型;
@JsonTypeInfo(
use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.EXTERNAL\u属性,
property=“type”,
可见=真实)
@JsonSubTypes({
@JsonSubTypes.Type(value=A.class,name=“A”),
@JsonSubTypes.Type(value=B.class,name=“B”),
@JsonSubTypes.Type(value=C.class,name=“C”)
})
私人清单项目;
接口项扩展包裹{}
A类实现项目{
//一堆getter/setter和Parcelable方法/构造函数
}
B类实现项目{
//一堆getter/setter和Parcelable方法/构造函数
}
类C实现一个项目{
//一堆getter/setter和Parcelable方法/构造函数
}
//一堆getter/setter和Parcelable方法/构造函数
}
要创建包裹类型列表,应提供
创建者
对象,这显然是接口所不能提供的。
我应该使用抽象类而不是接口吗?

第一件事:只有当值包含在另一个对象(POJO)中时,
外部属性才会起作用;并且不适用于
List
s、数组或
Map
s

若您使用的是另一种包含方法,那个么应该可以将内容序列化为JSON,并按照预期将生成的JSON读回。也就是说,从Java对象开始时支持往返序列化。
除此之外,还可以支持一些JSON编码结构;但这取决于您试图支持的确切结构类型。

好吧,因为Jackson需要每个列表元素的类型信息,而我不想为此POJO编写自定义反序列化程序-我用另一种方法解决了它

首先,我做了一个接口,我的所有子类型项都必须实现这个接口

@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();
    // ...
}
然后我创建了一个基类,所有子类型项都必须扩展它

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;
    }
    // ...
}
最后,我的POJO课程:

public class NewsFeedEnvelope {
    // ...
    @JsonProperty("rows")
    private List<FeedItem> items;
    // ...
}
公共类新闻提要信封{
// ...
@JsonProperty(“行”)
私人清单项目;
// ...
}

现在,Jackson已成功地自动反序列化POJO,而无需任何自定义反序列化程序。

为问题添加了json结构。在我的例子中应该使用什么包含方法?无法指示JSON数组中所有元素的类型;Jackson需要每个元素的类型信息。因此,为了支持这种安排,您需要编写一个自定义的反序列化程序(如果您想生成这样的JSON,还需要编写序列化程序)。我也有同样的疑问,我需要将子类型对象列表作为json数组发送。