public class Json extends Object
Generic JSON encoder using javax.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 via DateTimeFormatter.RFC_1123_DATE_TIME
.
This may improve compatibility with older web browsers.Modifier and Type | Class and Description |
---|---|
static class |
Json.Option |
Constructor and Description |
---|
Json() |
Modifier and Type | Method and 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.
|
public static String encode(Object object, Json.Option... options)
data
property of the JS object in the returned JSON string.object
- The object to be encoded as JSON.options
- The encoding options.IllegalArgumentException
- When given object or one of its properties cannot be inspected as a JavaBean.public static void encode(Object object, Writer writer, Json.Option... options)
data
property of the JS object in the returned JSON string.object
- The object to be encoded as JSON.writer
- The writer to stream the encoded output to.options
- The encoding options.IllegalArgumentException
- When given object or one of its properties cannot be inspected as a JavaBean.Comments to: faces-dev@eclipse.org.
Copyright © 2019 Eclipse Foundation. All rights reserved.
Use is subject to license terms.