Class Metrics.JsonObjectBuilder

java.lang.Object
com.ultikits.ultitools.utils.Metrics.JsonObjectBuilder
Enclosing class:
Metrics

public static class Metrics.JsonObjectBuilder extends Object
An extremely simple JSON builder.

While this class is neither feature-rich nor the most performant one, it's sufficient enough for its use-case.

  • Field Details

    • builder

      private StringBuilder builder
    • hasAtLeastOneField

      private boolean hasAtLeastOneField
  • Constructor Details

    • JsonObjectBuilder

      public JsonObjectBuilder()
  • Method Details

    • appendNull

      public Metrics.JsonObjectBuilder appendNull(String key)
      Appends a null field to the JSON.
      Parameters:
      key - The key of the field.
      Returns:
      A reference to this object.
    • appendField

      public Metrics.JsonObjectBuilder appendField(String key, String value)
      Appends a string field to the JSON.
      Parameters:
      key - The key of the field.
      value - The value of the field.
      Returns:
      A reference to this object.
    • appendField

      public Metrics.JsonObjectBuilder appendField(String key, int value)
      Appends an integer field to the JSON.
      Parameters:
      key - The key of the field.
      value - The value of the field.
      Returns:
      A reference to this object.
    • appendField

      Appends an object to the JSON.
      Parameters:
      key - The key of the field.
      object - The object.
      Returns:
      A reference to this object.
    • appendField

      public Metrics.JsonObjectBuilder appendField(String key, String[] values)
      Appends a string array to the JSON.
      Parameters:
      key - The key of the field.
      values - The string array.
      Returns:
      A reference to this object.
    • appendField

      public Metrics.JsonObjectBuilder appendField(String key, int[] values)
      Appends an integer array to the JSON.
      Parameters:
      key - The key of the field.
      values - The integer array.
      Returns:
      A reference to this object.
    • appendField

      Appends an object array to the JSON.
      Parameters:
      key - The key of the field.
      values - The integer array.
      Returns:
      A reference to this object.
    • appendFieldUnescaped

      private void appendFieldUnescaped(String key, String escapedValue)
      Appends a field to the object.
      Parameters:
      key - The key of the field.
      escapedValue - The escaped value of the field.
    • build

      Builds the JSON string and invalidates this builder.
      Returns:
      The built JSON string.
    • escape

      private static String escape(String value)
      Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.

      This method escapes only the necessary characters '"', '\'. and '' - ''. Compact escapes are not used (e.g., '\n' is escaped as " " and not as "\n").

      Parameters:
      value - The value to escape.
      Returns:
      The escaped value.