Package akka.javasdk

Class JsonSupport

Object
akka.javasdk.JsonSupport

public final class JsonSupport extends Object
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    static <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 supported
    static <T> Optional<T>
    decodeJson(Class<T> valueClass, String jsonType, com.google.protobuf.Any any)
    Deprecated.
    Protobuf Any with JSON is not supported
    static <T, C extends Collection<T>>
    C
    decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, byte[] bytes)
    Deprecated.
    was only intended for internal use
    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
    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
    static akka.util.ByteString
    Deprecated.
    was only intended for internal use
    static akka.util.ByteString
    Deprecated.
    was only intended for internal use
    static <T> com.google.protobuf.Any
    encodeJson(T value)
    Deprecated.
    Protobuf Any with JSON is not supported
    static <T> com.google.protobuf.Any
    encodeJson(T value, String jsonType)
    Deprecated.
    Protobuf Any with JSON is not supported
    static <T> akka.util.ByteString
    Encode the given value as JSON using Jackson.
    static <T> com.google.protobuf.ByteString
    encodeToBytes(T value)
    Deprecated.
    Use encodeToAkkaByteString
    static 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

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details Link icon

    • getObjectMapper Link icon

      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, from ServiceSetup.onStartup().
    • encodeJson Link icon

      @Deprecated public static <T> com.google.protobuf.Any encodeJson(T value)
      Deprecated.
      Protobuf Any with JSON is not supported
      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 Link icon

      @Deprecated public static <T> com.google.protobuf.Any encodeJson(T value, String jsonType)
      Deprecated.
      Protobuf Any with JSON is not supported
      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 Link icon

      @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 Link icon

      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 Link icon

      @Deprecated public static akka.util.ByteString encodeDynamicToAkkaByteString(String key, String value)
      Deprecated.
      was only intended for internal use
    • encodeDynamicCollectionToAkkaByteString Link icon

      @Deprecated public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values)
      Deprecated.
      was only intended for internal use
    • decodeJson Link icon

      public static <T> T decodeJson(Class<T> valueClass, akka.util.ByteString bytes)
      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 Link icon

      public static <T> T decodeJson(Class<T> valueClass, byte[] bytes)
      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 Link icon

      @Deprecated public static <T> T decodeJson(Class<T> valueClass, com.google.protobuf.Any any)
      Deprecated.
      Protobuf Any with JSON is not supported
      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 Link icon

      @Deprecated public static <T> T parseBytes(byte[] bytes, Class<T> valueClass) throws IOException
      Deprecated.
      Use decodeJson
      Throws:
      IOException
    • decodeJsonCollection Link icon

      @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 Link icon

      @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 Link icon

      @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 Link icon

      @Deprecated public static <T> Optional<T> decodeJson(Class<T> valueClass, String jsonType, com.google.protobuf.Any any)
      Deprecated.
      Protobuf Any with JSON is not supported
      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