简体   繁体   English

对象序列化为json,仅某些字段

[英]Object serialization to json, certain fields only

I have a large nested object. 我有一个很大的嵌套对象。 I want to serialise this object in the JSON string, however I need only certain fields to be included. 我想在JSON字符串中序列化此对象,但是我只需要包含某些字段。 Problem here is that fields could change very frequently and I want to build it in a way that could help me easy include or exclude fields for serialisation. 这里的问题是字段可能会非常频繁地更改,我想以一种可以帮助我轻松包含或排除字段进行序列化的方式来构建它。

I know that I can write a lot of code to extract certain fields and build JSON "manually". 我知道我可以编写很多代码来提取某些字段并“手动”构建JSON。 But I wonder if there are any other elegant way to achieve similar outcome but specifying a list of required fields? 但是我想知道是否还有其他优雅的方法可以实现类似的结果,但需要指定必填字段列表?

For example having following object structure I want include only id and name in the response: 例如,具有以下对象结构,我想在响应中仅包含idname

class Building {
    private List<Flat> flats;
}

class Flat {
    private Integer id;     
    private Person owner;
}


class Person {
    private String name;
    private String surname;
}

Json: JSON:

{
    "flats" : [
        {
            "flat":
            {
                "id" : "1",
                "person" : {
                    "name" : "John"
                }
            }
        }
    ]
}

You can use gson for serializing/deserializing JSON . 您可以使用gson序列化/反序列化JSON Then you can include the @Expose annotation to use only the fields you require. 然后,您可以包括@Expose批注以仅使用所需的字段。

Be sure to also configure your Gson object to only serialize "exposed" fields. 确保还将您的Gson对象配置为仅序列化“公开”字段。

Gson gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

Alternative: 替代方案:

You can actually do it the inverse way, marking fields which will not be exposed . 实际上,您可以按相反的方式进行操作,标记不会暴露的字段。 You can do this with the transient keyword. 您可以使用transient关键字来做到这一点。 So whatever you want to ignore just add transient to it. 因此,无论您要忽略什么,只需对其添加transient Here's how it works on gson . 这是在gson上的gson

PS: This works on most Java JSON serializers too. PS:这也适用于大多数Java JSON序列化器。

Using com.fasterxml.jackson.annotation.JsonIgnore is another way to achieve this. 使用com.fasterxml.jackson.annotation.JsonIgnore是实现此目的的另一种方法。

import com.fasterxml.jackson.annotation.JsonIgnore;

class Person {
    private String name;
    @JsonIgnore
    private String surname;
}

It will ignore the surname when the parser converts the bean to json. 当解析器将bean转换为json时,它将忽略surname Similar annotation will be available in other json processing libraries. 类似的注释将在其他json处理库中提供。

If using Gson, study how to use ExclusionStrategy & JsonSerializer. 如果使用Gson,请研究如何使用ExclusionStrategy和JsonSerializer。

Using those is a more flexible way to control serialization since it allows to decide per serialization what to serialize. 使用它们是控制序列化的一种更灵活的方法,因为它允许确定每个序列化序列化的内容。

Using annotations requires later to add / remove those annotations from fields if there is a need to change what to serialize. 如果需要更改要序列化的内容,则以后使用注释需要从字段中添加/删除这些注释。

In the case of your example the latter might be more appropriate. 对于您的示例,后者可能更合适。

This question might be good startpoint serialize-java-object-with-gson 这个问题可能是一个很好的起点serialize-java-object-with-gson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM