Interface DataFormat

All Known Implementing Classes:
DomXmlDataFormat, JacksonJsonDataFormat, SerializableDataFormat

public interface DataFormat
Maps a java object to the data format's internal representation and vice versa.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canMap(Object value)
    Returns true if this data format can map the provided Java Object.
    Returns a data-format-specific canonical type name.
    Returns the data format name.
    <T> T
    readValue(String value, Class<T> cls)
    Reads the internal representation of a data format to a java object of the desired class.
    <T> T
    readValue(String value, String typeIdentifier)
    Reads the internal representation of a data format to a java object of the desired class.
    default <T> T
    readValue(String value, String typeIdentifier, DeserializationTypeValidator validator)
    Reads the internal representation of a data format to a java object of the desired class, validating the resolved Java type before it is used for deserialization.
    Writes a java object to a data format's internal data representation.
  • Method Details

    • getName

      String getName()
      Returns the data format name.
    • canMap

      boolean canMap(Object value)
      Returns true if this data format can map the provided Java Object.
      Parameters:
      parameter - the java object to check
      Returns:
      true if this object can be mapped.
    • writeValue

      String writeValue(Object value)
      Writes a java object to a data format's internal data representation.
      Parameters:
      value - object that is written into internal data representation
      Returns:
      the data format's internal representation of that object
    • readValue

      <T> T readValue(String value, String typeIdentifier)
      Reads the internal representation of a data format to a java object of the desired class.
      Parameters:
      value - the object to be read
      typeIdentifier - the class to map the object to
      Returns:
      a java object of the specified class that was populated with the input parameter
    • readValue

      default <T> T readValue(String value, String typeIdentifier, DeserializationTypeValidator validator)
      Reads the internal representation of a data format to a java object of the desired class, validating the resolved Java type before it is used for deserialization.

      When validator is null (the default, backward-compatible case), the default implementation delegates to readValue(String, String) unchanged, preserving the behavior of existing DataFormat implementations that do not override this method. Implementations that resolve typeIdentifier to one or more concrete Java classes (e.g. via class loading or generic type construction) should override this method and invoke DeserializationTypeValidator.validate(String) for every resolved class before it is used, so that null is returned/an exception thrown instead of instantiating a disallowed type.

      When validator is non-null but this method is not overridden, the default implementation fails closed: it throws a ValueMapperException instead of silently deserializing without validation. A worker that has opted into DeserializationTypeValidator must be able to rely on it actually being enforced; silently ignoring it would defeat its purpose. Implementations that cannot honor the validator at all (e.g. because they do not use typeIdentifier for class selection in the first place, such as native Java serialization) should override this method themselves and throw their own, more specific exception explaining why - see SerializableDataFormat for an example.

      Parameters:
      value - the object to be read
      typeIdentifier - the class to map the object to
      validator - validates the class(es) that typeIdentifier resolves to before they are used for deserialization; may be null, in which case no validation is performed
      Returns:
      a java object of the specified class that was populated with the input parameter
      Throws:
      ValueMapperException - if validator is non-null and this method is not overridden
    • readValue

      <T> T readValue(String value, Class<T> cls)
      Reads the internal representation of a data format to a java object of the desired class.
      Parameters:
      value - the object to be read
      cls - a data-format-specific type identifier that describes the class to map to
      Returns:
      a java object of the specified class that was populated with the input parameter
    • getCanonicalTypeName

      String getCanonicalTypeName(Object value)
      Returns a data-format-specific canonical type name.