Class JSONBuilder

java.lang.Object
org.incenp.obofoundry.sssom.util.JSONBuilder

public class JSONBuilder extends Object
A helper class to assemble a JSON string.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a key in the current dictionary.
    void
    addValue(boolean value)
    Adds a boolean value at the current position.
    void
    Adds a floating point value at the current position.
    void
    Adds an arbitrary value at the current position.
    void
    Adds a string value at the current position.
    Closes all open structures and returns the resulting JSON string.
    void
    end()
    Closes the current list or dictionary, whichever it is.
    void
    Closes the current dictionary.
    void
    Closes the current list.
    void
    Starts a new dictionary at the current position.
    void
    Starts a new list at the current position.

    Methods inherited from class Object

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

    • JSONBuilder

      public JSONBuilder()
  • Method Details

    • startDict

      public void startDict()
      Starts a new dictionary at the current position.
    • endDict

      public void endDict()
      Closes the current dictionary.

      This is a no-op if the current structure is not, in fact, a dictionary.

    • startList

      public void startList()
      Starts a new list at the current position.
    • endList

      public void endList()
      Closes the current list.

      This is a no-op if the current structure is not, in fact, a list.

    • end

      public void end()
      Closes the current list or dictionary, whichever it is.
    • addKey

      public void addKey(String key)
      Adds a key in the current dictionary.

      If the current structure is not already a dictionary, a new dictionary is automatically started.

      Parameters:
      key - The name of the key to add.
    • addValue

      public void addValue(String value)
      Adds a string value at the current position.
      Parameters:
      value - The value to add.
    • addValue

      public void addValue(boolean value)
      Adds a boolean value at the current position.
      Parameters:
      value - The value to add.
    • addValue

      public void addValue(Double value)
      Adds a floating point value at the current position.
      Parameters:
      value - The value to add.
    • addValue

      public void addValue(Object value)
      Adds an arbitrary value at the current position.
      Parameters:
      value - The value to add.
    • close

      public String close()
      Closes all open structures and returns the resulting JSON string.

      This automatically clears the builder, which can then be reused to build a new JSON string.

      Returns:
      The assembled JSON string.