1 /* 2 * Copyright (C) 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.gson.annotations; 18 19 import com.google.gson.GsonBuilder; 20 import java.lang.annotation.Documented; 21 import java.lang.annotation.ElementType; 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 import java.lang.annotation.Target; 25 26 /** 27 * An annotation that indicates the version number until a member or a type should be present. 28 * Basically, if Gson is created with a version number that is equal to or exceeds the value 29 * stored in the {@code Until} annotation then the field will be ignored from the JSON output. 30 * This annotation is useful to manage versioning of your JSON classes for a web-service. 31 * 32 * <p> 33 * This annotation has no effect unless you build {@link com.google.gson.Gson} with a 34 * {@code GsonBuilder} and invoke the {@link GsonBuilder#setVersion(double)} method. 35 * 36 * <p>Here is an example of how this annotation is meant to be used:</p> 37 * <pre> 38 * public class User { 39 * private String firstName; 40 * private String lastName; 41 * @Until(1.1) private String emailAddress; 42 * @Until(1.1) private String password; 43 * } 44 * </pre> 45 * 46 * <p>If you created Gson with {@code new Gson()}, the {@code toJson()} and {@code fromJson()} 47 * methods will use all the fields for serialization and deserialization. However, if you created 48 * Gson with {@code Gson gson = new GsonBuilder().setVersion(1.2).create()} then the 49 * {@code toJson()} and {@code fromJson()} methods of Gson will exclude the {@code emailAddress} 50 * and {@code password} fields from the example above, because the version number passed to the 51 * GsonBuilder, {@code 1.2}, exceeds the version number set on the {@code Until} annotation, 52 * {@code 1.1}, for those fields. 53 * 54 * @author Inderjeet Singh 55 * @author Joel Leitch 56 * @see GsonBuilder#setVersion(double) 57 * @see Since 58 * @since 1.3 59 */ 60 @Documented 61 @Retention(RetentionPolicy.RUNTIME) 62 @Target({ElementType.FIELD, ElementType.TYPE}) 63 public @interface Until { 64 65 /** 66 * The value indicating a version number until this member or type should be be included. 67 * The number is exclusive; annotated elements will be included if {@code gsonVersion < value}. 68 */ value()69 double value(); 70 } 71