Class JSONBuilder


  • public class JSONBuilder
    extends java.lang.Object
    JSONBuilder for Groovy API
    HOW TO USE IT.
    1.
     
         JSONBuilder builder = new JSONBuilder();
         builder.put("version", "1.0")
                .array("events")
                    .object()
                        .put("happenOn", "2015-07-01T22:25:24.988+08:00")
                        .put("severity", "warn")
                        .put("message", "This is a message")
                        .put("custom", "This is a customer")
                    .endObject()
                .endArray();
     
     
    2.
     
         JSONBuilder builder = new JSONBuilder();
         builder.put("version", "1.0")
                .object("object")
                    .array("array")
                        .put("array-value")
                    .endArray()
                    .put("attr", "value")
                .endObject();
     
     
    Note: object() & endObject() is a pair method, which means that they should show up at the same time. So does array() & endArray()
    • Constructor Detail

      • JSONBuilder

        public JSONBuilder()
        Root JSONObject
      • JSONBuilder

        public JSONBuilder​(boolean isObject,
                           JSONBuilder parent)
        Parameters:
        isObject - true means current is JSONObject, otherwise is JSONArray
        parent - current object's parent, null means current is the ROOT
      • JSONBuilder

        public JSONBuilder​(JSONBuilder parent)
        New an JSONObject Builder
        Parameters:
        parent - Parent JSONBuilder
    • Method Detail

      • object

        public JSONBuilder object​(java.lang.String key)
        Start a new JSONObject,
        Parameters:
        key - null means current is JSONArray, otherwise means current is JSONObject
        Returns:
        a new JSONObject
      • object

        public JSONBuilder object()
        Start new JSONObject in a JSONArray
        Returns:
        a new JSONObject
      • endObject

        public JSONBuilder endObject()
        This should be show up in pair with object() This means
        Returns:
        current JSONObject's parent Object
      • array

        public JSONBuilder array​(java.lang.String key)
        Start a new JSONArray
        Parameters:
        key - null means current object is a JSONArray, otherwise is a JSONObject
        Returns:
        a new JSONArray
      • array

        public JSONBuilder array()
        Start a new JSONArray & current object is a JSONArray
        Returns:
        JSONBuilder object
      • endArray

        public JSONBuilder endArray()
        mean current JSONArray is ended.
        Returns:
        current's parent object
      • put

        public JSONBuilder put​(java.lang.String key,
                               java.lang.Object value)
        Put an new key-value in a JSONObjet
        Parameters:
        key - JSON key
        value - JSON value
        Returns:
        JSONBuilder object
      • put

        public JSONBuilder put​(java.lang.String value)
        Put an value in a JSONArray
        Parameters:
        value - JSON value
        Returns:
        JSONBuilder object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(int indentFactor)
        Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data structure is acyclical.
        Parameters:
        indentFactor - The number of spaces to add to each level of indentation.
        Returns:
        a printable, displayable, transmittable representation of the object, beginning with [ (left bracket) and ending with ]  (right bracket).