Davi Alves
Davi Alves

Reputation: 1744

Deserialize protected constructor with Jackson

Hi I'm trying to use jackson to serialize and deserialize a class(SimpleExpression) with protected constructor. When I use gson I don't have any problem for that, but it seems that jackson can't handle protected constructors. I tried using mixin-annotations but didn't worked. Serialization works without any problem. Jackson throws:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type

Any help? My code:

private static SimpleExpression create(String json) throws JsonParseException, JsonMappingException, IOException
{
    ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, Visibility.ANY);
    SimpleExpression sp = null;
    sp = mapper.readValue(json, SimpleExpression.class);
    return sp;
}

SimpleExpression class, i'm omiting the getter and setters.

public class SimpleExpression implements Criterion
{
private static final long serialVersionUID = 1L;

private final String propertyName;

private final Object value;

private boolean ignoreCase;

private final String op;

private final String type;

protected SimpleExpression(String propertyName, Object value, String op)
{
    this.propertyName = propertyName;
    this.value = value;
    this.op = op;
    this.type = value.getClass().getName();
}

protected SimpleExpression(String propertyName, Object value, String op, boolean ignoreCase)
{
    this.propertyName = propertyName;
    this.value = value;
    this.ignoreCase = ignoreCase;
    this.op = op;
    this.type = value.getClass().getName();
}
}

Upvotes: 2

Views: 9428

Answers (2)

user2511713
user2511713

Reputation: 555

Just if any1 comes to the thread. I was having the same issue. I just add a constructor

protected SimpleExpression(){}

and it worked fine

Upvotes: 0

StaxMan
StaxMan

Reputation: 116522

Protected part should not be problematic (they are found ok) but perhaps constructor takes arguments. To indicate non-default constructor to use, you use @JsonCreator; but beyond that it depends on what kind of constructor (or static factory method) to use.

But to know details, class definition is needed. Another possibility is that you are trying to deal with non-static inner class.

Upvotes: 3

Related Questions