1 /* 2 * Copyright (C) 2009 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; 18 19 /** 20 * Defines the expected format for a {@code long} or {@code Long} type when it is serialized. 21 * 22 * @since 1.3 23 * 24 * @author Inderjeet Singh 25 * @author Joel Leitch 26 */ 27 public enum LongSerializationPolicy { 28 /** 29 * This is the "default" serialization policy that will output a {@code Long} object as a JSON 30 * number. For example, assume an object has a long field named "f" then the serialized output 31 * would be: 32 * {@code {"f":123}} 33 * 34 * <p>A {@code null} value is serialized as {@link JsonNull}. 35 */ DEFAULT()36 DEFAULT() { 37 @Override public JsonElement serialize(Long value) { 38 if (value == null) { 39 return JsonNull.INSTANCE; 40 } 41 return new JsonPrimitive(value); 42 } 43 }, 44 45 /** 46 * Serializes a long value as a quoted string. For example, assume an object has a long field 47 * named "f" then the serialized output would be: 48 * {@code {"f":"123"}} 49 * 50 * <p>A {@code null} value is serialized as {@link JsonNull}. 51 */ STRING()52 STRING() { 53 @Override public JsonElement serialize(Long value) { 54 if (value == null) { 55 return JsonNull.INSTANCE; 56 } 57 return new JsonPrimitive(value.toString()); 58 } 59 }; 60 61 /** 62 * Serialize this {@code value} using this serialization policy. 63 * 64 * @param value the long value to be serialized into a {@link JsonElement} 65 * @return the serialized version of {@code value} 66 */ serialize(Long value)67 public abstract JsonElement serialize(Long value); 68 } 69