• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.annotation;
2 
3 import java.lang.annotation.ElementType;
4 import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy;
6 import java.lang.annotation.Target;
7 
8 /**
9  * Annotation used to indicate that a property should be serialized
10  * "unwrapped"; that is, if it would be serialized as JSON Object, its
11  * properties are instead included as properties of its containing
12  * Object. For example, consider case of POJO like:
13  *
14  *<pre>
15  *  public class Parent {
16  *    public int age;
17  *    public Name name;
18  *  }
19  *  public class Name {
20  *    public String first, last;
21  *  }
22  *</pre>
23  * which would normally be serialized as follows (assuming @JsonUnwrapped
24  * had no effect):
25  *<pre>
26  *  {
27  *    "age" : 18,
28  *    "name" : {
29  *      "first" : "Joey",
30  *      "last" : "Sixpack"
31  *    }
32  *  }
33  *</pre>
34  * can be changed to this:
35  *<pre>
36  *  {
37  *    "age" : 18,
38  *    "first" : "Joey",
39  *    "last" : "Sixpack"
40  *  }
41  *</pre>
42  * by changing Parent class to:
43  *<pre>
44  *  public class Parent {
45  *    public int age;
46  *    &#064;JsonUnwrapped
47  *    public Name name;
48  *  }
49  *</pre>
50  * Annotation can only be added to properties, and not classes, as it is contextual.
51  *<p>
52  * Also note that annotation only applies if
53  *<ul>
54  * <li>Value is serialized as JSON Object (can not unwrap JSON arrays using this
55  *   mechanism)
56  *   </li>
57  * <li>Serialization is done using <code>BeanSerializer</code>, not a custom serializer
58  *   </li>
59  * <li>No type information is added; if type information needs to be added, structure can
60  *   not be altered regardless of inclusion strategy; so annotation is basically ignored.
61  *   </li>
62  * </ul>
63  */
64 @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
65 @Retention(RetentionPolicy.RUNTIME)
66 @JacksonAnnotation
67 public @interface JsonUnwrapped
68 {
69     /**
70      * Property that is usually only used when overriding (masking) annotations,
71      * using mix-in annotations. Otherwise default value of 'true' is fine, and
72      * value need not be explicitly included.
73      */
enabled()74     boolean enabled() default true;
75 
76     /**
77      * Optional property that can be used to add prefix String to use in front
78      * of names of properties that are unwrapped: this can be done for example to prevent
79      * name collisions.
80      */
prefix()81     String prefix() default "";
82 
83     /**
84      * Optional property that can be used to add suffix String to append at the end
85      * of names of properties that are unwrapped: this can be done for example to prevent
86      * name collisions.
87      */
suffix()88     String suffix() default "";
89 }
90