开发者

Jackson JSON : top level segment inclusion

开发者 https://www.devze.com 2023-02-28 02:23 出处:网络
I am referring to this post: convert json to object using jackson , and the la开发者_C百科st answer there by StaxMan, which says it\'s possible in Jackson 1.7 to set the configuration such that Jackso

I am referring to this post: convert json to object using jackson , and the la开发者_C百科st answer there by StaxMan, which says it's possible in Jackson 1.7 to set the configuration such that Jackson starts producing (and maybe parsing too?) the top-level tag/segment in the JSON.

Can someone shed some light on how to set that configuration, and it is only for JSON generation or parsing as well?

Thank you very much!


You need to create and configure the ObjectMapper with the WRAP_ROOT_VALUE feature, e.g.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

See javadoc:

Feature that can be enabled to make root value (usually JSON Object but can be any type) wrapped within a single property JSON object, where key as the "root name", as determined by annotation introspector (esp. for JAXB that uses @XmlRootElement.name) or fallback (non-qualified class name). Feature is mostly intended for JAXB compatibility.

Default setting is false, meaning root value is not wrapped.

The javadoc also says that it hasn't yet been implemented, but I can see it being used in the Jackson source, so it might work OK. I haven't actually tried it myself, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消