Class ObjectLoader

java.lang.Object
org.incenp.linkml.ext.ObjectLoader

public class ObjectLoader extends Object
Helper class to load or dump LinkML data instances from or to YAML or JSON files.

This is the runtime’s “high-level interface”. For now this is merely a thin wrapper around both Jackson’s ObjectMapper and our own ConverterContext.

This helper object holds its own LinkML context, which is reused for all calls to the various load* and dump* methods.

  • Constructor Details

    • ObjectLoader

      public ObjectLoader()
  • Method Details

    • getContext

      public ConverterContext getContext()
      Gets the LinkML context used by this loader.

      Client code might require access to the context (1) to customise it before loading anything (e.g. to add externally defined prefixes or set up custom converters), or (2) after loading something, to get all the prefixes that may have been found in instance data.

      Returns:
      The underlying LinkML context.
    • loadObject

      public <T> T loadObject(File file, Class<T> type) throws IOException, LinkMLRuntimeException
      Loads an instance of the specified type from a YAML file.
      Type Parameters:
      T - The type of objects to load.
      Parameters:
      file - The file to load the object from.
      type - The type of objects to load.
      Returns:
      The object that was loaded.
      Throws:
      IOException - If any I/O error occurs while attempting to read from the file.
      LinkMLRuntimeException - If the contents of the file do not match what is expected for an instance of the specified type.
    • loadObject

      public <T> T loadObject(File file, Class<T> type, DataFormat format) throws IOException, LinkMLRuntimeException
      Loads an instance of the specified type from a file.
      Type Parameters:
      T - The type of objects to load.
      Parameters:
      file - The file to load the object from.
      type - The type of object to load.
      format - The expected format of the file.
      Returns:
      The object that was loaded.
      Throws:
      IOException - If any I/O error occurs while attempting to read from the file.
      LinkMLRuntimeException - If the contents of the file do not match what is expected for an instance of the specified type.
    • loadObjects

      public <T> List<T> loadObjects(File file, Class<T> type) throws IOException, LinkMLRuntimeException
      Loads a list of instances of the specified type from a YAML file.
      Type Parameters:
      T - The type of objects to load.
      Parameters:
      file - The file to load the objects from.
      type - The type of objects to load.
      Returns:
      The objects that were loaded.
      Throws:
      IOException - If any I/O error occurs when attempting to read from the file.
      LinkMLRuntimeException - If the contents of the file do not match what is expected for instances of the specified type.
    • loadObjects

      public <T> List<T> loadObjects(File file, Class<T> type, DataFormat format) throws IOException, LinkMLRuntimeException
      Loads a list of instances of the specified type from a file.
      Type Parameters:
      T - The type of objects to load.
      Parameters:
      file - The file to load the objects from.
      type - The type of objects to load.
      format - The expected format of the file.
      Returns:
      The objects that were loaded.
      Throws:
      IOException - If any I/O error occurs when attempting to read from the file.
      LinkMLRuntimeException - If the contents of the file do not match what is expected for instances of the specified type.
    • dumpObject

      public <T> void dumpObject(File file, T object) throws IOException, LinkMLRuntimeException
      Dumps a LinkML object into a YAML file.
      Type Parameters:
      T - The type of the object to dump.
      Parameters:
      file - The file where to dump the object.
      object - The object to dump.
      Throws:
      IOException - If any I/O error occurs when attempting to write to the file.
      LinkMLRuntimeException - If any error occurs when serialising the object to a raw YAML tree (this should not happen if the object is a valid LinkML object in the first place).
    • dumpObject

      public <T> void dumpObject(File file, T object, DataFormat format) throws IOException, LinkMLRuntimeException
      Dumps a LinkML object into a file.
      Type Parameters:
      T - The type of the object to dump.
      Parameters:
      file - The file where to dump the object.
      object - The object to dump.
      format - The format to use to serialise the dumped object.
      Throws:
      IOException - If any I/O error occurs when attempting to write to the file.
      LinkMLRuntimeException - If any error occurs when serialising the object to a raw YAML tree (this should not happen if the object is a valid LinkML object in the first place).
    • dumpObjects

      public <T> void dumpObjects(File file, List<T> objects) throws IOException, LinkMLRuntimeException
      Dumps a list of LinkML objects into a YAML file.
      Type Parameters:
      T - The type of the objects to dump. Of note, each object may be of a different subtype of that type.
      Parameters:
      file - The file where to dump the objects.
      objects - The objects to dump.
      Throws:
      IOException - If any I/O error occurs when attempting to write to the file.
      LinkMLRuntimeException - If any error occurs when serialising the objects to a raw YAML tree (this should not happen if the object is a valid LinkML object in the first place).
    • dumpObjects

      public <T> void dumpObjects(File file, List<T> objects, DataFormat format) throws IOException, LinkMLRuntimeException
      Dumps a list of LinkML objects into a file.
      Type Parameters:
      T - The type of the objects to dump. Of note, each object may be of a different subtype of that type.
      Parameters:
      file - The file where to dump the objects.
      objects - The objects to dump.
      format - The format to use to serialise the dumped objects.
      Throws:
      IOException - If any I/O error occurs when attempting to write to the file.
      LinkMLRuntimeException - If any error occurs when serialising the objects to a raw YAML tree (this should not happen if the object is a valid LinkML object in the first place).