Package com.google.gson.graph
Class GraphAdapterBuilder
- java.lang.Object
-
- com.google.gson.graph.GraphAdapterBuilder
-
public final class GraphAdapterBuilder extends java.lang.ObjectA builder for constructing a graph-aware type adapter. This class allows you to register types for which cyclic references are allowed by serializing the graph of objects as a list of named nodes. This approach ensures that objects referencing each other (or themselves) are properly serialized and deserialized.The builder maintains a mapping between types and their corresponding
InstanceCreatorinstances. When a type is registered, it will be serialized using a graph adapter that assigns a unique identifier to each object instance. During deserialization, the graph adapter first builds a mapping from these identifiers to their JSON representations and then reconstructs the object graph.Example usage:
GraphAdapterBuilder graphBuilder = new GraphAdapterBuilder(); graphBuilder.addType(MyClass.class); GsonBuilder gsonBuilder = new GsonBuilder(); graphBuilder.registerOn(gsonBuilder); Gson gson = gsonBuilder.create(); // Serialization String json = gson.toJson(myObject); // Deserialization MyClass deserialized = gson.fromJson(json, MyClass.class);
- See Also:
Gson,GsonBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classGraphAdapterBuilder.Element<T>An element of the graph during serialization or deserialization.(package private) static classGraphAdapterBuilder.FactoryA factory that creates type adapters capable of serializing and deserializing object graphs.(package private) static classGraphAdapterBuilder.Graph
-
Field Summary
Fields Modifier and Type Field Description private ConstructorConstructorconstructorConstructorprivate java.util.Map<java.lang.reflect.Type,InstanceCreator<?>>instanceCreators
-
Constructor Summary
Constructors Constructor Description GraphAdapterBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GraphAdapterBuilderaddType(java.lang.reflect.Type type)Registers the specified type with a default instance creator.GraphAdapterBuilderaddType(java.lang.reflect.Type type, InstanceCreator<?> instanceCreator)Registers the specified type with the provided instance creator.voidregisterOn(GsonBuilder gsonBuilder)Registers the graph adapter on the providedGsonBuilder.
-
-
-
Field Detail
-
instanceCreators
private final java.util.Map<java.lang.reflect.Type,InstanceCreator<?>> instanceCreators
-
constructorConstructor
private final ConstructorConstructor constructorConstructor
-
-
Method Detail
-
addType
public GraphAdapterBuilder addType(java.lang.reflect.Type type)
Registers the specified type with a default instance creator.- Parameters:
type- the type to register- Returns:
- this builder instance for chaining
-
addType
public GraphAdapterBuilder addType(java.lang.reflect.Type type, InstanceCreator<?> instanceCreator)
Registers the specified type with the provided instance creator.- Parameters:
type- the type to registerinstanceCreator- the instance creator used to create instances of the type during deserialization- Returns:
- this builder instance for chaining
-
registerOn
public void registerOn(GsonBuilder gsonBuilder)
Registers the graph adapter on the providedGsonBuilder. This method adds aTypeAdapterFactoryand registers the necessary type adapters for all types previously registered viaaddType(Type).- Parameters:
gsonBuilder- theGsonBuilderon which to register the graph adapter
-
-