Package qupath.lib.io

Class GsonTools

java.lang.Object
qupath.lib.io.GsonTools

public class GsonTools extends Object
Helper class providing Gson instances with type adapters registered to serialize several key classes.

These include:

Author:
Pete Bankhead
  • Constructor Details

    • GsonTools

      public GsonTools()
  • Method Details

    • getDefaultBuilder

      public static GsonBuilder getDefaultBuilder()
      Access the builder used with getInstance(). 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 a TypeAdapterFactory 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 from
      typeFieldName - a field name to include within the serialized JSON object to identify the specific type
      Returns:
    • getInstance

      public static Gson getInstance()
      Get default Gson, capable of serializing/deserializing some key QuPath classes.
      Returns:
      See Also:
    • getInstance

      public static Gson getInstance(boolean pretty)
      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