Package akka.javasdk

Class JsonSupport

Object
akka.javasdk.JsonSupport

public final class JsonSupport extends Object
  • Method Details

    • getObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      The Jackson ObjectMapper that is used for encoding and decoding JSON. You may adjust it's configuration, but that must only be performed before starting the service, from ServiceSetup.onStartup().
    • encodeJson

      public static <T> com.google.protobuf.Any encodeJson(T value)
      Encode 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

      public static <T> com.google.protobuf.Any encodeJson(T value, String jsonType)
      Encode 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

      public static <T> com.google.protobuf.ByteString encodeToBytes(T value) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • encodeToAkkaByteString

      public static <T> akka.util.ByteString encodeToAkkaByteString(T value) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • encodeDynamicToAkkaByteString

      public static akka.util.ByteString encodeDynamicToAkkaByteString(String key, String value) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • encodeDynamicCollectionToAkkaByteString

      public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • decodeJson

      public static <T> T decodeJson(Class<T> valueClass, com.google.protobuf.Any any)
      Decode 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

      public static <T> T parseBytes(byte[] bytes, Class<T> valueClass) throws IOException
      Throws:
      IOException
    • decodeJsonCollection

      public static <T, C extends Collection<T>> C decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, com.google.protobuf.Any any)
    • decodeJson

      public static <T> Optional<T> decodeJson(Class<T> valueClass, String jsonType, com.google.protobuf.Any any)
      Decode 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