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