Class JacksonDatabindMapper

java.lang.Object
io.micronaut.jackson.databind.JacksonDatabindMapper
All Implemented Interfaces:
JsonMapper

@Internal @Singleton public final class JacksonDatabindMapper extends Object implements JsonMapper
jackson-databind implementation of JsonMapper.
Since:
3.1
Author:
Jonas Konrad
  • Field Details

    • PROPERTY_JSON_VIEW_ENABLED

      public static final String PROPERTY_JSON_VIEW_ENABLED
      Property used to specify whether JSON view is enabled.
      See Also:
  • Constructor Details

    • JacksonDatabindMapper

      @Internal public JacksonDatabindMapper(tools.jackson.databind.ObjectMapper objectMapper)
    • JacksonDatabindMapper

      @Inject @Internal public JacksonDatabindMapper(tools.jackson.databind.ObjectMapper objectMapper, @Value("${jackson.json-view.enabled:false}") boolean allowViews)
    • JacksonDatabindMapper

      @Internal public JacksonDatabindMapper()
  • Method Details

    • getObjectMapper

      @Internal public tools.jackson.databind.ObjectMapper getObjectMapper()
    • createSpecific

      public @NonNull JsonMapper createSpecific(@NonNull Argument<?> type)
      Description copied from interface: JsonMapper
      Specialize this mapper for the given type. Read and write operations on the returned mapper may only be called with that type.
      Specified by:
      createSpecific in interface JsonMapper
      Parameters:
      type - The type to read or write
      Returns:
      The specialized JsonMapper.
    • readValueFromTree

      public <T> T readValueFromTree(@NonNull JsonNode tree, @NonNull Argument<T> type) throws IOException
      Description copied from interface: JsonMapper
      Transform a JsonNode to a value of the given type.
      Specified by:
      readValueFromTree in interface JsonMapper
      Type Parameters:
      T - Type variable of the return type.
      Parameters:
      tree - The input json data.
      type - The type to deserialize.
      Returns:
      The deserialized value.
      Throws:
      IOException - IOException
    • writeValueToTree

      public @NonNull JsonNode writeValueToTree(@Nullable Object value) throws IOException
      Description copied from interface: JsonMapper
      Transform an object value to a json tree.
      Specified by:
      writeValueToTree in interface JsonMapper
      Parameters:
      value - The object value to transform.
      Returns:
      The json representation.
      Throws:
      IOException - If there are any mapping exceptions (e.g. illegal values).
    • writeValueToTree

      public <T> @NonNull JsonNode writeValueToTree(@NonNull Argument<T> type, T value) throws IOException
      Description copied from interface: JsonMapper
      Transform an object value to a json tree.
      Specified by:
      writeValueToTree in interface JsonMapper
      Type Parameters:
      T - The type variable of the type.
      Parameters:
      type - The object type
      value - The object value to transform.
      Returns:
      The json representation.
      Throws:
      IOException - If there are any mapping exceptions (e.g. illegal values).
    • readValue

      public <T> T readValue(@NonNull InputStream inputStream, @NonNull Argument<T> type) throws IOException
      Description copied from interface: JsonMapper
      Parse and map json from the given stream.
      Specified by:
      readValue in interface JsonMapper
      Type Parameters:
      T - Type variable of the return type.
      Parameters:
      inputStream - The input data.
      type - The type to deserialize to.
      Returns:
      The deserialized object.
      Throws:
      IOException - IOException
    • readValue

      public <T> T readValue(byte @NonNull [] byteArray, @NonNull Argument<T> type) throws IOException
      Description copied from interface: JsonMapper
      Parse and map json from the given byte array.
      Specified by:
      readValue in interface JsonMapper
      Type Parameters:
      T - Type variable of the return type.
      Parameters:
      byteArray - The input data.
      type - The type to deserialize to.
      Returns:
      The deserialized object.
      Throws:
      IOException - IOException
    • readValue

      public <T> T readValue(@NonNull ByteBuffer<?> byteBuffer, @NonNull Argument<T> type) throws IOException
      Description copied from interface: JsonMapper
      Parse and map json from the given byte buffer.
      Specified by:
      readValue in interface JsonMapper
      Type Parameters:
      T - Type variable of the return type.
      Parameters:
      byteBuffer - The input data.
      type - The type to deserialize to.
      Returns:
      The deserialized object.
      Throws:
      IOException - IOException
    • writeValue

      public void writeValue(@NonNull OutputStream outputStream, @Nullable Object object) throws IOException
      Description copied from interface: JsonMapper
      Write an object as json.
      Specified by:
      writeValue in interface JsonMapper
      Parameters:
      outputStream - The stream to write to.
      object - The object to serialize.
      Throws:
      IOException - IOException
    • writeValue

      public <T> void writeValue(@NonNull OutputStream outputStream, @NonNull Argument<T> type, T object) throws IOException
      Description copied from interface: JsonMapper
      Write an object as json.
      Specified by:
      writeValue in interface JsonMapper
      Type Parameters:
      T - The generic type
      Parameters:
      outputStream - The stream to write to.
      type - The object type
      object - The object to serialize.
      Throws:
      IOException - IOException
    • writeValueAsBytes

      public byte[] writeValueAsBytes(@Nullable Object object) throws IOException
      Description copied from interface: JsonMapper
      Write an object as json.
      Specified by:
      writeValueAsBytes in interface JsonMapper
      Parameters:
      object - The object to serialize.
      Returns:
      The serialized encoded json.
      Throws:
      IOException - IOException
    • writeValueAsBytes

      public <T> byte[] writeValueAsBytes(@NonNull Argument<T> type, T object) throws IOException
      Description copied from interface: JsonMapper
      Write an object as json.
      Specified by:
      writeValueAsBytes in interface JsonMapper
      Type Parameters:
      T - The generic type
      Parameters:
      type - The object type
      object - The object to serialize.
      Returns:
      The serialized encoded json.
      Throws:
      IOException - IOException
    • updateValueFromTree

      public void updateValueFromTree(Object value, @NonNull JsonNode tree) throws IOException
      Description copied from interface: JsonMapper
      Update an object from json data.
      Specified by:
      updateValueFromTree in interface JsonMapper
      Parameters:
      value - The object to update.
      tree - The json data to update from.
      Throws:
      IOException - If there are any mapping exceptions (e.g. illegal values).
    • cloneWithFeatures

      public @NonNull JsonMapper cloneWithFeatures(@NonNull JsonFeatures features)
      Description copied from interface: JsonMapper
      Create a copy of this mapper with the given json features as returned by JsonMapper.detectFeatures(io.micronaut.core.annotation.AnnotationMetadata).
      Specified by:
      cloneWithFeatures in interface JsonMapper
      Parameters:
      features - The json features to configure.
      Returns:
      A new mapper.
    • cloneWithViewClass

      public @NonNull JsonMapper cloneWithViewClass(@NonNull Class<?> viewClass)
      Description copied from interface: JsonMapper
      Create a copy of this mapper with the given view class.
      Specified by:
      cloneWithViewClass in interface JsonMapper
      Parameters:
      viewClass - The view class to use for serialization and deserialization.
      Returns:
      A new mapper.
    • getStreamConfig

      public @NonNull JsonStreamConfig getStreamConfig()
      Specified by:
      getStreamConfig in interface JsonMapper
      Returns:
      The configured stream config.
    • createReactiveParser

      public @NonNull Processor<byte[],JsonNode> createReactiveParser(@NonNull Consumer<Processor<byte[],JsonNode>> onSubscribe, boolean streamArray)
      Description copied from interface: JsonMapper
      Create a reactive Processor that accepts json bytes and parses them as JsonNodes.
      Specified by:
      createReactiveParser in interface JsonMapper
      Parameters:
      onSubscribe - An additional function to invoke with this processor when the returned processor is subscribed to.
      streamArray - Whether to return a top-level json array as a stream of elements rather than a single array.
      Returns:
      The reactive processor.
    • detectFeatures

      public @NonNull Optional<JsonFeatures> detectFeatures(@NonNull AnnotationMetadata annotations)
      Description copied from interface: JsonMapper
      Detect JsonFeatures from the given annotation data.
      Specified by:
      detectFeatures in interface JsonMapper
      Parameters:
      annotations - The annotations to scan.
      Returns:
      The json features for use in JsonMapper.cloneWithFeatures(io.micronaut.json.JsonFeatures), or an empty optional if there were no feature annotations detected (or feature annotations are not supported).