Class ObjectLoader
java.lang.Object
org.incenp.linkml.ext.ObjectLoader
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> voiddumpObject(File file, T object) Dumps a LinkML object into a YAML file.<T> voiddumpObject(File file, T object, DataFormat format) Dumps a LinkML object into a file.<T> voiddumpObjects(File file, List<T> objects) Dumps a list of LinkML objects into a YAML file.<T> voiddumpObjects(File file, List<T> objects, DataFormat format) Dumps a list of LinkML objects into a file.Gets the LinkML context used by this loader.<T> TloadObject(File file, Class<T> type) Loads an instance of the specified type from a YAML file.<T> TloadObject(File file, Class<T> type, DataFormat format) Loads an instance of the specified type from a file.<T> List<T> loadObjects(File file, Class<T> type) Loads a list of instances of the specified type from a YAML file.<T> List<T> loadObjects(File file, Class<T> type, DataFormat format) Loads a list of instances of the specified type from a file.
-
Constructor Details
-
ObjectLoader
public ObjectLoader()
-
-
Method Details
-
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
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
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
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
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).
-