用Java序列化JSON子类信息

时间:2013-04-19 14:12:57

标签: java json serialization

我需要一些 JSON 序列化问题的帮助。

我有一个这样的课程:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("type")
@JsonSubTypes({ @JsonSubTypes.Type(value =TextFileCardEntity.class, name = "textCard"),
@JsonSubTypes.Type(value = MultipleChoiceFileCard.class, name = "choiceCard")@JsonSubTypes.Type(value = MultipleChoiceFileCard.class, name = "choiceCard")})
public class FileCardEntity extends ResourceEntity implements Cloneable {
    @NotNull
    @JsonIgnore
    private FileCardType type;
    private Date expirationDate;
        .....

我还有两个扩展了FileCardEntity的类:

@JsonTypeName(value = "textCard")
public class TextFileCardEntity extends FileCardEntity {
    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String question;

    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String answer;
        .......

@Entity
@DiscriminatorValue("1")
@JsonTypeName(value = "choiceCard")
public class MultipleChoiceFileCard extends FileCardEntity {
    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String question;

    @Lob
    @Type(type = "org.hibernate.type.TextType")
    private String crib;
        ......

我使用下一个代码来反序列化信息:

ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
final ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, classElement));
System.out.println(typedWriter.writeValueAsString(elements));

元素是:List<?> elements

当我从服务器反序列化有关TextFileCardEntity的信息时,我得到:

[{"textCard":{"status":"ACTIVE","lastModified":1366293513200,"expirationDate":0.....

没关系

当我尝试将有关TextFileCardEntity的信息序列化到服务器时,代码发送:

[{"FileCardEntity":{"status":"ACTIVE","lastModified":1366378327069,"shared":false,"orderID":0........

这不行,因为我需要在反序列化信息中使用类似的语法 - &GT; [{"textCard":{"status"....而非[{"FileCardEntity":{"status"....

我该怎么办?

0 个答案:

没有答案