Package io.micronaut.json
Interface JsonMapper
- All Known Implementing Classes:
JacksonDatabindMapper
public interface JsonMapper
Common abstraction for mapping json to data structures.
- Since:
- 3.1
- Author:
- Jonas Konrad
-
Method Summary
Modifier and TypeMethodDescriptiondefault @NonNull JsonMappercloneWithFeatures(@NonNull JsonFeatures features) Create a copy of this mapper with the given json features as returned bydetectFeatures(io.micronaut.core.annotation.AnnotationMetadata).default @NonNull JsonMappercloneWithViewClass(@NonNull Class<?> viewClass) Create a copy of this mapper with the given view class.static @NonNull JsonMapperResolves the defaultJsonMapper.createReactiveParser(@NonNull Consumer<Processor<byte[], JsonNode>> onSubscribe, boolean streamArray) Deprecated.default @NonNull JsonMappercreateSpecific(@NonNull Argument<?> type) Specialize this mapper for the given type.default @NonNull Optional<JsonFeatures>detectFeatures(@NonNull AnnotationMetadata annotations) DetectJsonFeaturesfrom the given annotation data.<T> TParse and map json from the given byte array.default <T> TRead a value from the byte array for the given type.default <T> TreadValue(@NonNull ByteBuffer<?> byteBuffer, @NonNull Argument<T> type) Parse and map json from the given byte buffer.<T> TreadValue(@NonNull InputStream inputStream, @NonNull Argument<T> type) Parse and map json from the given stream.default <T> TreadValue(@NonNull InputStream inputStream, @NonNull Class<T> type) Read a value from the given input stream for the given type.default <T> TParse and map json from the given string.default <T> TRead a value from the given string for the given type.<T> TreadValueFromTree(@NonNull JsonNode tree, @NonNull Argument<T> type) Transform aJsonNodeto a value of the given type.default <T> TreadValueFromTree(@NonNull JsonNode tree, @NonNull Class<T> type) Transform aJsonNodeto a value of the given type.default voidupdateValueFromTree(Object value, @NonNull JsonNode tree) Update an object from json data.<T> voidwriteValue(@NonNull OutputStream outputStream, @NonNull Argument<T> type, T object) Write an object as json.voidwriteValue(@NonNull OutputStream outputStream, @Nullable Object object) Write an object as json.<T> byte[]writeValueAsBytes(@NonNull Argument<T> type, T object) Write an object as json.byte[]writeValueAsBytes(@Nullable Object object) Write an object as json.writeValueAsString(@NonNull Argument<T> type, T object) Write the given value as a string.writeValueAsString(@NonNull Argument<T> type, T object, Charset charset) Write the given value as a string.writeValueAsString(@NonNull Object object) Write the given value as a string.writeValueToTree(@NonNull Argument<T> type, T value) Transform an object value to a json tree.writeValueToTree(@Nullable Object value) Transform an object value to a json tree.
-
Method Details
-
createSpecific
Specialize this mapper for the given type. Read and write operations on the returned mapper may only be called with that type.- Parameters:
type- The type to read or write- Returns:
- The specialized
JsonMapper.
-
readValueFromTree
<T> T readValueFromTree(@NonNull @NonNull JsonNode tree, @NonNull @NonNull Argument<T> type) throws IOException Transform 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:
IOException- IOException
-
readValueFromTree
default <T> T readValueFromTree(@NonNull @NonNull JsonNode tree, @NonNull @NonNull Class<T> type) throws IOException Transform 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:
IOException- IOException
-
readValue
<T> T readValue(@NonNull @NonNull InputStream inputStream, @NonNull @NonNull Argument<T> type) throws 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:
IOException- IOException
-
readValue
@Nullable default <T> T readValue(@NonNull @NonNull InputStream inputStream, @NonNull @NonNull Class<T> type) throws IOException Read a value from the given input stream for the given type.- Type Parameters:
T- The generic type- Parameters:
inputStream- The input streamtype- The type- Returns:
- The value or
nullif it decodes to null - Throws:
IOException- If an unrecoverable error occurs
-
readValue
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:
IOException- IOException
-
readValue
default <T> T readValue(@NonNull @NonNull ByteBuffer<?> byteBuffer, @NonNull @NonNull Argument<T> type) throws IOException Parse and map json from the given byte buffer.- 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
-
readValue
default <T> T readValue(@NonNull @NonNull String string, @NonNull @NonNull Argument<T> type) throws 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:
IOException- IOException
-
readValue
@Nullable default <T> T readValue(byte @NonNull [] byteArray, @NonNull @NonNull Class<T> type) throws IOException Read a value from the byte array for the given type.- Type Parameters:
T- The generic type- Parameters:
byteArray- The byte arraytype- The type- Returns:
- The value or
nullif it decodes to null - Throws:
IOException- If an unrecoverable error occurs- Since:
- 4.0.0
-
readValue
@Nullable default <T> T readValue(@NonNull @NonNull String string, @NonNull @NonNull Class<T> type) throws IOException Read a value from the given string for the given type.- Type Parameters:
T- The generic type- Parameters:
string- The stringtype- The type- Returns:
- The value or
nullif it decodes to null - Throws:
IOException- If an unrecoverable error occurs
-
createReactiveParser
@NonNull @Deprecated default @NonNull Processor<byte[],JsonNode> createReactiveParser(@NonNull @NonNull Consumer<Processor<byte[], JsonNode>> onSubscribe, boolean streamArray) Deprecated.- 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
Transform an object value to a json tree.- Parameters:
value- The object value to transform.- Returns:
- The json representation.
- Throws:
IOException- If there are any mapping exceptions (e.g. illegal values).
-
writeValueToTree
@NonNull <T> @NonNull JsonNode writeValueToTree(@NonNull @NonNull Argument<T> type, @Nullable T value) throws 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:
IOException- If there are any mapping exceptions (e.g. illegal values).
-
writeValue
void writeValue(@NonNull @NonNull OutputStream outputStream, @Nullable @Nullable Object object) throws IOException Write an object as json.- Parameters:
outputStream- The stream to write to.object- The object to serialize.- Throws:
IOException- IOException
-
writeValue
<T> void writeValue(@NonNull @NonNull OutputStream outputStream, @NonNull @NonNull Argument<T> type, @Nullable T object) throws 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:
IOException- IOException
-
writeValueAsBytes
Write an object as json.- Parameters:
object- The object to serialize.- Returns:
- The serialized encoded json.
- Throws:
IOException- IOException
-
writeValueAsBytes
<T> byte[] writeValueAsBytes(@NonNull @NonNull Argument<T> type, @Nullable T object) throws IOException Write an object as json.- Type Parameters:
T- The generic type- Parameters:
type- The object typeobject- The object to serialize.- Returns:
- The serialized encoded json.
- Throws:
IOException- IOException
-
writeValueAsString
@NonNull default @NonNull String writeValueAsString(@NonNull @NonNull Object object) throws IOException Write the given value as a string.- Parameters:
object- The object- Returns:
- The string
- Throws:
IOException- If an unrecoverable error occurs- Since:
- 4.0.0
-
writeValueAsString
@NonNull default <T> @NonNull String writeValueAsString(@NonNull @NonNull Argument<T> type, @Nullable T object) throws IOException Write the given value as a string.- Type Parameters:
T- The generic type- Parameters:
type- The type, nevernullobject- The object- Returns:
- The string
- Throws:
IOException- If an unrecoverable error occurs- Since:
- 4.0.0
-
writeValueAsString
@NonNull default <T> @NonNull String writeValueAsString(@NonNull @NonNull Argument<T> type, @Nullable T object, Charset charset) throws IOException Write the given value as a string.- Type Parameters:
T- The generic type- Parameters:
type- The type, nevernullobject- The objectcharset- The charset, nevernull- Returns:
- The string
- Throws:
IOException- If an unrecoverable error occurs- Since:
- 4.0.0
-
updateValueFromTree
Update an object from json data.- 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).UnsupportedOperationException- If this operation is not supported.
-
cloneWithFeatures
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 @NonNull Optional<JsonFeatures> detectFeatures(@NonNull @NonNull 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
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:
UnsupportedOperationException- If views are not supported by this mapper.
-
getStreamConfig
- Returns:
- The configured stream config.
-
createDefault
Resolves the defaultJsonMapper.- Returns:
- The default
JsonMapper - Throws:
IllegalStateException- If noJsonMapperimplementation exists on the classpath.- Since:
- 4.0.0
-