Class Json


  • public final class Json
    extends Object
    A simple JSON encoder.

    This class is not listed in showcase! Should I use it?

    This class is indeed intented for internal usage only. We won't add methods here on user request. We only add methods here once we encounter non-DRY code in OmniFaces codebase. The methods may be renamed/changed without notice.

    We don't stop you from using it if you found it in the Javadoc and you think you find it useful, but you have to accept the risk that the method signatures can be changed without notice. This utility class exists because OmniFaces intends to be free of 3rd party dependencies.

    Since:
    1.2
    Author:
    Bauke Scholtz
    • Method Detail

      • encode

        public static String encode​(Object object)
        Encodes the given object as JSON. This supports the standard types Boolean, Number, CharSequence, Date and since OmniFaces 3.6 also Enum and Temporal. If the given object type does not match any of them, then it will attempt to inspect the object as a javabean whereby the public properties (with public getters) will be encoded as a JS object. It also supports Collections, Maps and arrays of them, even nested ones. The Date and Temporal are formatted in RFC 1123 format, so you can if necessary just pass it straight to new Date() in JavaScript.
        Parameters:
        object - The object to be encoded as JSON.
        Returns:
        The JSON-encoded representation of the given object.
        Throws:
        IllegalArgumentException - When the given object or one of its properties cannot be inspected as a bean.
      • encode

        public static String encode​(Object object,
                                    UnaryOperator<String> propertyNameFormatter)
        Does the same as encode(Object) but then with a custom property name formatter.
        Parameters:
        object - The object to be encoded as JSON.
        propertyNameFormatter - The property name formatter. When this is null, then the property names are not adjusted.
        Returns:
        The JSON-encoded representation of the given object.
        Throws:
        IllegalArgumentException - When the given object or one of its properties cannot be inspected as a bean.
        Since:
        3.6