使用GSON的默认序列化

时间:2018-08-27 12:54:32

标签: json gson

我一直在与GSON合作,它在所有序列化用例中都非常有效,但是有一种情况令我感到震惊。因此,我有一个要序列化的POJO:

@JsonAdapter(ContactSerializer.class)
public class Contact{

    // TODO: Make this impl. implement Parcelable

    @Expose
    private Long contactId;

    @Expose
    private String displayName;

    // DO NOT INCLUDE IN JSON
    private String nickName;

    // DO NOT INCLUDE IN JSON
    // Note: Can be included later
    private String homePhone;

    @Expose
    private String mobilePhone;

    // DO NOT INCLUDE IN JSON
    // Note: Can be included later
    private String workPhone;

    // DO NOT INCLUDE IN JSON
    // Note: Can be included later
    private String photoPath;

    // Combine the below two fields into 1
    private String homeEmail;

    // Instead we combine home email and work email under "emails" for serialization
    private String workEmail;

    // DO NOT INCLUDE IN JSON
    // Note: Can be included later
    private String companyName;

    // DO NOT INCLUDE IN JSON
    // Note: Can be included later
    private String title;

    public Contact(@NonNull Long contactId, @NonNull String displayName) {
        this.contactId = contactId;
        this.displayName = displayName;
    }

    // All fields constructor
    public Contact(@NonNull Long contactId, @NonNull String displayName, String nickName, String homePhone,
                   String mobilePhone, String workPhone, String photoPath, String homeEmail,
                   String workEmail, String companyName, String title) {

        this.contactId = contactId;
        this.displayName = displayName;
        this.nickName = nickName;
        this.homePhone = homePhone;
        this.mobilePhone = mobilePhone;
        this.workPhone = workPhone;
        this.photoPath = photoPath;
        this.homeEmail = homeEmail;
        this.workEmail = workEmail;
        this.companyName = companyName;
        this.title = title;
    }

}

现在,正如您在上面的“联系人”中所看到的,我想将所有电子邮件合并到一个JSON数组中,并添加到最终的JSON中,然后再发送到服务器。

为此,我决定使用自定义序列化器:

public class ContactSerializer implements JsonSerializer<Contact> {

    private final Gson gson = new Gson();

    @Override
    public JsonElement serialize(Contact src, Type typeOfSrc, JsonSerializationContext context) {

        JsonElement jsonElement = context.serialize(src, typeOfSrc);

        List<String> emails = new ArrayList<>();

        if(src.getHomeEmail() != null && !TextUtils.isEmpty(src.getHomeEmail())){
            emails.add(src.getHomeEmail());
        }
        if(src.getWorkEmail() != null && !TextUtils.isEmpty(src.getWorkEmail())){
            emails.add(src.getWorkEmail());
        }
        if(!emails.isEmpty()){

            // Add a field for array
            JsonArray emailJsonArray = new JsonArray();
            for(String email : emails){
                emailJsonArray.add(email);
            }

            jsonElement.getAsJsonObject().add("emails", emailJsonArray);
        }
        else{
            // By default, we would like to have null values represented
            jsonElement.getAsJsonObject().add("emails", null);
        }

        return jsonElement;
    }
}

唯一的问题是,它无法按预期工作-由于调用context.serialize(src, typeOfSrc);

,它进入了无限循环

这是非常出乎意料的,尤其是来自GSON之类的库。是否有任何解决方法或工具可以进行默认序列化然后进行修改?

0 个答案:

没有答案