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 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:

Since:
2.3
Author:
Bauke Scholtz
  • Constructor Details

    • Json

      public Json()
  • Method Details

    • 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 as data 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 as data 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.