Package akka.javasdk

Class JsonSupport

Object
akka.javasdk.JsonSupport

public final class JsonSupport extends Object
  • Method Summary

    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> akka.util.ByteString
    Encode the given value as JSON using Jackson.
    static <T> String
    encodeToString(T value)
    Encode the given value as JSON using Jackson.
    static com.fasterxml.jackson.databind.ObjectMapper
    The Jackson ObjectMapper that is used for encoding and decoding JSON for HTTP endpoints and HTTP requests.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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, from ServiceSetup.onStartup().

    • 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
    • encodeToString

      public static <T> String encodeToString(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
    • decodeJson

      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

      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