只有序列超领域JSON与杰克逊 [英] Only serialize superclass fields to JSON with Jackson

查看:175
本文介绍了只有序列超领域JSON与杰克逊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用序列杰克逊类的一个实例。
什么是唯一的序列化此实例的超?

I am serializing an instance of a class with Jackson. What is the best way to only serialize the fields of the superclass of this instance?

目前我已经向JSONIgnore注释到每一个方法在我的子类,
有没有更好的方法呢?

Currently I have to add the JSONIgnore annotation to every method in my subclass, is there any better way to do that?

推荐答案

假设你直接通过 ObjectMapper 有序列化过程的控制,你可以很容易地创建一个完成此<一href=\"http://fasterxml.github.com/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/ObjectMapper.html#writerWithType%28java.lang.Class%29\"相对=nofollow> ObjectWriter 具体到你的超类。下面是说明这部分样本code。

Assuming you have control of the serialization process directly via ObjectMapper, you can easily accomplish this by creating an ObjectWriter specific to your superclass. Here is some sample code illustrating this.

public class Account implements Serializable {
    private String accountNumber;
    private String routingNumber;
    private BigDecimal balance;

    // Constructors, setters/getters
}

public class SavingsAccount extends Account {
    private BigDecimal savingsRate;

    // Constructors, setters/getters
}


final ObjectMapper mapper = new ObjectMapper();
final ObjectWriter writer = mapper.writerWithType(Account.class);
final ByteArrayOutputStream baos = new ByteArrayOutputStream(6400);
final SavingsAccount account = new SavingsAccount("0031-3402-2189",
    "0009835011203", BigDecimal.valueOf(53500),
     BigDecimal.valueOf(0.3));

writer.writeValue(baos, account);
final String results = new String(baos.toByteArray());
baos.close();

System.out.println(results);

运行上面的code显示,杰克逊写道超域,即使运行实例的子类型。

Running the above code illustrates that Jackson writes the superclass fields, even though the runtime instance is of the subclass type.

{"accountNumber":"0031-3402-2189","routingNumber":"0009835011203","balance":53500}


编辑:

谢谢,但在我的情况下,我尽量做到这种行为好
  类是序列化的外部类的领域。我可以定义
  类型一般多吗?

Thanks, but in my case I try to achieve this behaviour for several classes are fields of the outer class that I serialize. Can I define the types more in general?

这情景更容易对付。只是注释在外部类中的相应字段与<一个href=\"http://jackson.$c$chaus.org/1.9.9/javadoc/index.html?org/$c$chaus/jackson/annotate/JsonIgnore.html\"相对=nofollow> @ JsonSerialize 。如果声明的字段作为超类型,然后设置键入静态。这将标志使用,而不是运行时类型的编译时类型进行序列化杰克逊的领域。

This scenario is even easier to deal with. Just annotate the appropriate fields in the outer class with @JsonSerialize. If you declare the fields as the superclass type, then set the typing as static. This will flag those fields to Jackson to be serialized using the compile time type as opposed to the runtime type.

在另一方面,如果声明的字段为子类型,然后设置使用作为父类。

On the other hand, if you declare the fields as the subclass type, then set the using as the superclass.

下面的示例使用我上面进一步定义的类:

The following example uses the classes I defined further above:

public class BankCustomer implements Serializable {
    @JsonSerialize(typing=Typing.STATIC)
    private Account cdAccount;

    @JsonSerialize(using=Account.class)
    private SavingsAccount savingsAccount;
}

这篇关于只有序列超领域JSON与杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆