Package org.omnifaces.util
Class Json
- java.lang.Object
-
- org.omnifaces.util.Json
-
public final class Json extends Object
A simple JSON encoder.- Since:
- 1.2
- Author:
- 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
encode(Object object, UnaryOperator<String> propertyNameFormatter)
Does the same asencode(Object)
but then with a custom property name formatter.
-
-
-
Method Detail
-
encode
public static String encode(Object object)
Encodes the given object as JSON. This supports the standard typesBoolean
,Number
,CharSequence
,Date
and since OmniFaces 3.6 alsoEnum
andTemporal
. 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
andTemporal
are 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.
-
encode
public static String encode(Object object, UnaryOperator<String> propertyNameFormatter)
Does the same asencode(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
-
-