Json使用相同的名称进行序列化和反序列化

时间:2019-08-19 17:45:32

标签: java json serialization jackson json-deserialization

当我使用请求正文调用API时,会在POJO中使用变量名反序列化它。我修改了相同的列表并返回,但它重复了该列表

@JsonSerialize
@JsonIgnoreProperties(ignoreUnknown = true)
public class UASchema {

    @JsonProperty("metric_id")
    private ArrayList<String> fMetricId;
    @JsonProperty("schema")
    private ArrayList<String> fSchema;
    @JsonProperty("hash")
    private String fHash;
...
...
//getter and setters
}

请求正文为

{
    "data" : [
        {
            "metric_id": ["k1", "ak2", "d90"],
            "schema": ["s1", "s2"]
        },
        {
            "metric_id": ["k21", "k22"],
            "schema": ["a11", "s22"]    
        }
    ]
}

我得到的响应是(添加了哈希)

{
    "result": [
        {
            "fmetricId": [
                "k1",
                "ak2",
                "d90"
            ],
            "fschema": [
                "s1",
                "s2"
            ],
            "metric_id": [
                "k1",
                "ak2",
                "d90"
            ],
            "schema": [
                "s1",
                "s2"
            ],
            "hash": "389abc9093442cfd2aee1f20807ba467"
        },
        {
            "fmetricId": [
                "k21",
                "k22"
            ],
            "fschema": [
                "a11",
                "s22"
            ],
            "metric_id": [
                "k21",
                "k22"
            ],
            "schema": [
                "a11",
                "s22"
            ],
            "hash": "5f366dde65b69fa679f95a81f3115b7f"
        }
    ]
}

它复制了列表,并且没有正确序列化它。我希望响应与请求正文具有相同的列表,并添加了哈希。

1 个答案:

答案 0 :(得分:0)

看起来您的算法重复了条目,或者您手动生成了getterssetters,它们重复了输出。默认情况下,Jackson不添加额外的条目。请参阅以下示例,您如何做到这一点,我在getters中生成了settersIDE。字段的f-fieldName模式已经过时,您应该使用常规名称。例如,参见Google's Java Guide

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.File;
import java.util.List;
import java.util.UUID;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Root root = mapper.readValue(jsonFile, Root.class);
        root.getData().forEach(s -> s.setfHash(UUID.randomUUID().toString()));

        System.out.println(mapper.writeValueAsString(root));
    }
}

class Root {
    private List<UASchema> data;

    public List<UASchema> getData() {
        return data;
    }

    public void setData(List<UASchema> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Root{" +
                "data=" + data +
                '}';
    }
}

class UASchema {

    @JsonProperty("metric_id")
    private List<String> fMetricId;

    @JsonProperty("schema")
    private List<String> fSchema;

    @JsonProperty("hash")
    private String fHash;

    public List<String> getfMetricId() {
        return fMetricId;
    }

    public void setfMetricId(List<String> fMetricId) {
        this.fMetricId = fMetricId;
    }

    public List<String> getfSchema() {
        return fSchema;
    }

    public void setfSchema(List<String> fSchema) {
        this.fSchema = fSchema;
    }

    public String getfHash() {
        return fHash;
    }

    public void setfHash(String fHash) {
        this.fHash = fHash;
    }

    @Override
    public String toString() {
        return "UASchema{" +
                "fMetricId=" + fMetricId +
                ", fSchema=" + fSchema +
                ", fHash='" + fHash + '\'' +
                '}';
    }
}

上面的代码显示:

{
  "data" : [ {
    "metric_id" : [ "k1", "ak2", "d90" ],
    "schema" : [ "s1", "s2" ],
    "hash" : "80dcf06d-1d83-463c-afb8-edef8efdc71f"
  }, {
    "metric_id" : [ "k21", "k22" ],
    "schema" : [ "a11", "s22" ],
    "hash" : "a83d7981-4b80-4318-a632-f3c91d14379b"
  } ]
}