英文:
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:
{
"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 ?:
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("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
Thanks in advance
答案1
得分: 1
- JsonNode是一个简单的对象。
public class Paramter {
private List<Parameters> parameters;
//获取器和设置器
public static class Parameters {
private String name;
private JsonNode value;
//获取器和设置器
}
}
- 如果需要具体类,这将对您有所帮助:
https://stackoverflow.com/questions/39923752/jackson-parse-different-model-under-same-key-at-runtime
英文:
For handling dynamic json using jackson.
-
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 }
}
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论