Interface JsonMapper


  • public interface JsonMapper
    Common abstraction for mapping json to data structures.
    Since:
    3.1
    • Method Detail

      • readValueFromTree

        <T> T readValueFromTree​(@NonNull
                                JsonNode tree,
                                @NonNull
                                io.micronaut.core.type.Argument<T> type)
                         throws java.io.IOException
        Transform a JsonNode to a value of the given type.
        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:
        java.io.IOException - IOException
      • readValueFromTree

        default <T> T readValueFromTree​(@NonNull
                                        JsonNode tree,
                                        @NonNull
                                        java.lang.Class<T> type)
                                 throws java.io.IOException
        Transform a JsonNode to a value of the given type.
        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:
        java.io.IOException - IOException
      • readValue

        <T> T readValue​(@NonNull
                        java.io.InputStream inputStream,
                        @NonNull
                        io.micronaut.core.type.Argument<T> type)
                 throws java.io.IOException
        Parse and map json from the given stream.
        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:
        java.io.IOException - IOException
      • readValue

        <T> T readValue​(@NonNull
                        byte[] byteArray,
                        @NonNull
                        io.micronaut.core.type.Argument<T> type)
                 throws java.io.IOException
        Parse and map json from the given byte array.
        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:
        java.io.IOException - IOException
      • readValue

        default <T> T readValue​(@NonNull
                                java.lang.String string,
                                @NonNull
                                io.micronaut.core.type.Argument<T> type)
                         throws java.io.IOException
        Parse and map json from the given string.
        Type Parameters:
        T - Type variable of the return type.
        Parameters:
        string - The input data.
        type - The type to deserialize to.
        Returns:
        The deserialized object.
        Throws:
        java.io.IOException - IOException
      • createReactiveParser

        @NonNull
        org.reactivestreams.Processor<byte[],​JsonNode> createReactiveParser​(@NonNull
                                                                                  java.util.function.Consumer<org.reactivestreams.Processor<byte[],​JsonNode>> onSubscribe,
                                                                                  boolean streamArray)
        Create a reactive Processor that accepts json bytes and parses them as JsonNodes.
        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.
      • writeValueToTree

        @NonNull
        JsonNode writeValueToTree​(@Nullable
                                  java.lang.Object value)
                           throws java.io.IOException
        Transform an object value to a json tree.
        Parameters:
        value - The object value to transform.
        Returns:
        The json representation.
        Throws:
        java.io.IOException - If there are any mapping exceptions (e.g. illegal values).
      • writeValueToTree

        @NonNull
        <T> JsonNode writeValueToTree​(@NonNull
                                      io.micronaut.core.type.Argument<T> type,
                                      @Nullable
                                      T value)
                               throws java.io.IOException
        Transform an object value to a json tree.
        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:
        java.io.IOException - If there are any mapping exceptions (e.g. illegal values).
      • writeValue

        void writeValue​(@NonNull
                        java.io.OutputStream outputStream,
                        @Nullable
                        java.lang.Object object)
                 throws java.io.IOException
        Write an object as json.
        Parameters:
        outputStream - The stream to write to.
        object - The object to serialize.
        Throws:
        java.io.IOException - IOException
      • writeValue

        <T> void writeValue​(@NonNull
                            java.io.OutputStream outputStream,
                            @NonNull
                            io.micronaut.core.type.Argument<T> type,
                            @Nullable
                            T object)
                     throws java.io.IOException
        Write an object as json.
        Type Parameters:
        T - The generic type
        Parameters:
        outputStream - The stream to write to.
        type - The object type
        object - The object to serialize.
        Throws:
        java.io.IOException - IOException
      • writeValueAsBytes

        byte[] writeValueAsBytes​(@Nullable
                                 java.lang.Object object)
                          throws java.io.IOException
        Write an object as json.
        Parameters:
        object - The object to serialize.
        Returns:
        The serialized encoded json.
        Throws:
        java.io.IOException - IOException
      • writeValueAsBytes

        <T> byte[] writeValueAsBytes​(@NonNull
                                     io.micronaut.core.type.Argument<T> type,
                                     @Nullable
                                     T object)
                              throws java.io.IOException
        Write an object as json.
        Type Parameters:
        T - The generidc type
        Parameters:
        type - The object type
        object - The object to serialize.
        Returns:
        The serialized encoded json.
        Throws:
        java.io.IOException - IOException
      • updateValueFromTree

        default void updateValueFromTree​(java.lang.Object value,
                                         @NonNull
                                         JsonNode tree)
                                  throws java.io.IOException
        Update an object from json data.
        Parameters:
        value - The object to update.
        tree - The json data to update from.
        Throws:
        java.io.IOException - If there are any mapping exceptions (e.g. illegal values).
        java.lang.UnsupportedOperationException - If this operation is not supported.
      • detectFeatures

        @NonNull
        default java.util.Optional<JsonFeatures> detectFeatures​(@NonNull
                                                                io.micronaut.core.annotation.AnnotationMetadata annotations)
        Detect JsonFeatures from the given annotation data.
        Parameters:
        annotations - The annotations to scan.
        Returns:
        The json features for use in cloneWithFeatures(io.micronaut.json.JsonFeatures), or an empty optional if there were no feature annotations detected (or feature annotations are not supported).
      • cloneWithViewClass

        @NonNull
        default JsonMapper cloneWithViewClass​(@NonNull
                                              java.lang.Class<?> viewClass)
        Create a copy of this mapper with the given view class.
        Parameters:
        viewClass - The view class to use for serialization and deserialization.
        Returns:
        A new mapper.
        Throws:
        java.lang.UnsupportedOperationException - If views are not supported by this mapper.
      • getStreamConfig

        @NonNull
        JsonStreamConfig getStreamConfig()
        Returns:
        The configured stream config.