Package io.micronaut.json
Interface JsonMapper
-
public interface JsonMapper
Common abstraction for mapping json to data structures.- Since:
- 3.1
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default JsonMapper
cloneWithFeatures(JsonFeatures features)
Create a copy of this mapper with the given json features as returned bydetectFeatures(io.micronaut.core.annotation.AnnotationMetadata)
.default JsonMapper
cloneWithViewClass(java.lang.Class<?> viewClass)
Create a copy of this mapper with the given view class.org.reactivestreams.Processor<byte[],JsonNode>
createReactiveParser(java.util.function.Consumer<org.reactivestreams.Processor<byte[],JsonNode>> onSubscribe, boolean streamArray)
Create a reactiveProcessor
that accepts json bytes and parses them asJsonNode
s.default java.util.Optional<JsonFeatures>
detectFeatures(io.micronaut.core.annotation.AnnotationMetadata annotations)
DetectJsonFeatures
from the given annotation data.JsonStreamConfig
getStreamConfig()
<T> T
readValue(byte[] byteArray, io.micronaut.core.type.Argument<T> type)
Parse and map json from the given byte array.<T> T
readValue(java.io.InputStream inputStream, io.micronaut.core.type.Argument<T> type)
Parse and map json from the given stream.default <T> T
readValue(java.lang.String string, io.micronaut.core.type.Argument<T> type)
Parse and map json from the given string.<T> T
readValueFromTree(JsonNode tree, io.micronaut.core.type.Argument<T> type)
Transform aJsonNode
to a value of the given type.default <T> T
readValueFromTree(JsonNode tree, java.lang.Class<T> type)
Transform aJsonNode
to a value of the given type.default void
updateValueFromTree(java.lang.Object value, JsonNode tree)
Update an object from json data.<T> void
writeValue(java.io.OutputStream outputStream, io.micronaut.core.type.Argument<T> type, T object)
Write an object as json.void
writeValue(java.io.OutputStream outputStream, java.lang.Object object)
Write an object as json.<T> byte[]
writeValueAsBytes(io.micronaut.core.type.Argument<T> type, T object)
Write an object as json.byte[]
writeValueAsBytes(java.lang.Object object)
Write an object as json.<T> JsonNode
writeValueToTree(io.micronaut.core.type.Argument<T> type, T value)
Transform an object value to a json tree.JsonNode
writeValueToTree(java.lang.Object value)
Transform an object value to a json tree.
-
-
-
Method Detail
-
readValueFromTree
<T> T readValueFromTree(@NonNull JsonNode tree, @NonNull io.micronaut.core.type.Argument<T> type) throws java.io.IOException
Transform aJsonNode
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 aJsonNode
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 reactiveProcessor
that accepts json bytes and parses them asJsonNode
s.- 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 typevalue
- 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 typeobject
- 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 typeobject
- 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.
-
cloneWithFeatures
@NonNull default JsonMapper cloneWithFeatures(@NonNull JsonFeatures features)
Create a copy of this mapper with the given json features as returned bydetectFeatures(io.micronaut.core.annotation.AnnotationMetadata)
.- Parameters:
features
- The json features to configure.- Returns:
- A new mapper.
-
detectFeatures
@NonNull default java.util.Optional<JsonFeatures> detectFeatures(@NonNull io.micronaut.core.annotation.AnnotationMetadata annotations)
DetectJsonFeatures
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.
-
-