I have a JSON input where a given field can either be an ObjectNode or ArrayNode. I have a custom deserializer that "works" but I'm looking to find out if Jackson provides an easier way.
The "data" field of the array can be ObjectNode or ArrayNode.In addition, the fields within can also be ObjectNode or ArrayNode.
Here are some examples of JSON needing to be parsed:
ObjectNode:
{
transactionType: "A2I",
transactionId: 4321,
data: {
field1: "value",
field2: "value"
}
}
ArrayNode
{
transactionType: "BXX",
transactionId: 4322,
data: [{
field1: "value",
field2: "value"
}]
}
Finally:
{
transactionType: "C43",
transactionId: 4322,
data: [{
field1: ["value"],
field2: {
subfield1: "value"
}
}]
}
The fields need to derserialize ito either ArrayList or Map<String, Object>. I do not need to map POJOs.
As I said, I have the custom deserializer, but if Jackson can deal with this case easily, I don't want to go the custom route.
I have a JSON input where a given field can either be an ObjectNode or ArrayNode. I have a custom deserializer that "works" but I'm looking to find out if Jackson provides an easier way.
The "data" field of the array can be ObjectNode or ArrayNode.In addition, the fields within can also be ObjectNode or ArrayNode.
Here are some examples of JSON needing to be parsed:
ObjectNode:
{
transactionType: "A2I",
transactionId: 4321,
data: {
field1: "value",
field2: "value"
}
}
ArrayNode
{
transactionType: "BXX",
transactionId: 4322,
data: [{
field1: "value",
field2: "value"
}]
}
Finally:
{
transactionType: "C43",
transactionId: 4322,
data: [{
field1: ["value"],
field2: {
subfield1: "value"
}
}]
}
The fields need to derserialize ito either ArrayList or Map<String, Object>. I do not need to map POJOs.
As I said, I have the custom deserializer, but if Jackson can deal with this case easily, I don't want to go the custom route.
Share Improve this question asked Mar 4 at 11:19 JasonJason 4,17716 gold badges72 silver badges113 bronze badges 5 |2 Answers
Reset to default 1You could just declare the data
field as TreeNode
then do further logic in your application
public class Transaction {
private String transactionType;
private Long transactionId;
private TreeNode data;
// constructor / getters
}
public class MyService {
private final ObjectMapper mapper = new ObjectMapper();
public void doStuff(String json) {
Transaction transaction = mapper.readValue(json, Transaction.class);
TreeNode data = transaction.getData();
if (data.isArray()) {
for (int i = 0; i < data.size(); ++i) {
processDataNode(data.get(i));
}
} else {
processDataNode(data);
}
}
private void processDataNode(TreeNode dataNode) {
if (dataNode.isObject()) {
Map<String, Object> mapValue = mapper.treeToValue(dataNode, Map.class);
...
} else if (dataNode.isValueNode()) {
String stringValue = dataNode.asText();
...
} else {
...
}
}
}
Since the data node can be anything ,can it be checked for the identifier of data and its before or after characters ,so if you put that json in and as a string you can just check for the data keyword and create a string pattern according to what node it is like data :{ or data:[{ ,this might take more computational time because inside the subfields can be those arraynode or object nodes too
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047357a4608181.html
ObjectMapper
or with annotations. A custom deserializer seems to be the only way, given the unpredictable nature of the JSON. – dani-vta Commented Mar 4 at 11:40transactionType
value? – Raymond Choi Commented Mar 4 at 17:31