Package com.sun.faces.util
Class Json
java.lang.Object
com.sun.faces.util.Json
Generic JSON encoder using jakarta.json API.
This supports the standard types Boolean
, Number
, Character
, CharSequence
,
Date
, LocalDate
and Instant
. If the given object type does not match any of them, then it
will attempt to inspect the object as a JavaBean using the Introspector
, whereby the public properties
(public getters) will be encoded as a JS object. It also supports arrays, Collection
s and Map
s of
them, even nested ones. The dates are formatted as ISO8601 instant via DateTimeFormatter.ISO_INSTANT
, so you
can if necessary just pass the value straight to new Date(value)
in JavaScript.
Below encoding options are available:
Json.Option.SKIP_NULL_VALUES
: skip null values in arrays, collections, maps and beans. This may reduce an unnecessarily bloated JSON object.Json.Option.USE_RFC1123_DATE
: format dates as RFC1123 viaDateTimeFormatter.RFC_1123_DATE_TIME
. This may improve compatibility with older web browsers.
- Since:
- 2.3
- Author:
- Bauke Scholtz
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
encode
(Object object, Json.Option... options) Encodes the given object as JSON and returns a string in JSON format.static void
encode
(Object object, Writer writer, Json.Option... options) Encodes the given object as JSON while streaming the string in JSON format to the given writer.
-
Constructor Details
-
Json
public Json()
-
-
Method Details
-
encode
Encodes the given object as JSON and returns a string in JSON format. The encoded object will be available asdata
property of the JS object in the returned JSON string.- Parameters:
object
- The object to be encoded as JSON.options
- The encoding options.- Returns:
- The JSON-encoded representation of the given object.
- Throws:
IllegalArgumentException
- When given object or one of its properties cannot be inspected as a JavaBean.
-
encode
Encodes the given object as JSON while streaming the string in JSON format to the given writer. The encoded object will be available asdata
property of the JS object in the returned JSON string.- Parameters:
object
- The object to be encoded as JSON.writer
- The writer to stream the encoded output to.options
- The encoding options.- Throws:
IllegalArgumentException
- When given object or one of its properties cannot be inspected as a JavaBean.
-