Package com.sun.faces.util
Class Json
- java.lang.Object
-
- com.sun.faces.util.Json
-
public class Json extends Object
Generic JSON encoder using jakarta.json API.
This supports the standard types
Boolean
,Number
,Character
,CharSequence
,Date
,LocalDate
andInstant
. If the given object type does not match any of them, then it will attempt to inspect the object as a JavaBean using theIntrospector
, whereby the public properties (public getters) will be encoded as a JS object. It also supports arrays,Collection
s andMap
s of them, even nested ones. The dates are formatted as ISO8601 instant viaDateTimeFormatter.ISO_INSTANT
, so you can if necessary just pass the value straight tonew 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
Nested Classes Modifier and Type Class Description static class
Json.Option
-
Constructor Summary
Constructors Constructor Description Json()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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.
-
-
-
Method Detail
-
encode
public static String encode(Object object, Json.Option... options)
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
public 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. 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.
-
-