简体   繁体   English

如何将 @JsonProperty 注释与包含不同格式元素的数组一起使用

[英]How to use @JsonProperty annotation with array containing elements of different format

I'm using an API that uses in requests & responses json array in a SpringBoot App with elements of various format and I'm struggling to dynamically get the right class to be used for serialisation/deserialisation of elements of such array.我正在使用 API,它在 SpringBoot 应用程序中的请求和响应中使用 json 数组,其中包含各种格式的元素,我正在努力动态获取正确的 class 元素以用于序列化/反序列化。

Here is a simplified description of the json:以下是 json 的简化说明:

{
    "parameters" :[
        {
            "name": "elementA",
            "value": "valueOfElementAsAString"
        },
        {
            "name": "elementB",
            "value": "valueOfElementBAsString"
        },
        {
            "name": "elementC",
            "value": [
                {
                    "label":"label1",
                    "size":"size1"
                },
                {
                    "label":"label2",
                    "size":"size2"
                }

            ] 
        }
    ]
}

Considering the following base classes:考虑以下基类:

class SimpleElement {
    @JsonProperty("name")
    public String name;
    @JsonProperty("value")
    public String value;

    //constructor & stuff 
}


class ComplexElement {
    @JsonProperty("name")
    public String name;
    @JsonProperty("value")
    public ArrayList<ComplexElementValue> value = new ArrayList<ComplexElementValue>();

    //constructor & stuff 
}

class ComplexElementValue {
    @JsonProperty("label")
    public String label;
    @JsonProperty("size")
    public String size;  

    //constructor & stuff   
}

Serialization/Deserialisation of individual Simple or Complex Elements works fine but how shall I declare my class Parameters using @JsonProperty to get the full array correctly handled?:单个简单或复杂元素的序列化/反序列化工作正常,但我应该如何使用 @JsonProperty 声明我的 class 参数以正确处理整个数组?:

class Parameters {
    ???
}

Based on what I read, I've got the impression I have to write custom deserialiser & serialiser but before doing so I would like to make sure there is no simpler way?根据我阅读的内容,我的印象是我必须编写自定义反序列化器和序列化器,但在这样做之前我想确保没有更简单的方法?

At the moment, considering I've got nx simpleElements and 1 x complexElement, I get it to work with such a class:目前,考虑到我有 nx simpleElements 和 1 x complexElement,我让它与这样的 class 一起工作:

class Parameter {
   @JsonProperty("parameters")
   public MultiElement element;

   public class MultiElement {
      public ArrayList<SimpleElement> regularElements;
      public ComplexElement complexElement;
   }
  
   //constructor & stuff 
}

and handling the deserialisation/serialisation of the json "manually" using a JsonNode library (in this case org.camunda.spin.json.SpinJsonNode), but this sounds for me like a temporary solution并使用 JsonNode 库(在本例中为 org.camunda.spin.json.SpinJsonNode)“手动”处理 json 的反序列化/序列化,但这对我来说听起来像是一个临时解决方案

Thanks in advance提前致谢

For handling dynamic json using jackson.使用 jackson 处理动态 json。

  1. JsonNode is simple one. JsonNode 很简单。

    public class Paramter { private List parameters;公共 class 参数 { 私有列表参数; //Getters and setters public static class Parameters { //getter和setter public static class 参数 {

     private String name; private JsonNode value; //Getters and setters }

    } }

  2. this will help you correctly if if need concrete class Jackson - parse different model under same key at runtime如果需要具体的 class Jackson,这将正确地帮助您 - 在运行时在同一密钥下解析不同的 model

Basically try to convert json to java class using any online converters like http://www.jsonschema2pojo.org/基本上尝试使用任何在线转换器将 json 转换为 java class

Try using modelmapper library for object mapping.尝试使用 modelmapper 库进行 object 映射。 Like mapping json objects to java objects.就像将 json 对象映射到 java 对象一样。

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

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