Class ScalarConverterBase
- java.lang.Object
-
- org.incenp.linkml.core.ScalarConverterBase
-
- All Implemented Interfaces:
IConverter
- Direct Known Subclasses:
BooleanConverter,DateConverter,DatetimeConverter,DoubleConverter,EnumConverter,FloatConverter,IntegerConverter,StringConverter,TimeConverter,URIConverter
public abstract class ScalarConverterBase extends Object implements IConverter
Base class for most scalar converters.The role of this class is to have in a single place all the basic tests on the raw value that most converters will need to perform.
-
-
Constructor Summary
Constructors Constructor Description ScalarConverterBase()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Objectconvert(Object raw, ConverterContext ctx)Converts a raw object into a LinkML object.voidconvertForSlot(Object raw, Object dest, Slot slot, ConverterContext ctx)Converts a raw object into a LinkML object and assigns the result to a slot of another object.protected abstract ObjectconvertImpl(Object raw, ConverterContext ctx)Performs the actual type conversion.Objectserialise(Object object, ConverterContext ctx)Converts a LinkML object into a raw object.ObjectserialiseForSlot(Object object, Slot slot, ConverterContext ctx)Converts a LinkML object into a raw object, when the object is the value of a specific LinkML slot.protected List<Object>toList(Object raw)Checks that a raw object is a list, and casts it as such.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.incenp.linkml.core.IConverter
getType
-
-
-
-
Method Detail
-
convert
public Object convert(Object raw, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from interface:IConverterConverts a raw object into a LinkML object.Given the raw representation of a LinkML object, as it may have been obtained from a generic JSON/YAML parser, this method shall return a suitable Java representation of the object.
- Specified by:
convertin interfaceIConverter- Parameters:
raw- The raw object to convert.ctx- The global converter context.- Returns:
- The converted object.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given value.
-
convertForSlot
public void convertForSlot(Object raw, Object dest, Slot slot, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from interface:IConverterConverts a raw object into a LinkML object and assigns the result to a slot of another object.In principle, calling this method shall perform the equivalent of the following code:
slot.setValue(dest, convert(raw, ctx));
but sometimes, an object of a given type may have to be deserialised differently depending on the slot it is intended to be assigned to. Converter implementations shall then perform whatever is necessary in this method for converting an object for a given slot.
- Specified by:
convertForSlotin interfaceIConverter- Parameters:
raw- The raw object to convert.dest- The object that should received the converted value.slot- The slot of thedestobject to which the converted value should be assigned.ctx- The global converter context.- Throws:
LinkMLRuntimeException- If the converter cannot convert the given value, or cannot assign it to the target object/slot.
-
convertImpl
protected abstract Object convertImpl(Object raw, ConverterContext ctx) throws LinkMLRuntimeException
Performs the actual type conversion.- Parameters:
raw- The raw object to convert. This is guaranteed to be a non-null, non-list, non-dictionary object.- Returns:
- The converted value.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given value.
-
serialise
public Object serialise(Object object, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from interface:IConverterConverts a LinkML object into a raw object.Given the Java representation of a LinkML object, this method shall return an object suitable to be given to a generic JSON/YAML writer.
- Specified by:
serialisein interfaceIConverter- Parameters:
object- The LinkML object to convert.ctx- The global converter context.- Returns:
- The raw object that represents the original LinkML object.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given object.
-
serialiseForSlot
public Object serialiseForSlot(Object object, Slot slot, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from interface:IConverterConverts a LinkML object into a raw object, when the object is the value of a specific LinkML slot.We need such a method because the way to serialise a LinkML object into a raw object will sometimes depend on the slot to which the object belongs (the slot of which it is a value), especially with respect to inlining.
- Specified by:
serialiseForSlotin interfaceIConverter- Parameters:
object- The LinkML object to convert.slot- The slot that the given object is a value of.ctx- The global converter context.- Returns:
- The raw object that represents the original LinkML object.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given object.
-
toList
protected List<Object> toList(Object raw)
Checks that a raw object is a list, and casts it as such.If the object is not in fact a list, it will be turned into a one-element list. Having a single object where a list is expected is strictly speaking invalid, but it occurs so frequently in the wild (including within LinkML’s own meta schema!) that we don’t really have a choice here but to support that case, even though the LinkML specification considers that as a “Repair normalization” that implementations only MAY “choose” to support.
- Parameters:
raw- The raw object to cast.- Returns:
- The input object, cast into a list.
-
-