json - 使用 Jackson 反序列化 protected 构造函数

标签 json serialization jackson

嗨,我正在尝试使用 jackson 来序列化和反序列化具有 protected 构造函数的类(SimpleExpression)。当我使用 gson 时,我没有任何问题,但 jackson 似乎无法处理 protected 构造函数。我尝试使用 mixin-annotations 但没有成功。序列化工作没有任何问题。 jackson throw :

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

有什么帮助吗? 我的代码:

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 类,我省略了 getter 和 setter。

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();
}
}

最佳答案

protected 部分应该不会有问题(它们被发现没问题),但构造函数可能需要参数。要指示要使用的非默认构造函数,请使用 @JsonCreator;但除此之外,这取决于使用哪种构造函数(或静态工厂方法)。

但要了解详细信息,需要类定义。另一种可能性是您正在尝试处理非静态内部类。

关于json - 使用 Jackson 反序列化 protected 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317955/

相关文章:

android - 单个数组的JSON解析和数组名

java - Jackson 无法将 JSON 映射到包含 Map 的对象

java - "com.fasterxml.jackson.databind.JsonMappingException: Expected type float, integer, or string."使用 ObjectMapper 转换 java.time.Instant

json - jq - 将 json 转换为 csv - 如何将 "null"视为字符串?

javascript - jQuery json 值没有被附加

c# - 尽管将 PreserveReferencesHandling 设置为 "None",Json.Net 仍将 $id 添加到 EF 对象

java - 使用 Jackson 反序列化派生类

java - 使用 Jackson 创建 JSON

c# - 在 VS 2012 项目中启用 System.Net.Http

java - Serializable/Cloneable/... 的惯用设计在 Scala 中看起来如何?