Package kalix.javasdk

Class JsonSupport

Object
kalix.javasdk.JsonSupport

public final class JsonSupport extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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.
    static <T, C extends Collection<T>>
    C
    decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, com.google.protobuf.Any any)
     
    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.kalix.io/[valueClassName]".
    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.kalix.io/[jsonType]".
    static <T> com.google.protobuf.ByteString
    encodeToBytes(T value)
     
    static com.fasterxml.jackson.databind.ObjectMapper
    The Jackson ObjectMapper that is used for encoding and decoding JSON.
    static <T> T
    parseBytes(byte[] bytes, Class<T> valueClass)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 Kalix
    • 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.kalix.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.kalix.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
    • 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.kalix.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