Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jackson annotation to suppress unnecessary wrapping of JSON?

Tags:

java

json

jackson

The class I'm serializing:

public class LogsDTO {

    /** The logs. */
    private List<LogDTO> logs;

    /** Meta data. */
    private Meta meta = new Meta();

    // more
}

And the generated JSON:

{"LogsDTO":{"logs":[{"id":11,"archived":false}],"meta":{"totalPages":0}}}

I'd like my JSON to look like:

{"logs":[{"id":11,"archived":false}],"meta":{"totalPages":0}} 

Is there a way to annotate so that this happens?

Thanks

like image 621
user2819101 Avatar asked Nov 01 '22 06:11

user2819101


1 Answers

@JsonRootName: class annotation used to indicate name of "wrapper" entry used for root value, if root-wrapping is enabled.

Says in jackson docs : https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

Related Jira Task: http://jira.codehaus.org/browse/JACKSON-630 1.9 and above versions supports it.

When investigate the source code of @JsonRootName,

They commented alwaysWrap method.

/* * Optional marker property that can be defined as true to force * wrapping of root element, regardless of whether globally * "root wrapping" is enabled or not. *

* Note that value of false is taken to mean "use defaults", * and will not block use of wrapper if use is indicated by global features. * * @since 2.4 public boolean alwaysWrap() default false; */

They have a plan to activate it on v2.5

As of 2.4, one missing feature is property "alwaysWrap", which is hoped * to be added in 2.5, and would be used to force root name wrapping * for individual types.

like image 64
İlker Korkut Avatar answered Nov 09 '22 13:11

İlker Korkut