Server returns such part of JSON:
{"condition": {
"or": [
{
"and": [
{
"operand": "a",
"operator": "==",
"value": "true"
},
{
"not": {
"operand": "b",
"operator": "==",
"value": "true"
}
}
]
},
{
"and": [
{
"operand": "b",
"operator": "==",
"value": "true"
},
{
"not": {
"operand": "a",
"operator": "==",
"value": "true"
}
}
]
}
]
}}
I wrote next classes hierarchy:
public interface Condition {}
public class Expression implements Condition {
public Expression(String operator, String operand, String value) {
}
}
public class Not implements Condition {
public Not(Condition condition) {
}
}
public abstract class GroupOperation implements Condition {
public GroupOperation (List<Condition> conditions) {
}
}
public class And extends GroupOperation {
public And(List<Condition> conditions) {
}
}
public class Or extends GroupOperation {
public Or(List<Condition> conditions) {
}
}
I've added next jackson annotations in hope to deserialize JSON above:
@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value=Not.class, name="not"),
@JsonSubTypes.Type(value=And.class, name="and"),
@JsonSubTypes.Type(value=Or.class, name="or"),
@JsonSubTypes.Type(value=Expression.class, name=""),
})
I marked appropriate constructors as @JsonCreator
.
This doesn't work for Expression
class.
If I modify JSON that every expression
object has the name "expression":
"expression" : {
"operand": "a",
"operator": "==",
"value": "true"
}
And
@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value=Not.class, name="not"),
@JsonSubTypes.Type(value=And.class, name="and"),
@JsonSubTypes.Type(value=Or.class, name="or"),
@JsonSubTypes.Type(value=Expression.class, name="expression"),
})
It fails when trying to parse "not" condition saying that "can't instantiate abstract class need more information about type". So looks like it loses annotations declaration in deeper parsing.
I wonder if it's possible to write deserialization with jackson for original JSON
Why second approach doesn't work for
Not
deserialization