Class Json


  • public final class Json
    extends Object
    A simple JSON encoder. See org.omnifaces.util.Json version 1.2 file licensed under ASL v2.0 org.omnifaces.util.Utils Copyright 2016 OmniFaces and the original author or authors.
    Author:
    Arjan Tijms, 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 and Date. 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 is 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.
      • escapeJS

        public static String escapeJS​(String string,
                                      boolean escapeSingleQuote)
        Escapes the given string according the JavaScript code rules. This escapes among others the special characters, the whitespace, the quotes and the unicode characters. Useful whenever you want to use a Java string variable as a JavaScript string variable.
        Parameters:
        string - The string to be escaped according the JavaScript code rules.
        escapeSingleQuote - Whether to escape single quotes as well or not. Set to false if you want to escape it for usage in JSON.
        Returns:
        The escaped string according the JavaScript code rules.
      • formatRFC1123

        public static String formatRFC1123​(Date date)
        Formats the given Date to a string in RFC1123 format. This format is used in HTTP headers and in JavaScript Date constructor.
        Parameters:
        date - The Date to be formatted to a string in RFC1123 format.
        Returns:
        The formatted string.
        Since:
        1.2
      • parseRFC1123

        public static Date parseRFC1123​(String string)
                                 throws ParseException
        Parses the given string in RFC1123 format to a Date object.
        Parameters:
        string - The string in RFC1123 format to be parsed to a Date object.
        Returns:
        The parsed Date.
        Throws:
        ParseException - When the given string is not in RFC1123 format.
        Since:
        1.2