如何在包含不同格式元素的数组中使用 @JsonProperty 注解

huangapple go评论63阅读模式
英文:

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.

Here is a simplified description of the 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/Deserialization 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 ?:

class Parameters {
    ???
}

Based on what I read, I've got the impression I have to write custom deserializer & serialiser but before doing so I would like to make sure there is no simpler way ?

At the moment, considering I've got n x simpleElements and 1 x complexElement, I get it to work with such a class:

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

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

and handling the deserialization/serialization 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

Thanks in advance

英文:

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.

Here is a simplified description of the json:

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

            ] 
        }
    ]
}

Considering the following base classes:

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

    //constructor &amp; stuff 
}


class ComplexElement {
    @JsonProperty(&quot;name&quot;)
    public String name;
    @JsonProperty(&quot;value&quot;)
    public ArrayList&lt;ComplexElementValue&gt; value = new ArrayList&lt;ComplexElementValue&gt;();

    //constructor &amp; stuff 
}

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

    //constructor &amp; 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 ?:

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 n x simpleElements and 1 x complexElement, I get it to work with such a class:

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

   public class MultiElement {
      public ArrayList&lt;SimpleElement&gt; regularElements;
      public ComplexElement complexElement;
   }
  
   //constructor &amp; 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

Thanks in advance

答案1

得分: 1

  1. JsonNode是一个简单的对象。
public class Paramter {
  private List<Parameters> parameters;
  //获取器和设置器
  public static class Parameters {

          private String name;
          private JsonNode value;
         //获取器和设置器
        }
 }
  1. 如果需要具体类,这将对您有所帮助:
    https://stackoverflow.com/questions/39923752/jackson-parse-different-model-under-same-key-at-runtime
英文:

For handling dynamic json using jackson.

  1. JsonNode is simple one.

    public class Paramter {
    private List<Parameters> parameters;
    //Getters and setters
    public static class Parameters {

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

    }

  2. this will help you correctly if if need concrete class
    https://stackoverflow.com/questions/39923752/jackson-parse-different-model-under-same-key-at-runtime

答案2

得分: 0

Sure, here's the translation:

基本上尝试使用类似 http://www.jsonschema2pojo.org/ 这样的在线转换工具将 JSON 转换为 Java 类。

尝试使用 ModelMapper 库进行对象映射,例如将 JSON 对象映射到 Java 对象。

英文:

Basically try to convert json to java class using any online converters like http://www.jsonschema2pojo.org/

Try using modelmapper library for object mapping. Like mapping json objects to java objects.

huangapple
  • 本文由 发表于 2020年7月28日 01:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63120082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定