AndBase开发框架  1.6
 全部  命名空间 文件 函数 变量 枚举值 
Public 成员函数 | 包函数 | 包属性 | 静态包属性 | Private 成员函数 | Private 属性 | 静态 Private 属性 | 所有成员列表
com.google.gson.Gson类 参考

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< TypeAdapterFactoryfactories
 
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

new Gson()

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

ParameterizedType

(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

ParameterizedType

:

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.

参见
com.google.gson.reflect.TypeToken
作者
Inderjeet Singh
Joel Leitch

构造及析构函数说明

com.google.gson.Gson.Gson ( )
inline

Constructs a Gson object with default configuration. The default configuration has the following settings:

com.google.gson.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 
)
inlinepackage

成员函数说明

void com.google.gson.Gson.checkValidFloatingPoint ( double  value)
inlineprivate
TypeAdapter<Number> com.google.gson.Gson.doubleAdapter ( boolean  serializeSpecialFloatingPointValues)
inlineprivate
TypeAdapter<Number> com.google.gson.Gson.floatAdapter ( boolean  serializeSpecialFloatingPointValues)
inlineprivate
TypeAdapter<Number> com.google.gson.Gson.longAdapter ( LongSerializationPolicy  longSerializationPolicy)
inlineprivate

类成员变量说明

final ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?> > > com.google.gson.Gson.calls
private
初始值:
= new ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>>() {
@Override protected Map<TypeToken<?>, FutureTypeAdapter<?>> initialValue() {
return new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
}
}

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.

final ConstructorConstructor com.google.gson.Gson.constructorConstructor
private
final boolean com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE = false
staticpackage
final JsonDeserializationContext com.google.gson.Gson.deserializationContext
package
初始值:
= new JsonDeserializationContext() {
@SuppressWarnings("unchecked")
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (T) fromJson(json, typeOfT);
}
}
final List<TypeAdapterFactory> com.google.gson.Gson.factories
private
final boolean com.google.gson.Gson.generateNonExecutableJson
private
final boolean com.google.gson.Gson.htmlSafe
private
final String com.google.gson.Gson.JSON_NON_EXECUTABLE_PREFIX = ")]}'\n"
staticprivate
final boolean com.google.gson.Gson.prettyPrinting
private
final JsonSerializationContext com.google.gson.Gson.serializationContext
package
初始值:
= new JsonSerializationContext() {
public JsonElement serialize(Object src) {
return toJsonTree(src);
}
public JsonElement serialize(Object src, Type typeOfSrc) {
return toJsonTree(src, typeOfSrc);
}
}
final boolean com.google.gson.Gson.serializeNulls
private
final Map<TypeToken<?>, TypeAdapter<?> > com.google.gson.Gson.typeTokenCache = Collections.synchronizedMap(new HashMap<TypeToken<?>, TypeAdapter<?>>())
private

该类的文档由以下文件生成: