Interface ValueMapper


  • public interface ValueMapper
    Automatically converts Pkl objects to Java objects. Use ValueMapperBuilder to create an instance of this type, configured according to your needs.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default <S,​T>
      Converter<S,​T>
      getConverter​(org.pkl.core.PClassInfo<S> sourceType, java.lang.Class<T> targetType)
      Same as getConverter(PClassInfo, Type), except that the target type is narrowed from Type to Class to allow for better type inference.
      <S,​T>
      Converter<S,​T>
      getConverter​(org.pkl.core.PClassInfo<S> sourceType, java.lang.reflect.Type targetType)
      Returns the converter with the given source and target types.
      default <S,​T>
      T
      map​(S model, java.lang.Class<T> targetType)
      Same as map(Object, Type), except that the target type is narrowed from Type to Class to allow for better type inference.
      <S,​T>
      T
      map​(S model, java.lang.reflect.Type targetType)
      Converts the given Pkl object to the given Java target type.
      static ValueMapper preconfigured()
      Shorthand for ValueMapperBuilder.preconfigured().build().
      ValueMapperBuilder toBuilder()
      Returns a value mapper builder that, unless further configured, will build value mappers with the same configuration as this value mapper.
    • Method Detail

      • preconfigured

        static ValueMapper preconfigured()
        Shorthand for ValueMapperBuilder.preconfigured().build().
      • map

        <S,​T> T map​(S model,
                          java.lang.reflect.Type targetType)
        Converts the given Pkl object to the given Java target type. The Pkl object can be an entire PModule or any value contained therein. See PClassInfo.forValue(T) for which Java types are used to represent Pkl objects.

        When mapping to a generic target type, a fully parameterized type needs to be passed, e.g. List<String>. Parameterized type literals can be created using Types, e.g. Types.listOf(String.class).

        If an error occurs during conversion, or if ValueMapper does not know how to convert from the given object to the given target type, a ConversionException is thrown.

      • map

        default <S,​T> T map​(S model,
                                  java.lang.Class<T> targetType)
        Same as map(Object, Type), except that the target type is narrowed from Type to Class to allow for better type inference.
      • getConverter

        <S,​T> Converter<S,​T> getConverter​(org.pkl.core.PClassInfo<S> sourceType,
                                                      java.lang.reflect.Type targetType)
        Returns the converter with the given source and target types. Throws ConversionException if no such converter exists.
      • getConverter

        default <S,​T> Converter<S,​T> getConverter​(org.pkl.core.PClassInfo<S> sourceType,
                                                              java.lang.Class<T> targetType)
        Same as getConverter(PClassInfo, Type), except that the target type is narrowed from Type to Class to allow for better type inference.
      • toBuilder

        ValueMapperBuilder toBuilder()
        Returns a value mapper builder that, unless further configured, will build value mappers with the same configuration as this value mapper. In other words, this is the inverse operation of ValueMapperBuilder.build(), except that a new builder is returned.