Package qupath.lib.io
Class GsonTools.SubTypeAdapterFactory<T>
java.lang.Object
qupath.lib.io.GsonTools.SubTypeAdapterFactory<T>
- Type Parameters:
T
-
- All Implemented Interfaces:
TypeAdapterFactory
- Enclosing class:
GsonTools
A
TypeAdapterFactory
that is suitable for handling class hierarchies.
This can be used to construct the appropriate subtype when parsing the JSON.
This is inspired and influenced by the RuntimeTypeAdapterFactory
class available as part of Gson extras,
but not the main Gson library
(https://github.com/google/gson/blob/gson-parent-2.8.6/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java),
which is Copyright (C) 2011 Google Inc. licensed under Apache License, Version 2.0.
This behavior of this class differs in several ways:
- it supports alias labels for deserialization, which can be used to help achieve backwards compatibility
- it avoids use of the internal
Streams
class for Gson, which complicates modularity - it does not support a
maintainLabel
option (the label field is always removed)
-
Method Summary
Modifier and TypeMethodDescription<R> TypeAdapter
<R> registerAlias
(Class<? extends T> subtype, String alias) Register an alias label for a specified subtype.registerSubtype
(Class<? extends T> subtype) Register a subtype using the default label (the simple name of the class).registerSubtype
(Class<? extends T> subtype, String label) Register a subtype using a custom label.
-
Method Details
-
create
- Specified by:
create
in interfaceTypeAdapterFactory
-
registerSubtype
Register a subtype using a custom label. This allows objects to serialized to JSON and deserialized while retaining the same class.- Parameters:
subtype
- the subtype to registerlabel
- the label used to identify objects of this subtype; this must be unique- Returns:
- this
GsonTools.SubTypeAdapterFactory
- See Also:
-
registerAlias
Register an alias label for a specified subtype. This can be used during deserialization for backwards compatibility, but will not be used for serializing new objects.- Parameters:
subtype
- the subtype to registeralias
- the alias used as an alternative label to identify objects of this subtype- Returns:
- this
GsonTools.SubTypeAdapterFactory
- See Also:
-
registerSubtype
Register a subtype using the default label (the simple name of the class). This allows objects to serialized to JSON and deserialized while retaining the same class.- Parameters:
subtype
- the subtype to register- Returns:
- this
GsonTools.SubTypeAdapterFactory
- See Also:
-