Public 成员函数 | |
| Gson () | |
包函数 | |
| Gson (final Excluder excluder, final FieldNamingStrategy fieldNamingPolicy, final Map< Type, InstanceCreator<?>> instanceCreators, boolean serializeNulls, boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting, boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy, List< TypeAdapterFactory > typeAdapterFactories) | |
包属性 | |
| final JsonDeserializationContext | deserializationContext |
| final JsonSerializationContext | serializationContext |
静态包属性 | |
| static final boolean | DEFAULT_JSON_NON_EXECUTABLE = false |
Private 成员函数 | |
| TypeAdapter< Number > | doubleAdapter (boolean serializeSpecialFloatingPointValues) |
| TypeAdapter< Number > | floatAdapter (boolean serializeSpecialFloatingPointValues) |
| void | checkValidFloatingPoint (double value) |
| TypeAdapter< Number > | longAdapter (LongSerializationPolicy longSerializationPolicy) |
Private 属性 | |
| final ThreadLocal< Map < TypeToken <?>, FutureTypeAdapter<?> > > | calls |
| final Map< TypeToken <?>, TypeAdapter<?> > | typeTokenCache = Collections.synchronizedMap(new HashMap<TypeToken<?>, TypeAdapter<?>>()) |
| final List< TypeAdapterFactory > | factories |
| final ConstructorConstructor | constructorConstructor |
| final boolean | serializeNulls |
| final boolean | htmlSafe |
| final boolean | generateNonExecutableJson |
| final boolean | prettyPrinting |
静态 Private 属性 | |
| static final String | JSON_NON_EXECUTABLE_PREFIX = ")]}'\n" |
This is the main class for using Gson. Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it.
You can create a Gson instance by invoking
if the default configuration is all you need. You can also use GsonBuilder to build a Gson instance with various configuration options such as versioning support, pretty printing, custom JsonSerializers, JsonDeserializers, and InstanceCreators.
Here is an example of how Gson is used for a simple Class:
Gson gson = new Gson(); // Or use new GsonBuilder().create(); MyType target = new MyType(); String json = gson.toJson(target); // serializes target to Json MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
If the object that your are serializing/deserializing is a
(i.e. contains at least one type parameter and may be an array) then you must use the toJson(Object, Type) or fromJson(String, Type) method. Here is an example for serializing and deserialing a
:
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");Gson gson = new Gson(); String json = gson.toJson(target, listType); List<String> target2 = gson.fromJson(json, listType);
See the Gson User Guide for a more complete set of examples.
|
inline |
Constructs a Gson object with default configuration. The default configuration has the following settings:
toJson methods is in compact representation. This means that all the unneeded white-space is removed. You can change this behavior with GsonBuilder#setPrettyPrinting(). versionNumber will be output as "versionNumber; in Json. The same rules are applied for mapping incoming Json to the Java classes. You can change this policy through GsonBuilder#setFieldNamingPolicy(FieldNamingPolicy). transient or static fields from consideration for serialization and deserialization. You can change this behavior through GsonBuilder#excludeFieldsWithModifiers(int...).
|
inlinepackage |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
private |
This thread local guards against reentrant calls to getAdapter(). In certain object graphs, creating an adapter for a type may recursively require an adapter for the same type! Without intervention, the recursive lookup would stack overflow. We cheat by returning a proxy type adapter. The proxy is wired up once the initial adapter has been created.
|
private |
|
staticpackage |
|
package |
|
private |
|
private |
|
private |
|
staticprivate |
|
private |
|
package |
|
private |
|
private |
1.8.8