Package akka.javasdk
Class JsonSupport
Object
akka.javasdk.JsonSupport
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
decodeJson
(Class<T> valueClass, byte[] bytes) Decode the given bytes to an instance of T using Jackson.static <T> T
decodeJson
(Class<T> valueClass, akka.util.ByteString bytes) Decode the given bytes to an instance of T using Jackson.static <T> T
decodeJson
(Class<T> valueClass, com.google.protobuf.Any any) Deprecated.Protobuf Any with JSON is not supportedstatic <T> Optional
<T> decodeJson
(Class<T> valueClass, String jsonType, com.google.protobuf.Any any) Deprecated.Protobuf Any with JSON is not supportedstatic <T,
C extends Collection<T>>
CdecodeJsonCollection
(Class<T> valueClass, Class<C> collectionType, byte[] bytes) Deprecated.was only intended for internal usestatic <T,
C extends Collection<T>>
CdecodeJsonCollection
(Class<T> valueClass, Class<C> collectionType, akka.util.ByteString bytes) Deprecated.was only intended for internal usestatic <T,
C extends Collection<T>>
CdecodeJsonCollection
(Class<T> valueClass, Class<C> collectionType, com.google.protobuf.Any any) Deprecated.Protobuf Any with JSON is not supportedstatic akka.util.ByteString
encodeDynamicCollectionToAkkaByteString
(String key, Collection<?> values) Deprecated.was only intended for internal usestatic akka.util.ByteString
encodeDynamicToAkkaByteString
(String key, String value) Deprecated.was only intended for internal usestatic <T> com.google.protobuf.Any
encodeJson
(T value) Deprecated.Protobuf Any with JSON is not supportedstatic <T> com.google.protobuf.Any
encodeJson
(T value, String jsonType) Deprecated.Protobuf Any with JSON is not supportedstatic <T> akka.util.ByteString
encodeToAkkaByteString
(T value) Encode the given value as JSON using Jackson.static <T> com.google.protobuf.ByteString
encodeToBytes
(T value) Deprecated.Use encodeToAkkaByteStringstatic com.fasterxml.jackson.databind.ObjectMapper
The Jackson ObjectMapper that is used for encoding and decoding JSON for HTTP endpoints and HTTP requests.static <T> T
parseBytes
(byte[] bytes, Class<T> valueClass) Deprecated.Use decodeJson
-
Method Details
-
getObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()The Jackson ObjectMapper that is used for encoding and decoding JSON for HTTP endpoints and HTTP requests. You may adjust its configuration, but that must only be performed before starting the service, fromServiceSetup.onStartup()
. -
encodeJson
Deprecated.Protobuf Any with JSON is not supportedEncode the given value as JSON using Jackson and put the encoded string as bytes in a protobuf Any with the type URL"json.akka.io/[valueClassName]"
.Note that if the serialized Any is published to a pub/sub topic that is consumed by an external service using the class name suffix this introduces coupling as the internal class name of this service becomes known to the outside of the service (and for exampe renaming it may break existing consumers). For such cases consider using the overload with an explicit name for the JSON type instead.
-
encodeJson
Deprecated.Protobuf Any with JSON is not supportedEncode the given value as JSON using Jackson and put the encoded string as bytes in a protobuf Any with the type URL"json.akka.io/[jsonType]"
.- Parameters:
value
- the object to encode as JSON, must be an instance of a class properly annotated with the needed Jackson annotations.jsonType
- A discriminator making it possible to identify which type of object is in the JSON, useful for example when multiple different objects are passed through a pub/sub topic.- Throws:
IllegalArgumentException
- if the given value cannot be turned into JSON
-
encodeToBytes
@Deprecated public static <T> com.google.protobuf.ByteString encodeToBytes(T value) throws com.fasterxml.jackson.core.JsonProcessingException Deprecated.Use encodeToAkkaByteString- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
encodeToAkkaByteString
public static <T> akka.util.ByteString encodeToAkkaByteString(T value) Encode the given value as JSON using Jackson.- Parameters:
value
- the object to encode as JSON, must be an instance of a class properly annotated with the needed Jackson annotations.- Throws:
IllegalArgumentException
- if the given value cannot be turned into JSON
-
encodeDynamicToAkkaByteString
@Deprecated public static akka.util.ByteString encodeDynamicToAkkaByteString(String key, String value) Deprecated.was only intended for internal use -
encodeDynamicCollectionToAkkaByteString
@Deprecated public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values) Deprecated.was only intended for internal use -
decodeJson
Decode the given bytes to an instance of T using Jackson. The bytes must be the JSON string as bytes.- Parameters:
valueClass
- The type of class to deserialize the object to, the class must have the proper Jackson annotations for deserialization.bytes
- The bytes to deserialize.- Returns:
- The decoded object
- Throws:
IllegalArgumentException
- if the given value cannot be decoded to a T
-
decodeJson
Decode the given bytes to an instance of T using Jackson. The bytes must be the JSON string as bytes.- Parameters:
valueClass
- The type of class to deserialize the object to, the class must have the proper Jackson annotations for deserialization.bytes
- The bytes to deserialize.- Returns:
- The decoded object
- Throws:
IllegalArgumentException
- if the given value cannot be decoded to a T
-
decodeJson
Deprecated.Protobuf Any with JSON is not supportedDecode the given protobuf Any object to an instance of T using Jackson. The object must have the JSON string as bytes as value and a type URL starting with "json.akka.io/".- Parameters:
valueClass
- The type of class to deserialize the object to, the class must have the proper Jackson annotations for deserialization.any
- The protobuf Any object to deserialize.- Returns:
- The decoded object
- Throws:
IllegalArgumentException
- if the given value cannot be decoded to a T
-
parseBytes
Deprecated.Use decodeJson- Throws:
IOException
-
decodeJsonCollection
@Deprecated public static <T,C extends Collection<T>> C decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, com.google.protobuf.Any any) Deprecated.Protobuf Any with JSON is not supported -
decodeJsonCollection
@Deprecated public static <T,C extends Collection<T>> C decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, akka.util.ByteString bytes) Deprecated.was only intended for internal use -
decodeJsonCollection
@Deprecated public static <T,C extends Collection<T>> C decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, byte[] bytes) Deprecated.was only intended for internal use -
decodeJson
@Deprecated public static <T> Optional<T> decodeJson(Class<T> valueClass, String jsonType, com.google.protobuf.Any any) Deprecated.Protobuf Any with JSON is not supportedDecode the given protobuf Any to an instance of T using Jackson but only if the suffix of the type URL matches the given jsonType.- Returns:
- An Optional containing the successfully decoded value or an empty Optional if the type suffix does not match.
- Throws:
IllegalArgumentException
- if the suffix matches but the Any cannot be parsed into a T
-