Class DefaultInstanceBuilder

java.lang.Object
cz.cvut.kbss.jsonld.deserialization.DefaultInstanceBuilder
All Implemented Interfaces:
InstanceBuilder

public class DefaultInstanceBuilder extends Object implements InstanceBuilder
Default implementation of the JSON-LD deserializer, which takes values parsed from a JSON-LD document and builds Java instances from them.
  • Constructor Details

  • Method Details

    • openObject

      public void openObject(String id, String property, List<String> types)
      Description copied from interface: InstanceBuilder
      Creates new instance to fill filled mapped by the specified property.

      The instance type is determined from the declared type of the mapped field, which is taken from the currently open object, and from the specified types. Therefore, another object has to be already open before this method can be called.

      The new instance also becomes the currently open object.

      This method should also verify cardinality, i.e. multiple objects cannot be set for the same property, if the field it maps to is singular.

      This method assumes that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Specified by:
      openObject in interface InstanceBuilder
      Parameters:
      id - Identifier of the object being open
      property - Property identifier (IRI)
      types - Types of the object being open
    • openObject

      public <T> void openObject(String id, Class<T> cls)
      Description copied from interface: InstanceBuilder
      Creates new instance of the specified class.

      If there is a collection currently open, it adds the new instance to it.

      The new instance also becomes the currently open object.

      This method is intended for creating top level objects or adding objects to collections. Use InstanceBuilder.openObject(String, String, List) for opening objects as values of attributes.

      Specified by:
      openObject in interface InstanceBuilder
      Type Parameters:
      T - The type of the object to open
      Parameters:
      id - Identifier of the object being open
      cls - Java type of the object being open
      See Also:
    • closeObject

      public void closeObject()
      Description copied from interface: InstanceBuilder
      Closes the most recently open object.
      Specified by:
      closeObject in interface InstanceBuilder
    • openCollection

      public void openCollection(String property)
      Description copied from interface: InstanceBuilder
      Creates new instance of appropriate collection and sets it as value of the specified property of the currently open object.

      The collection type is determined from the declared type of the mapped field, which is taken from the currently open object. Therefore, another object has to be already open before this method can be called.

      The new collection also becomes the currently open object.

      This method should also verify cardinality, i.e. a collection cannot be set as value of a field mapped by property, if the field is singular.

      This method assumes that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Specified by:
      openCollection in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
    • openCollection

      public void openCollection(CollectionType collectionType)
      Description copied from interface: InstanceBuilder
      Creates new instance of the specified collection type.

      If there is a collection currently open, it adds the new collection as its new item.

      The new collection also becomes the currently open object.

      This method is intended for creating top level collections or nesting collections. Use InstanceBuilder.openCollection(String) for opening collections as values of attributes.

      Specified by:
      openCollection in interface InstanceBuilder
      Parameters:
      collectionType - Type of the JSON collection to instantiate in Java
      See Also:
    • closeCollection

      public void closeCollection()
      Description copied from interface: InstanceBuilder
      Closes the most recently open collection.
      Specified by:
      closeCollection in interface InstanceBuilder
    • addValue

      public void addValue(String property, Object value)
      Description copied from interface: InstanceBuilder
      Adds the specified value of the specified property to the currently open object.

      This method is intended for non-composite JSON values like String, Number Boolean and null. It can also handle IRIs of objects already parsed by the deserializer, which are serialized as Strings in JSON-LD. In this case, the field is filled with the deserialized object.

      This method should also verify cardinality and correct typing, e.g. multiple values cannot be set for the same property, if the field it maps to is singular.

      This method assumes that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Specified by:
      addValue in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
      value - The value to set
    • addValue

      public void addValue(Object value)
      Description copied from interface: InstanceBuilder
      Adds the specified value to the currently open collection.

      This method is intended for non-composite JSON values like String, Number Boolean and null. It can also handle IRIs of objects already parsed by the deserializer, which are serialized as Strings in JSON-LD. In this case, the deserialized object is added to the collection.

      Specified by:
      addValue in interface InstanceBuilder
      Parameters:
      value - The value to add
    • addNodeReference

      public void addNodeReference(String property, String nodeId)
      Description copied from interface: InstanceBuilder
      Adds a reference to a node to the currently open object.

      This methods is invoked in case the deserializer encounters an object with a single attribute - @id. This assumes that the node either references an already encountered object (known instance) or that the node is a value of a plain identifier-valued object property.

      This method should also verify cardinality and correct typing, e.g. multiple values cannot be set for the same property, if the field it maps to is singular.

      It is assumed that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Specified by:
      addNodeReference in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
      nodeId - Identifier (IRI) of the node, i.e. value of the @id attribute
    • addNodeReference

      public void addNodeReference(String nodeId)
      Description copied from interface: InstanceBuilder
      Adds the specified value to the currently open collection.

      This methods is invoked in case the deserializer encounters an object with a single attribute - @id. This assumes that the node either references an already encountered object (known instance) or that the node is a value of a plain identifier-valued object property.

      Specified by:
      addNodeReference in interface InstanceBuilder
      Parameters:
      nodeId - Identifier (IRI) of the node, i.e. value of the @id attribute
    • getCurrentRoot

      public Object getCurrentRoot()
      Description copied from interface: InstanceBuilder
      Returns current root of the deserialized object graph.
      Specified by:
      getCurrentRoot in interface InstanceBuilder
      Returns:
      Object graph root, it can be a OWLClass instance, or a Collection
    • getCurrentCollectionElementType

      public Class<?> getCurrentCollectionElementType()
      Description copied from interface: InstanceBuilder
      Returns the declared type of elements of the current instance, if it is a collection.
      Specified by:
      getCurrentCollectionElementType in interface InstanceBuilder
      Returns:
      Collection element type
    • isPlural

      public boolean isPlural(String property)
      Description copied from interface: InstanceBuilder
      Checks whether the specified property is mapped to a plural field.

      This method assumes that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Note that Types and Properties fields are always treated as plural.

      Specified by:
      isPlural in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
      Returns:
      Whether mapped field is collection-valued or not
    • isPropertyMapped

      public boolean isPropertyMapped(String property)
      Description copied from interface: InstanceBuilder
      Checks whether the specified property is mapped by the class representing the current instance context.

      Returns true also for the JsonLd.TYPE property, even though the target instance may not contain a Types field. The builder has to be able to handle types no matter whether a types field is present or not.

      Specified by:
      isPropertyMapped in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
      Returns:
      Whether the property is mapped in the current instance context
    • isPropertyDeserializable

      public boolean isPropertyDeserializable(String property)
      Description copied from interface: InstanceBuilder
      Checks whether the specified property can be deserialized.

      A property cannot be deserialized if it is not mapped or if the mapped field's access is read-only.

      Specified by:
      isPropertyDeserializable in interface InstanceBuilder
      Parameters:
      property - Property identifier (IRI)
      Returns:
      Whether property can be deserialized
      See Also:
    • getCurrentContextType

      public Class<?> getCurrentContextType()
      Description copied from interface: InstanceBuilder
      Gets the Java type of the current object context.
      Specified by:
      getCurrentContextType in interface InstanceBuilder
      Returns:
      Java class of the instance currently being built
    • isCurrentCollectionProperties

      public boolean isCurrentCollectionProperties()
      Description copied from interface: InstanceBuilder
      Checks whether the current collection context represents a Properties attribute.
      Specified by:
      isCurrentCollectionProperties in interface InstanceBuilder
      Returns:
      true if the current context is a collection representing a Properties field
    • getTargetType

      public Class<?> getTargetType(String property)
      Description copied from interface: InstanceBuilder
      Gets the target type of the specified property.

      That is, it retrieves the type of the field to which the specified property is mapped. If the target field is a collection, the element type is returned (equivalent to InstanceBuilder.getCurrentCollectionElementType()).

      This method assumes that the property is mapped, i.e. that InstanceBuilder.isPropertyMapped(String) returned true.

      Specified by:
      getTargetType in interface InstanceBuilder
      Parameters:
      property - Property whose target type to resolve
      Returns:
      Target type wrapped in an optional