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 020/** 021 * Annotation for specifying various JSON options for the JSON serializers and parsers. 022 * 023 * <p> 024 * Can be applied to Java types. 025 * 026 * <p> 027 * Can be used for the following: 028 * <ul class='spaced-list'> 029 * <li> 030 * Wrap bean instances inside wrapper object (e.g. <c>{'wrapperAttr':bean}</c>). 031 * </ul> 032 */ 033@Documented 034@Target({TYPE,METHOD,FIELD}) 035@Retention(RUNTIME) 036@Inherited 037public @interface Json { 038 039 /** 040 * Dynamically apply this annotation to the specified classes/methods/fields. 041 * 042 * <p> 043 * Used in conjunction with the {@link JsonConfig#applyJson()}. 044 * It is ignored when the annotation is applied directly to classes/methods/fields. 045 * 046 * <h5 class='section'>Valid patterns:</h5> 047 * <ul class='spaced-list'> 048 * <li>Classes: 049 * <ul> 050 * <li>Fully qualified: 051 * <ul> 052 * <li><js>"com.foo.MyClass"</js> 053 * </ul> 054 * <li>Fully qualified inner class: 055 * <ul> 056 * <li><js>"com.foo.MyClass$Inner1$Inner2"</js> 057 * </ul> 058 * <li>Simple: 059 * <ul> 060 * <li><js>"MyClass"</js> 061 * </ul> 062 * <li>Simple inner: 063 * <ul> 064 * <li><js>"MyClass$Inner1$Inner2"</js> 065 * <li><js>"Inner1$Inner2"</js> 066 * <li><js>"Inner2"</js> 067 * </ul> 068 * </ul> 069 * <li>Methods: 070 * <ul> 071 * <li>Fully qualified with args: 072 * <ul> 073 * <li><js>"com.foo.MyClass.myMethod(String,int)"</js> 074 * <li><js>"com.foo.MyClass.myMethod(java.lang.String,int)"</js> 075 * <li><js>"com.foo.MyClass.myMethod()"</js> 076 * </ul> 077 * <li>Fully qualified: 078 * <ul> 079 * <li><js>"com.foo.MyClass.myMethod"</js> 080 * </ul> 081 * <li>Simple with args: 082 * <ul> 083 * <li><js>"MyClass.myMethod(String,int)"</js> 084 * <li><js>"MyClass.myMethod(java.lang.String,int)"</js> 085 * <li><js>"MyClass.myMethod()"</js> 086 * </ul> 087 * <li>Simple: 088 * <ul> 089 * <li><js>"MyClass.myMethod"</js> 090 * </ul> 091 * <li>Simple inner class: 092 * <ul> 093 * <li><js>"MyClass$Inner1$Inner2.myMethod"</js> 094 * <li><js>"Inner1$Inner2.myMethod"</js> 095 * <li><js>"Inner2.myMethod"</js> 096 * </ul> 097 * </ul> 098 * <li>Fields: 099 * <ul> 100 * <li>Fully qualified: 101 * <ul> 102 * <li><js>"com.foo.MyClass.myField"</js> 103 * </ul> 104 * <li>Simple: 105 * <ul> 106 * <li><js>"MyClass.myField"</js> 107 * </ul> 108 * <li>Simple inner class: 109 * <ul> 110 * <li><js>"MyClass$Inner1$Inner2.myField"</js> 111 * <li><js>"Inner1$Inner2.myField"</js> 112 * <li><js>"Inner2.myField"</js> 113 * </ul> 114 * </ul> 115 * <li>A comma-delimited list of anything on this list. 116 * </ul> 117 * 118 * <ul class='seealso'> 119 * <li class='link'>{@doc juneau-marshall.DynamicallyAppliedAnnotations} 120 * </ul> 121 */ 122 String on() default ""; 123 124 /** 125 * Wraps beans in a JSON object with the specified attribute name. 126 * 127 * <p> 128 * Applies only to {@link ElementType#TYPE}. 129 * 130 * <p> 131 * This annotation can be applied to beans as well as other objects serialized to other types (e.g. strings). 132 * 133 * <h5 class='figure'>Example:</h5> 134 * <p class='bcode w800'> 135 * <ja>@Json</ja>(wrapperAttr=<js>"myWrapper"</js>) 136 * <jk>public class</jk> MyBean { 137 * <jk>public int</jk> f1 = 123; 138 * } 139 * </p> 140 * 141 * <p> 142 * Without the <ja>@Json</ja> annotations, serializing this bean as JSON would have produced the following... 143 * <p class='bcode w800'> 144 * { 145 * f1: 123 146 * } 147 * </p> 148 * 149 * <p> 150 * With the annotations, serializing this bean as JSON produces the following... 151 * <p class='bcode w800'> 152 * { 153 * myWrapper: { 154 * f1: 123 155 * } 156 * } 157 * </p> 158 */ 159 String wrapperAttr() default ""; 160}