Class JsonCodec

  • All Implemented Interfaces:
    java.io.Serializable

    public class JsonCodec
    extends java.lang.Object
    implements java.io.Serializable
    Decoder for converting RPC parameters and other values from JSON in transfer between the client and the server and vice versa.
    Since:
    7.0
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  JsonCodec.BeanProperty  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object decodeCustomType​(java.lang.reflect.Type targetType, elemental.json.JsonValue value, ConnectorTracker connectorTracker)  
      static java.lang.Object decodeInternalOrCustomType​(java.lang.reflect.Type targetType, elemental.json.JsonValue value, ConnectorTracker connectorTracker)  
      static java.lang.Object decodeInternalType​(java.lang.reflect.Type targetType, boolean restrictToInternalTypes, elemental.json.JsonValue encodedJsonValue, ConnectorTracker connectorTracker)
      Decodes a value that is of an internal type.
      static EncodeResult encode​(java.lang.Object value, elemental.json.JsonValue diffState, java.lang.reflect.Type valueType, ConnectorTracker connectorTracker)  
      static java.util.Collection<JsonCodec.BeanProperty> getProperties​(java.lang.Class<?> type)  
      static boolean isInternalTransportType​(java.lang.String transportType)  
      static boolean isInternalType​(java.lang.reflect.Type type)  
      static boolean jsonEquals​(elemental.json.JsonValue a, elemental.json.JsonValue b)
      Compares two json values for deep equality.
      static <TYPE> void setCustomSerializer​(java.lang.Class<TYPE> clazz, JSONSerializer<TYPE> jsonSerializer)
      Set a custom JSONSerializer for a specific Class.
      • Methods inherited from class java.lang.Object

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

      • isInternalTransportType

        public static boolean isInternalTransportType​(java.lang.String transportType)
      • isInternalType

        public static boolean isInternalType​(java.lang.reflect.Type type)
      • decodeInternalOrCustomType

        public static java.lang.Object decodeInternalOrCustomType​(java.lang.reflect.Type targetType,
                                                                  elemental.json.JsonValue value,
                                                                  ConnectorTracker connectorTracker)
      • decodeCustomType

        public static java.lang.Object decodeCustomType​(java.lang.reflect.Type targetType,
                                                        elemental.json.JsonValue value,
                                                        ConnectorTracker connectorTracker)
      • decodeInternalType

        public static java.lang.Object decodeInternalType​(java.lang.reflect.Type targetType,
                                                          boolean restrictToInternalTypes,
                                                          elemental.json.JsonValue encodedJsonValue,
                                                          ConnectorTracker connectorTracker)
        Decodes a value that is of an internal type.

        Ensures the encoded value is of the same type as target type.

        Allows restricting collections so that they must be declared using generics. If this is used then all objects in the collection are encoded using the declared type. Otherwise only internal types are allowed in collections.

        Parameters:
        targetType - The type that should be returned by this method
        restrictToInternalTypes -
        encodedJsonValue -
        connectorTracker -
        Returns:
      • setCustomSerializer

        public static <TYPE> void setCustomSerializer​(java.lang.Class<TYPE> clazz,
                                                      JSONSerializer<TYPE> jsonSerializer)
        Set a custom JSONSerializer for a specific Class. Existence of custom serializers is checked after basic types (Strings, Booleans, Numbers, Characters), Collections and Maps, so setting custom serializers for these won't have any effect.

        To remove a previously set serializer, call this method with the second parameter set to null.

        Custom serializers should only be added from static initializers or other places that are guaranteed to run only once. Trying to add a serializer to a class that already has one will cause an exception.

        Warning: removing existing custom serializers may lead into unexpected behavior in components that expect the customized data. The framework's custom serializers are loaded in the static initializer block of this class.

        Parameters:
        clazz - The target class.
        jsonSerializer - Custom JSONSerializer to add. If null, remove custom serializer from class clazz.
        Throws:
        java.lang.IllegalArgumentException - Thrown if parameter clazz is null.
        java.lang.IllegalStateException - Thrown if serializer for parameter clazz is already registered and parameter jsonSerializer is not null.
        See Also:
        DateSerializer
      • encode

        public static EncodeResult encode​(java.lang.Object value,
                                          elemental.json.JsonValue diffState,
                                          java.lang.reflect.Type valueType,
                                          ConnectorTracker connectorTracker)
      • getProperties

        public static java.util.Collection<JsonCodec.BeanProperty> getProperties​(java.lang.Class<?> type)
                                                                          throws java.beans.IntrospectionException
        Throws:
        java.beans.IntrospectionException
      • jsonEquals

        public static boolean jsonEquals​(elemental.json.JsonValue a,
                                         elemental.json.JsonValue b)
        Compares two json values for deep equality. This is a helper for overcoming the fact that JsonValue.equals(Object) only does an identity check and JsonValue.jsEquals(JsonValue) is defined to use JavaScript semantics where arrays and objects are equals only based on identity.
        Parameters:
        a - the first json value to check, may not be null
        b - the second json value to check, may not be null
        Returns:
        true if both json values are the same; false otherwise
        Since:
        7.4