Finish second-to-last commit
[unical.git] / gson / com / google / gson / JsonDeserializer.java
CommitLineData
cfd903b6
MG
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
17package com.google.gson;
18
19import java.lang.reflect.Type;
20
21/**
22 * <p>Interface representing a custom deserializer for Json. You should write a custom
23 * deserializer, if you are not happy with the default deserialization done by Gson. You will
24 * also need to register this deserializer through
25 * {@link GsonBuilder#registerTypeAdapter(Type, Object)}.</p>
26 *
27 * <p>Let us look at example where defining a deserializer will be useful. The {@code Id} class
28 * defined below has two fields: {@code clazz} and {@code value}.</p>
29 *
30 * <pre>
31 * public class Id&lt;T&gt; {
32 * private final Class&lt;T&gt; clazz;
33 * private final long value;
34 * public Id(Class&lt;T&gt; clazz, long value) {
35 * this.clazz = clazz;
36 * this.value = value;
37 * }
38 * public long getValue() {
39 * return value;
40 * }
41 * }
42 * </pre>
43 *
44 * <p>The default deserialization of {@code Id(com.foo.MyObject.class, 20L)} will require the
45 * Json string to be <code>{"clazz":com.foo.MyObject,"value":20}</code>. Suppose, you already know
46 * the type of the field that the {@code Id} will be deserialized into, and hence just want to
47 * deserialize it from a Json string {@code 20}. You can achieve that by writing a custom
48 * deserializer:</p>
49 *
50 * <pre>
51 * class IdDeserializer implements JsonDeserializer&lt;Id&gt;() {
52 * public Id deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
53 * throws JsonParseException {
54 * return new Id((Class)typeOfT, id.getValue());
55 * }
56 * </pre>
57 *
58 * <p>You will also need to register {@code IdDeserializer} with Gson as follows:</p>
59 *
60 * <pre>
61 * Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdDeserializer()).create();
62 * </pre>
63 *
64 * <p>New applications should prefer {@link TypeAdapter}, whose streaming API
65 * is more efficient than this interface's tree API.
66 *
67 * @author Inderjeet Singh
68 * @author Joel Leitch
69 *
70 * @param <T> type for which the deserializer is being registered. It is possible that a
71 * deserializer may be asked to deserialize a specific generic type of the T.
72 */
73public interface JsonDeserializer<T> {
74
75 /**
76 * Gson invokes this call-back method during deserialization when it encounters a field of the
77 * specified type.
78 * <p>In the implementation of this call-back method, you should consider invoking
79 * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
80 * for any non-trivial field of the returned object. However, you should never invoke it on the
81 * the same type passing {@code json} since that will cause an infinite loop (Gson will call your
82 * call-back method again).
83 *
84 * @param json The Json data being deserialized
85 * @param typeOfT The type of the Object to deserialize to
86 * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
87 * @throws JsonParseException if json is not in the expected format of {@code typeofT}
88 */
89 public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
90 throws JsonParseException;
91}
This page took 0.013782 seconds and 4 git commands to generate.