Package io.micronaut.json
Interface JsonMapper
-
public interface JsonMapperCommon 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 JsonMappercloneWithFeatures(JsonFeatures features)Create a copy of this mapper with the given json features as returned bydetectFeatures(io.micronaut.core.annotation.AnnotationMetadata).default JsonMappercloneWithViewClass(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 reactiveProcessorthat accepts json bytes and parses them asJsonNodes.default java.util.Optional<JsonFeatures>detectFeatures(io.micronaut.core.annotation.AnnotationMetadata annotations)DetectJsonFeaturesfrom the given annotation data.JsonStreamConfiggetStreamConfig()<T> TreadValue(byte[] byteArray, io.micronaut.core.type.Argument<T> type)Parse and map json from the given byte array.<T> TreadValue(java.io.InputStream inputStream, io.micronaut.core.type.Argument<T> type)Parse and map json from the given stream.default <T> TreadValue(java.lang.String string, io.micronaut.core.type.Argument<T> type)Parse and map json from the given string.<T> TreadValueFromTree(JsonNode tree, io.micronaut.core.type.Argument<T> type)Transform aJsonNodeto a value of the given type.default <T> TreadValueFromTree(JsonNode tree, java.lang.Class<T> type)Transform aJsonNodeto a value of the given type.default voidupdateValueFromTree(java.lang.Object value, JsonNode tree)Update an object from json data.<T> voidwriteValue(java.io.OutputStream outputStream, io.micronaut.core.type.Argument<T> type, T object)Write an object as json.voidwriteValue(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> JsonNodewriteValueToTree(io.micronaut.core.type.Argument<T> type, T value)Transform an object value to a json tree.JsonNodewriteValueToTree(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.IOExceptionTransform aJsonNodeto 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
-
readValueFromTree
default <T> T readValueFromTree(@NonNull JsonNode tree, @NonNull java.lang.Class<T> type) throws java.io.IOExceptionTransform aJsonNodeto 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
-
readValue
<T> T readValue(@NonNull java.io.InputStream inputStream, @NonNull io.micronaut.core.type.Argument<T> type) throws java.io.IOExceptionParse 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
-
readValue
<T> T readValue(@NonNull byte[] byteArray, @NonNull io.micronaut.core.type.Argument<T> type) throws java.io.IOExceptionParse 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
-
readValue
default <T> T readValue(@NonNull java.lang.String string, @NonNull io.micronaut.core.type.Argument<T> type) throws java.io.IOExceptionParse 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
-
createReactiveParser
@NonNull org.reactivestreams.Processor<byte[],JsonNode> createReactiveParser(@NonNull java.util.function.Consumer<org.reactivestreams.Processor<byte[],JsonNode>> onSubscribe, boolean streamArray)
Create a reactiveProcessorthat accepts json bytes and parses them asJsonNodes.- 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.IOExceptionWrite an object as json.- Parameters:
outputStream- The stream to write to.object- The object to serialize.- Throws:
java.io.IOException
-
writeValue
<T> void writeValue(@NonNull java.io.OutputStream outputStream, @NonNull io.micronaut.core.type.Argument<T> type, @Nullable T object) throws java.io.IOExceptionWrite 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
-
writeValueAsBytes
byte[] writeValueAsBytes(@Nullable java.lang.Object object) throws java.io.IOExceptionWrite an object as json.- Parameters:
object- The object to serialize.- Returns:
- The serialized encoded json.
- Throws:
java.io.IOException
-
writeValueAsBytes
<T> byte[] writeValueAsBytes(@NonNull io.micronaut.core.type.Argument<T> type, @Nullable T object) throws java.io.IOExceptionWrite 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
-
updateValueFromTree
default void updateValueFromTree(java.lang.Object value, @NonNull JsonNode tree) throws java.io.IOExceptionUpdate 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)
DetectJsonFeaturesfrom 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.
-
-