连载类成员为JSON对象 [英] Serialising a class Member into JSON Object

查看:194
本文介绍了连载类成员为JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作如何序列化的POJO转换成JSON字符串。我使用的是杰克逊图书馆和所遇到的问题。

I am working on how to serialised a POJO into a json string. I am using the jackson library and have run into a problem.

在JSON对象的值可以是一个字符串或字符串数​​组像这样...

A value in the json object can either be a string or a string array like so...

{"mimeTypes":"all"}

 {"mimeTypes":["application/pdf", "application/msword"]}

这是我的做法在目前

@JsonIgnore
private String mimeTypes;
@JsonIgnore
private String[] mimeTypesArray;
@JsonRawValue
@JsonProperty("integration/enabled-mime-types")
private String mimeType;

public void setMimeTypes(String mimeTypes) {
    this.mimeTypes = mimeTypes;
    mimeType = mimeTypes;
}

public void setMimeTypes(String[] mimeTypes) {
    this.mimeTypesArray = mimeTypes;
    try {
        JSONArray jsonArray = new JSONArray(mimeTypes);
        this.mimeType = jsonArray.toString();
    } catch (JSONException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

基本上我有一个字段的字符串,字符串数组的字段,并为我想要添加到JSON对象字符串的字段。我上连载二传手所以对象映射器没有选择使用哪个字段。

Basically I have a field for the string, a field for the string array, and a field for the string I want to add to the json object. I serialise on the setter so the object mapper doesn't have to choose which field to use.

这感觉就像做事情的方式哈克。会不会有更好的解决办法?我一直在阅读有关使用杰克逊的意见,但我不知道这是否可能对我的问题。

This feels like a hacky way of doing things. Would there be a better solution? I have been reading about using jackson views but I am not sure if this is possible for my problem.

推荐答案


我没有测试过,但我想尝试增加专用方法计算应该怎样序列化:

I haven't tested it, but I'd try adding a dedicated method for computing what should be serialized:

class MyMimeTypes {
    @JsonIgnore
    private String[] mimeTypesArray;

    /** A convenience method for setting just one item. */
    public void setMimeType(String mimeType) {
        this.mimeTypesArray = new String[] { mimeType };
    }

    public void setMimeTypes(String[] mimeTypes) {
        this.mimeTypesArray = mimeTypes;
    }

    // The JSON-specific part:

    @JsonProperty("mimeTypes")
    public Object jsonMimeTypes() {
        if (mimeTypesArray.length == 1)
            return mimeTypesArray[0];
        else
            return mimeTypesArray;
    }
}

这样,如果阵列中只有一个项目,序列化的值将只是该项目。否则,整个数组将被序列化。

This way, if you have just one item in the array, the serialized value will be just the item. Otherwise the whole array will be serialized.

请注意,这是具有两个setter方法​​具有相同的名称和不同的签名一种不好的做法。 AFAIK违反了Java Beans规范。

Note that it is a bad practise having two setters with the same name and different signatures. AFAIK that violates the Java Beans specification.

也许你可以使用一个可变参数的方法在这里代替两个setter方法​​(但我不知道如何去这与Java Beans规范起来):

Perhaps you could use a vararg method here to replace the two setters (but I don't know how this goes together with Java Beans specification):

public void setMimeTypes(String... mimeTypes) {
    this.mimeTypesArray = mimeTypes;
}

这篇关于连载类成员为JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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