【发布时间】:2014-05-22 20:06:37
【问题描述】:
在经历了类似的问题之后,我仍然无法解决这个问题。我正在使用Jackson 来序列化和反序列化没有匹配 getter/setters 方法的类。
public class Products implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty
private final Map<Product, String> prices;
public Products() {
this.prices = new HashMap<>();
}
public void addPrice(final Product product, final String price) {
prices.put(product, price);
}
}
产品类别:
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty
private final String name;
@JsonProperty
private final String type;
public Product(final String price, final String type) {
this.name = price;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public static void main(String[] args) {
Products products = new Products();
products.addPrice(new Product("apple", "fruit"), "16");
JsonDataConverter converter = new JsonDataConverter();
String json = converter.toData(products);
System.out.println(json);
Products deserializedProducts = converter.fromData(json, Products.class);
}
}
JsonDataConverter我使用的是来自AWS Flow Framework:http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/simpleworkflow/flow/JsonDataConverter.html
我在反序列化步骤遇到的异常是:Can not find a (Map) Key deserializer for type [simple type, class mypackage.Product] when mapping key "null"。
我无法理解这一点,因为我的 prices 映射不包含任何空值。奇怪的是,如果 Product 只有 1 个成员(只有 name)字段,它工作正常。
知道出了什么问题吗?
谢谢
【问题讨论】:
-
看起来问题在于您的 Products 地图有一个复合键。 Jackson 需要知道如何将其转换为字符串。
-
@Alexey,它可能是,但它序列化就好了。如果
Product类中只有一个成员,则一切正常。所以不确定这是否是问题。 -
你想要的 JSON 格式是什么?
-
json格式我不太担心,只要序列化和反序列化成功就可以了。
-
我也有同样的问题,它序列化很好,但是我在反序列化时遇到了问题。它给出了以下错误:-没有找到适合类型 [简单类型,类 org.joda.time.chrono.ISOChronology] 的构造函数:无法从 JSON 对象实例化(需要添加/启用类型信息?)
标签: java serialization jackson