简体   繁体   中英

Jackson databind - how to deserialize an object without specifying the target class?

I am migrating from snakeyaml and a feature snakeyaml had is that it automatically wrote the object type in the output YAML so that deserialization would be transparent and easy. I cannot seem to find the same feature in Jackson Databind.

If I merely specify Object.class, I get a LinkedHashMap which is to be expected. Additionally, for this to work, I would have to see the class type in the JSON and I haven't found a native way to do that.

Is this required?

If I understand the problem correctly perhaps you are looking for JsonTypeInfo

From the docs

  // Include Java class name ("com.myempl.ImplClass") as JSON property "class"
  @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")

I ended up prepending the class name before the actual object data and then split that out prior to actually deserializing the data back into an object which is what snakeyaml does automatically. I was hoping to do this automatically in Jackson though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM