3

所以我一直试图让杰克逊序列化/反序列化我拥有的一个对象,该对象本质上是结构:

MyObject {
    String a;
    Map<String, Object> map;
}

wheremap可以保存嵌套映射或“原始”值(String, Integer, Long, Double, ...)

显然,由于需要类型信息才能正确反序列化,我不得不告诉杰克逊这样做——为此我使用了默认类型:

return new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, JsonTypeInfo.As.WRAPPER_ARRAY);

该对象最终完全按照我的意愿序列化,仅在必要时添加类型信息:

{
   "a":"SomeString",
   "map":{
      "String1":"String1",
      "Float1":[
         "java.lang.Float",
         1.0
      ],
      "Long1":[
         "java.lang.Long",
         1
      ],
      "Int1":1,
      "Double1":1.0
   }
}

但是,当我尝试使用 Jackson 反序列化此 JSON 时,它失败并出现以下错误:

java.lang.IllegalArgumentException: Can not deserialize instance of <my type> out of START_ARRAY token
at [Source: N/A; line: -1, column: -1] (through reference chain: <my type>["name"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.rbsgbm.agile.mongo.dbobject.JacksonDBObjectConverter.fromDBObject(JacksonDBObjectConverter.java:39)
at com.rbsgbm.agile.mongo.DBCursorIterator.next(DBCursorIterator.java:32)
at com.rbsgbm.agile.repository.StorageBasedRepository$StorageBasedQuery$StorageBasedQueryIterator.next(StorageBasedRepository.java:258)
at com.rbsgbm.agile.repository.StorageBasedRepository$StorageBasedQuery$StorageBasedQueryIterator.next(StorageBasedRepository.java:242)
at com.rbs.agile.strategy.strategymanager.store.mongo.MongoStrategyStore.loadStrategies(MongoStrategyStore.java:81)

这表明它对杰克逊决定不需要类型信息的映射中的值感到困惑,因此没有将其包装在数组中。

有人可以建议正确的方法吗?

4

1 回答 1

4

嗨,伙计,请这样做来解决您的问题

MyObject首先使用适当的 getter 和 setter 方法创建一个类。

/**
 * @author qualebs
 */
public class MyObject {
    private String a;
    private Map<String, Object> map;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}

ObjectMapper然后在您要序列化的类中使用以下配置创建您的实例MyObject

ObjectMapper mapper = new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
        .enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

// get an create a new MyObject
MyObject object = new MyObject();

// set the values you want ie the String a and the Map
Map<String, Object> map = new HashMap<String, Object>();
map.put("string", "example string");
map.put("int", 1);
map.put("long", 1l);
map.put("double", 2.0);
// we can also put an array
map.put("intArray", new int[]{1, 2, 3, 10});

// add the map to your object
object.setMap(map);

// set the string a
object.setA("example String 2");

// now we serialize the object
String mySerializedObj = mapper.writeValueAsString(object);

// to deserialize simply do
MyObject myUnserializedObj = mapper.readValue(mySerializedObj, MyObject.class);

如果这回答了您的问题,请接受我的回答,我可以使用积分。谢谢你。

于 2013-07-17T08:45:54.347 回答