Package org.apache.myfaces.push
Class Json
java.lang.Object
org.apache.myfaces.push.Json
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 Summary
Modifier and TypeMethodDescriptionstatic StringEncodes the given object as JSON.static StringEscapes the given string according the JavaScript code rules.static StringformatRFC1123(Date date) Formats the givenDateto a string in RFC1123 format.static DateparseRFC1123(String string) Parses the given string in RFC1123 format to aDateobject.
-
Method Details
-
encode
Encodes the given object as JSON. This supports the standard typesBoolean,Number,CharSequenceandDate. 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 supportsCollections,Maps and arrays of them, even nested ones. TheDateis formatted in RFC 1123 format, so you can if necessary just pass it straight tonew 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
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 tofalseif you want to escape it for usage in JSON.- Returns:
- The escaped string according the JavaScript code rules.
-
formatRFC1123
Formats the givenDateto a string in RFC1123 format. This format is used in HTTP headers and in JavaScriptDateconstructor.- Parameters:
date- TheDateto be formatted to a string in RFC1123 format.- Returns:
- The formatted string.
- Since:
- 1.2
-
parseRFC1123
Parses the given string in RFC1123 format to aDateobject.- Parameters:
string- The string in RFC1123 format to be parsed to aDateobject.- Returns:
- The parsed
Date. - Throws:
ParseException- When the given string is not in RFC1123 format.- Since:
- 1.2
-