Class JsonSupport


  • public final class JsonSupport
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      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> java.util.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> 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.akkaserverless.com/[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.akkaserverless.com/[jsonType]".
      static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      The Jackson ObjectMapper that is used for encoding and decoding JSON.
      • Methods inherited from class java.lang.Object

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

    • Method Detail

      • 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 AkkaServerless
      • 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.akkaserverless.com/[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.akkaserverless.com/[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
      • 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.akkaserverless.com/".
        Parameters:
        valueClass - The type of class to deserialize the object to, the class must have the proper Jackson annotations for deserialization.
        Returns:
        The decoded object
        Throws:
        IllegalArgumentException - if the given value cannot be decoded to a T
      • decodeJson

        public static <T> java.util.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