• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import java.lang.reflect.Type;
20 
21 /**
22  * Context for serialization that is passed to a custom serializer during invocation of its
23  * {@link JsonSerializer#serialize(Object, Type, JsonSerializationContext)} method.
24  *
25  * @author Inderjeet Singh
26  * @author Joel Leitch
27  */
28 public interface JsonSerializationContext {
29 
30   /**
31    * Invokes default serialization on the specified object.
32    *
33    * @param src the object that needs to be serialized.
34    * @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}.
35    */
serialize(Object src)36   public JsonElement serialize(Object src);
37 
38   /**
39    * Invokes default serialization on the specified object passing the specific type information.
40    * It should never be invoked on the element received as a parameter of the
41    * {@link JsonSerializer#serialize(Object, Type, JsonSerializationContext)} method. Doing
42    * so will result in an infinite loop since Gson will in-turn call the custom serializer again.
43    *
44    * @param src the object that needs to be serialized.
45    * @param typeOfSrc the actual genericized type of src object.
46    * @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}.
47    */
serialize(Object src, Type typeOfSrc)48   public JsonElement serialize(Object src, Type typeOfSrc);
49 }
50