Jackson 序列化:忽略空值(或 null) [英] Jackson serialization: ignore empty values (or null)

查看:61
本文介绍了Jackson 序列化:忽略空值(或 null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是 jackson 2.1.4,当我将对象转换为 JSON 字符串时,我在忽略字段时遇到了一些麻烦.

I'm currently using jackson 2.1.4 and I'm having some trouble ignoring fields when I'm converting an object to a JSON string.

这是我的类,它充当要转换的对象:

Here's my class which acts as the object to be converted:

public class JsonOperation {

public static class Request {
    @JsonInclude(Include.NON_EMPTY)
    String requestType;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        String username;
        String email;
        String password;
        String birthday;
        String coinsPackage;
        String coins;
        String transactionId;
        boolean isLoggedIn;
    }
}

public static class Response {
    @JsonInclude(Include.NON_EMPTY)
    String requestType = null;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        enum ErrorCode { ERROR_INVALID_LOGIN, ERROR_USERNAME_ALREADY_TAKEN, ERROR_EMAIL_ALREADY_TAKEN };
        enum Status { ok, error };

        Status status;
        ErrorCode errorCode;
        String expiry;
        int coins;
        String email;
        String birthday;
        String pictureUrl;
        ArrayList <Performer> performer;
    }
}
}

这里是我如何转换它:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

JsonOperation subscribe = new JsonOperation();

subscribe.request.requestType = "login";

subscribe.request.data.username = "Vincent";
subscribe.request.data.password = "test";


Writer strWriter = new StringWriter();
try {
    mapper.writeValue(strWriter, subscribe.request);
} catch (JsonGenerationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Log.d("JSON", strWriter.toString())

输出如下:

{"data":{"birthday":null,"coins":null,"coinsPackage":null,"email":null,"username":"Vincent","password":"test","transactionId":null,"isLoggedIn":false},"requestType":"login"}

我怎样才能避免那些空值?我只想获取订阅"所需的信息!

How can I avoid those null values? I only want to take required information for the "subscription" purpose!

这正是我正在寻找的输出:

Here's exactly the output that I'm looking for:

{"data":{"username":"Vincent","password":"test"},"requestType":"login"}

我也尝试过@JsonInclude(Include.NON_NULL) 并将我的所有变量设置为空,但它也不起作用!谢谢大家的帮助!

I also tried @JsonInclude(Include.NON_NULL) and put all my variables to null, but it didn't work either! Thanks for your help guys!

推荐答案

您的注释放错了位置 - 它需要在类上,而不是在字段上.即:

You have the annotation in the wrong place - it needs to be on the class, not the field. i.e:

@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case 
public static class Request {
  // ...
}

如评论中所述,在低于 2.x 的版本中,此注释的语法为:

As noted in comments, in versions below 2.x the syntax for this annotation is:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY

另一个选项是直接配置ObjectMapper,只需调用mapper.setSerializationInclusion(Include.NON_NULL);

The other option is to configure the ObjectMapper directly, simply by calling mapper.setSerializationInclusion(Include.NON_NULL);

(为了记录,我认为这个答案的流行表明这个注释应该在逐个字段的基础上适用,@fasterxml)

(for the record, I think the popularity of this answer is an indication that this annotation should be applicable on a field-by-field basis, @fasterxml)

这篇关于Jackson 序列化:忽略空值(或 null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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