Class JSONBuilder


  • public class JSONBuilder
    extends 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);
     
    • Method Detail

      • put

        public JSONBuilder put​(String key,
                               @Nullable
                               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​(String key,
                               @Nullable
                               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​(String key,
                               @Nullable
                               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​(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
      • object

        public JSONBuilder object​(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​(String key,
                                 Collection<?> values)
        Puts an array.
        Parameters:
        key - Property key
        values - Collection of property values
        Returns:
        this
      • toMap

        public Map<String,​ObjecttoMap()
        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