Package qupath.lib.io
Class GsonTools
java.lang.Object
qupath.lib.io.GsonTools
Helper class providing Gson instances with type adapters registered to serialize
several key classes.
These include:
PathObject
PathClass
ROI
ImagePlane
- Java Topology Suite Geometry objects
- Author:
- Pete Bankhead
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
ATypeAdapterFactory
that is suitable for handling class hierarchies. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> GsonTools.SubTypeAdapterFactory
<T> createSubTypeAdapterFactory
(Class<T> baseType, String typeFieldName) Create aTypeAdapterFactory
that is suitable for handling class hierarchies.static GsonBuilder
Access the builder used withgetInstance()
.static Gson
Get default Gson, capable of serializing/deserializing some key QuPath classes.static Gson
getInstance
(boolean pretty) Get default Gson, optionally with pretty printing enabled.static List
<PathObject> parseObjectsFromGeoJSON
(JsonElement element) Attempt to read PathObjects from a JsonElement, interpreting it as GeoJSON.
-
Constructor Details
-
GsonTools
public GsonTools()
-
-
Method Details
-
getDefaultBuilder
Access the builder used withgetInstance()
. This makes it possible to register new type adapters if required, which will be used by future Gson instances returned by this class.Use this with caution! Changes made here impact JSON serialization/deserialization throughout the software. Access by be removed or restricted in the future for this reason.
To create a derived builder that inherits from the default but does not change it, use
GsonBuilder.getInstance().newBuilder()
.- Returns:
-
createSubTypeAdapterFactory
public static <T> GsonTools.SubTypeAdapterFactory<T> createSubTypeAdapterFactory(Class<T> baseType, String typeFieldName) Create aTypeAdapterFactory
that is suitable for handling class hierarchies. This can be used to construct the appropriate subtype when parsing the JSON by using a specific field in the JSON representation.- Type Parameters:
T
-- Parameters:
baseType
- the base type, i.e. the class or interface that all types descend fromtypeFieldName
- a field name to include within the serialized JSON object to identify the specific type- Returns:
-
getInstance
Get default Gson, capable of serializing/deserializing some key QuPath classes.- Returns:
- See Also:
-
getInstance
Get default Gson, optionally with pretty printing enabled.- Parameters:
pretty
- if true, write using pretty-printing (i.e. more whitespace for formatting)- Returns:
- See Also:
-
parseObjectsFromGeoJSON
public static List<PathObject> parseObjectsFromGeoJSON(JsonElement element) throws JsonParseException Attempt to read PathObjects from a JsonElement, interpreting it as GeoJSON.This deals with the fact that objects can be stored in different forms: as a Feature, FeatureCollection, or array of Features.
This method can be provided with other JsonElements, and provided they do not contain characteristic GeoJSON fields (i.e. a "type" of "Feature", "FeatureCollection", or a "Geometry" subtype), they will be ignored.
- Parameters:
element
-- Returns:
- a list of objects read from the element, or an empty list if none could be found
- Throws:
JsonParseException
- Since:
- v0.5.0
-