001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.json.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021
022import java.lang.annotation.*;
023
024import org.apache.juneau.annotation.*;
025
026/**
027 * Annotation for specifying various JSON options for the JSON serializers and parsers.
028 *
029 * <p>
030 * Can be used in the following locations:
031 * <ul>
032 *    <li>Marshalled classes/methods/fields.
033 *    <li><ja>@Rest</ja>-annotated classes and <ja>@RestOp</ja>-annotated methods when an {@link #on()} value is specified.
034 * </ul>
035 *
036 * <p>
037 * Can be used for the following:
038 * <ul class='spaced-list'>
039 *    <li>
040 *       Wrap bean instances inside wrapper object (e.g. <c>{'wrapperAttr':bean}</c>).
041 * </ul>
042 *
043 * <h5 class='section'>See Also:</h5><ul>
044 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonBasics">JSON Basics</a>
045
046 * </ul>
047 */
048@Documented
049@Target({TYPE,METHOD,FIELD})
050@Retention(RUNTIME)
051@Inherited
052@Repeatable(JsonAnnotation.Array.class)
053@ContextApply(JsonAnnotation.Apply.class)
054public @interface Json {
055
056    /**
057     * Optional description for the exposed API.
058     *
059     * @return The annotation value.
060     * @since 9.2.0
061     */
062    String[] description() default {};
063
064    /**
065    * Dynamically apply this annotation to the specified classes/methods/fields.
066    *
067    * <p>
068    * Used in conjunction with {@link org.apache.juneau.BeanContext.Builder#applyAnnotations(Class...)} to dynamically apply an annotation to an existing class/method/field.
069    * It is ignored when the annotation is applied directly to classes/methods/fields.
070    *
071    * <h5 class='section'>Valid patterns:</h5>
072    * <ul class='spaced-list'>
073    *  <li>Classes:
074    *       <ul>
075    *          <li>Fully qualified:
076    *             <ul>
077    *                <li><js>"com.foo.MyClass"</js>
078    *             </ul>
079    *          <li>Fully qualified inner class:
080    *             <ul>
081    *                <li><js>"com.foo.MyClass$Inner1$Inner2"</js>
082    *             </ul>
083    *          <li>Simple:
084    *             <ul>
085    *                <li><js>"MyClass"</js>
086    *             </ul>
087    *          <li>Simple inner:
088    *             <ul>
089    *                <li><js>"MyClass$Inner1$Inner2"</js>
090    *                <li><js>"Inner1$Inner2"</js>
091    *                <li><js>"Inner2"</js>
092    *             </ul>
093    *       </ul>
094    *    <li>Methods:
095    *       <ul>
096    *          <li>Fully qualified with args:
097    *             <ul>
098    *                <li><js>"com.foo.MyClass.myMethod(String,int)"</js>
099    *                <li><js>"com.foo.MyClass.myMethod(java.lang.String,int)"</js>
100    *                <li><js>"com.foo.MyClass.myMethod()"</js>
101    *             </ul>
102    *          <li>Fully qualified:
103    *             <ul>
104    *                <li><js>"com.foo.MyClass.myMethod"</js>
105    *             </ul>
106    *          <li>Simple with args:
107    *             <ul>
108    *                <li><js>"MyClass.myMethod(String,int)"</js>
109    *                <li><js>"MyClass.myMethod(java.lang.String,int)"</js>
110    *                <li><js>"MyClass.myMethod()"</js>
111    *             </ul>
112    *          <li>Simple:
113    *             <ul>
114    *                <li><js>"MyClass.myMethod"</js>
115    *             </ul>
116    *          <li>Simple inner class:
117    *             <ul>
118    *                <li><js>"MyClass$Inner1$Inner2.myMethod"</js>
119    *                <li><js>"Inner1$Inner2.myMethod"</js>
120    *                <li><js>"Inner2.myMethod"</js>
121    *             </ul>
122    *       </ul>
123    *    <li>Fields:
124    *       <ul>
125    *          <li>Fully qualified:
126    *             <ul>
127    *                <li><js>"com.foo.MyClass.myField"</js>
128    *             </ul>
129    *          <li>Simple:
130    *             <ul>
131    *                <li><js>"MyClass.myField"</js>
132    *             </ul>
133    *          <li>Simple inner class:
134    *             <ul>
135    *                <li><js>"MyClass$Inner1$Inner2.myField"</js>
136    *                <li><js>"Inner1$Inner2.myField"</js>
137    *                <li><js>"Inner2.myField"</js>
138    *             </ul>
139    *       </ul>
140    *    <li>A comma-delimited list of anything on this list.
141    * </ul>
142    *
143    * <h5 class='section'>See Also:</h5><ul>
144    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
145    * </ul>
146    *
147    * @return The annotation value.
148    */
149   String[] on() default {};
150
151   /**
152    * Dynamically apply this annotation to the specified classes.
153    *
154    * <p>
155    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
156    *
157    * <h5 class='section'>See Also:</h5><ul>
158    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
159    * </ul>
160    *
161    * @return The annotation value.
162    */
163   Class<?>[] onClass() default {};
164
165   /**
166    * Wraps beans in a JSON object with the specified attribute name.
167    *
168    * <p>
169    * Applies only to {@link ElementType#TYPE}.
170    *
171    * <p>
172    * This annotation can be applied to beans as well as other objects serialized to other types (e.g. strings).
173    *
174    * <h5 class='figure'>Example:</h5>
175    * <p class='bjava'>
176    *    <ja>@Json</ja>(wrapperAttr=<js>"myWrapper"</js>)
177    *    <jk>public class</jk> MyBean {
178    *       <jk>public int</jk> <jf>f1</jf> = 123;
179    *    }
180    * </p>
181    *
182    * <p>
183    * Without the <ja>@Json</ja> annotations, serializing this bean as JSON would have produced the following...
184    * <p class='bjson'>
185    *    {
186    *       f1: 123
187    *    }
188    * </p>
189    *
190    * <p>
191    * With the annotations, serializing this bean as JSON produces the following...
192    * <p class='bjson'>
193    *    {
194    *       myWrapper: {
195    *          f1: 123
196    *       }
197    *    }
198    * </p>
199    *
200    * @return The annotation value.
201    */
202   String wrapperAttr() default "";
203}