Top Description Inners Fields Constructors Methods
com.sun.faces.util

public Class Json

extends Object
Class Inheritance
Static Imports
com.sun.faces.util.Json.Option.SKIP_NULL_VALUES, .Json.Option.USE_RFC1123_DATE, java.time.ZoneOffset.UTC, java.time.format.DateTimeFormatter.ISO_INSTANT, .DateTimeFormatter.RFC_1123_DATE_TIME, java.util.Arrays.asList, .EnumSet.copyOf, .EnumSet.noneOf

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, Collections and Maps 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:

Author
Bauke Scholtz
Since
2.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public
Json()

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

The JSON-encoded representation of the given object.
encode
(Object
The object to be encoded as JSON.
object
,
Json.Option...
The encoding options.
options
)

Encodes the given object as JSON and returns a string in JSON format.

public static void
encode(Object
The object to be encoded as JSON.
object
,
Writer
The writer to stream the encoded output to.
writer
,
Json.Option...
The encoding options.
options
)

Encodes the given object as JSON while streaming the string in JSON format to the given writer.

private static void
encode(String name, Object object, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
encodeArray(String name, Object array, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
encodeBean(String name, Object bean, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
private static void
private static void
encodeBoolean(String name, Boolean value, JsonGenerator generator)

private static void
encodeCollection(String name, Collection<?> collection, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
encodeDouble(String name, double value, JsonGenerator generator)

private static void
encodeInstant(String name, Instant value, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
encodeInteger(String name, int value, JsonGenerator generator)

private static void
encodeLong(String name, long value, JsonGenerator generator)

private static void
encodeMap(String name, Map<?, ?> map, JsonGenerator generator, EnumSet<Json.Option> options)

private static void
encodeNull(String name, JsonGenerator generator)

private static void
encodeString(String name, String value, JsonGenerator generator)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ERROR_INVALID_BEANback to summary
private static final String ERROR_INVALID_BEAN
ERROR_INVALID_GETTERback to summary
private static final String ERROR_INVALID_GETTER

Constructor Detail

Jsonback to summary
public Json()

Method Detail

encodeback to summary
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 as data property of the JS object in the returned JSON string.

Parameters
object:Object

The object to be encoded as JSON.

options:Json.Option[]

The encoding options.

Returns:String

The JSON-encoded representation of the given object.

Exceptions
IllegalArgumentException:
When given object or one of its properties cannot be inspected as a JavaBean.
encodeback to summary
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 as data property of the JS object in the returned JSON string.

Parameters
object:Object

The object to be encoded as JSON.

writer:Writer

The writer to stream the encoded output to.

options:Json.Option[]

The encoding options.

Exceptions
IllegalArgumentException:
When given object or one of its properties cannot be inspected as a JavaBean.
encodeback to summary
private static void encode(String name, Object object, JsonGenerator generator, EnumSet<Json.Option> options)
encodeArrayback to summary
private static void encodeArray(String name, Object array, JsonGenerator generator, EnumSet<Json.Option> options)
encodeBeanback to summary
private static void encodeBean(String name, Object bean, JsonGenerator generator, EnumSet<Json.Option> options)
encodeBigDecimalback to summary
private static void encodeBigDecimal(String name, BigDecimal value, JsonGenerator generator)
encodeBigIntegerback to summary
private static void encodeBigInteger(String name, BigInteger value, JsonGenerator generator)
encodeBooleanback to summary
private static void encodeBoolean(String name, Boolean value, JsonGenerator generator)
encodeCollectionback to summary
private static void encodeCollection(String name, Collection<?> collection, JsonGenerator generator, EnumSet<Json.Option> options)
encodeDoubleback to summary
private static void encodeDouble(String name, double value, JsonGenerator generator)
encodeInstantback to summary
private static void encodeInstant(String name, Instant value, JsonGenerator generator, EnumSet<Json.Option> options)
encodeIntegerback to summary
private static void encodeInteger(String name, int value, JsonGenerator generator)
encodeLongback to summary
private static void encodeLong(String name, long value, JsonGenerator generator)
encodeMapback to summary
private static void encodeMap(String name, Map<?, ?> map, JsonGenerator generator, EnumSet<Json.Option> options)
encodeNullback to summary
private static void encodeNull(String name, JsonGenerator generator)
encodeStringback to summary
private static void encodeString(String name, String value, JsonGenerator generator)
com.sun.faces.util back to summary

public final Enum Json.Option

extends Enum<Json.Option>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final Json.Option
SKIP_NULL_VALUES

Skip null values in arrays, collections, maps and beans.

public static final Json.Option

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Json.Option
public static Json.Option[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

SKIP_NULL_VALUESback to summary
public static final Json.Option SKIP_NULL_VALUES

Skip null values in arrays, collections, maps and beans. This may reduce an unnecessarily bloated JSON object.

USE_RFC1123_DATEback to summary
public static final Json.Option USE_RFC1123_DATE

Format dates as RFC1123 via DateTimeFormatter#RFC_1123_DATE_TIME. This may improve compatibility with older web browsers.

Constructor Detail

Optionback to summary
private Option()

Method Detail

valueOfback to summary
public static Json.Option valueOf(String name)
valuesback to summary
public static Json.Option[] values()