Class JSONBuilder

java.lang.Object
org.shredzone.acme4j.toolbox.JSONBuilder

public class JSONBuilder
extends java.lang.Object
Builder for JSON structures.

Example:

 JSONBuilder cb = new JSONBuilder();
 cb.put("foo", 123).put("bar", "hello world");
 cb.object("sub").put("data", "subdata");
 cb.array("array", 123, 456, 789);
 
  • Constructor Summary

    Constructors
    Constructor Description
    JSONBuilder()  
  • Method Summary

    Modifier and Type Method Description
    JSONBuilder array​(java.lang.String key, java.util.Collection<?> values)
    Puts an array.
    JSONBuilder object​(java.lang.String key)
    Creates an object for the given key.
    JSONBuilder put​(java.lang.String key, java.lang.Object value)
    Puts a property.
    JSONBuilder put​(java.lang.String key, java.time.Duration value)
    Puts a Duration to the JSON.
    JSONBuilder put​(java.lang.String key, java.time.Instant value)
    Puts an Instant to the JSON.
    JSONBuilder putBase64​(java.lang.String key, byte[] data)
    Puts binary data to the JSON.
    JSONBuilder putKey​(java.lang.String key, java.security.PublicKey publickey)
    Puts a Key into the claim.
    JSON toJSON()
    Returns a JSON representation of the current state.
    java.util.Map<java.lang.String,​java.lang.Object> toMap()
    Returns a Map representation of the current state.
    java.lang.String toString()
    Returns a JSON string representation of the current state.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • put

      public JSONBuilder put​(java.lang.String key, @Nullable java.lang.Object value)
      Puts a property. If a property with the key exists, it will be replaced.
      Parameters:
      key - Property key
      value - Property value
      Returns:
      this
    • put

      public JSONBuilder put​(java.lang.String key, @Nullable java.time.Instant value)
      Puts an Instant to the JSON. If a property with the key exists, it will be replaced.
      Parameters:
      key - Property key
      value - Property Instant value
      Returns:
      this
    • put

      public JSONBuilder put​(java.lang.String key, @Nullable java.time.Duration value)
      Puts a Duration to the JSON. If a property with the key exists, it will be replaced.
      Parameters:
      key - Property key
      value - Property Duration value
      Returns:
      this
      Since:
      2.3
    • putBase64

      public JSONBuilder putBase64​(java.lang.String key, byte[] data)
      Puts binary data to the JSON. The data is base64 url encoded.
      Parameters:
      key - Property key
      data - Property data
      Returns:
      this
    • putKey

      public JSONBuilder putKey​(java.lang.String key, java.security.PublicKey publickey)
      Puts a Key into the claim. The key is serializied as JWK.
      Parameters:
      key - Property key
      publickey - PublicKey to serialize
      Returns:
      this
    • object

      public JSONBuilder object​(java.lang.String key)
      Creates an object for the given key.
      Parameters:
      key - Key of the object
      Returns:
      Newly created JSONBuilder for the object.
    • array

      public JSONBuilder array​(java.lang.String key, java.util.Collection<?> values)
      Puts an array.
      Parameters:
      key - Property key
      values - Collection of property values
      Returns:
      this
    • toMap

      public java.util.Map<java.lang.String,​java.lang.Object> toMap()
      Returns a Map representation of the current state.
      Returns:
      Map of the current state
    • toJSON

      public JSON toJSON()
      Returns a JSON representation of the current state.
      Returns:
      JSON of the current state
    • toString

      public java.lang.String toString()
      Returns a JSON string representation of the current state.
      Overrides:
      toString in class java.lang.Object