Class JsonBeanDecoder


  • public class JsonBeanDecoder
    extends JsonCodec
    Decoder for converting JSON to a Java object graph.

    The decoding is based on the expected type passed to the decode method.

    The convertion rules are as follows:

    • If the expected type is Object and the JSON input is an array, the JSON array is converted to a JsonArray.DefaultJsonArray, which is an ArrayList with element type Object and some helpful accessor methods.

    • If the expected type implements Collection a container of the expected type is created. Its element type is again Object. The JSON input must be an array.

    • If the expected type is an array type, an array of the expected type with the given element type is created. The JSON input must be an array.

    • In all cases above, the element type is passed as expected type when decoding the members of the JSON array.

    • If the expected type is an Object and the JSON input is a JSON object, the input is converted to a JsonObject.DefaultJsonObject, which is a HashMap<String,Object> with some helpful accessor methods.

    • If the expected type is neither of the above, it is assumed to be a JavaBean and the JSON input must be a JSON object. The key/value pairs of the JSON input are interpreted as properties of the JavaBean and set if the values have been parsed successfully. The type of the properties are passed as expected types when parsing the values.

      Constructors with ConstructorProperties are used if all required values are available. Else, if no setter is available for a key/value pair, an attempt is made to gain access to a private field with the name of the key and assign the value to that field. Note thatthis will fail when using Java 9 modules unless you explicitly grant the decoder access to private fields. So defining a constructor with a ConstructorProperties annotation and all immutable properties as parameters is strongly recommended.

    A JSON object can have a “class” key. It must be the first key of the object. Its value is used to instantiate the Java object in which the information of the JSON object is stored. If provided, the class specified by this key/value pair overrides the class passed as expected class. It is checked, however, that the specified class is assignable to the expected class.

    The value specified is first matched against the aliases that have been registered with the decoder (see addAlias(Class, String)). If no match is found, the converter set with setClassConverter(Function) is used to convert the name to a class. The function defaults to Class.forName(String). If the converter does not return a result, a JsonObject is used as container for the values provided by the JSON object.

    • Constructor Detail

      • JsonBeanDecoder

        public JsonBeanDecoder​(com.fasterxml.jackson.core.JsonParser parser)
    • Method Detail

      • addAlias

        public JsonBeanDecoder addAlias​(Class<?> clazz,
                                        String alias)
        Description copied from class: JsonCodec
        Add an alias for the given class.

        If defined, the alias will be used instead of the class name by the encoder.

        Specified by:
        addAlias in class JsonCodec
        Parameters:
        clazz - the class
        alias - the alias
        Returns:
        the object for easy chaining
      • setClassConverter

        public JsonBeanDecoder setClassConverter​(Function<String,​Optional<Class<?>>> converter)
        Sets the converter that maps a specified “class” to an actual Java Class.

        If it does not return a class, a HashMap is used to store the data of the JSON object.

        Parameters:
        converter - the converter to use
        Returns:
        the conversion result
      • create

        public static JsonBeanDecoder create​(Reader in)
        Create a new decoder using a default JsonParser.
        Parameters:
        in - the source
        Returns:
        the decoder
      • create

        public static JsonBeanDecoder create​(String input)
        Create a new decoder using a default parser to parse the given string.
        Parameters:
        input - the input
        Returns:
        the decoder
      • create

        public static JsonBeanDecoder create​(com.fasterxml.jackson.core.JsonParser parser)
        Create a new decoder using the given parser.
        Parameters:
        parser - the parser
        Returns:
        the decoder
      • readObject

        public <T> T readObject​(Class<T> expected)
                         throws JsonDecodeException
        Read a JSON object description into a new object of the expected type.

        The result may have a type derived from the expected type if the JSON read has a class key.

        Parameters:
        expected - the expected type
        Returns:
        the result
        Throws:
        JsonDecodeException
      • readArray

        public <T> T readArray​(Class<T> expected)
                        throws JsonDecodeException
        Read a JSON array description into a new array of the expected type.
        Type Parameters:
        T - the generic type
        Parameters:
        expected - the expected type
        Returns:
        the result
        Throws:
        JsonDecodeException