Package org.apache.myfaces.push
Class Json
- java.lang.Object
-
- org.apache.myfaces.push.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
encode(Object object)
Encodes the given object as JSON.static String
escapeJS(String string, boolean escapeSingleQuote)
Escapes the given string according the JavaScript code rules.static String
formatRFC1123(Date date)
Formats the givenDate
to a string in RFC1123 format.static Date
parseRFC1123(String string)
Parses the given string in RFC1123 format to aDate
object.
-
-
-
Method Detail
-
encode
public static String encode(Object object)
Encodes the given object as JSON. This supports the standard typesBoolean
,Number
,CharSequence
andDate
. 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 supportsCollection
s,Map
s and arrays of them, even nested ones. TheDate
is 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
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 tofalse
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 givenDate
to a string in RFC1123 format. This format is used in HTTP headers and in JavaScriptDate
constructor.- Parameters:
date
- TheDate
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 aDate
object.- Parameters:
string
- The string in RFC1123 format to be parsed to aDate
object.- Returns:
- The parsed
Date
. - Throws:
ParseException
- When the given string is not in RFC1123 format.- Since:
- 1.2
-
-