Class JsopBuilder

  • All Implemented Interfaces:
    JsopWriter

    public class JsopBuilder
    extends Object
    implements JsopWriter
    A builder for Json and Jsop strings. It encodes string values, and knows when a comma is needed. A comma is appended before '{', '[', a value, or a key; but only if the last appended token was '}', ']', or a value. There is no limit to the number of nesting levels.
    • Constructor Detail

      • JsopBuilder

        public JsopBuilder()
    • Method Detail

      • resetWriter

        public void resetWriter()
        Resets this instance.
        Specified by:
        resetWriter in interface JsopWriter
      • setLineLength

        public void setLineLength​(int length)
        Description copied from interface: JsopWriter
        Set the line length, after which a newline is added (to improve readability).
        Specified by:
        setLineLength in interface JsopWriter
        Parameters:
        length - the length
      • append

        public JsopBuilder append​(JsopWriter buffer)
        Append all entries of the given buffer.
        Specified by:
        append in interface JsopWriter
        Parameters:
        buffer - the buffer
        Returns:
        this
      • tag

        public JsopBuilder tag​(char tag)
        Append a Jsop tag character.
        Specified by:
        tag in interface JsopWriter
        Parameters:
        tag - the string to append
        Returns:
        this
      • object

        public JsopBuilder object()
        Append '{'. A comma is appended first if needed.
        Specified by:
        object in interface JsopWriter
        Returns:
        this
      • array

        public JsopBuilder array()
        Append '['. A comma is appended first if needed.
        Specified by:
        array in interface JsopWriter
        Returns:
        this
      • key

        public JsopBuilder key​(String name)
        Append the key (in quotes) plus a colon. A comma is appended first if needed.
        Specified by:
        key in interface JsopWriter
        Parameters:
        name - the key
        Returns:
        this
      • value

        public JsopBuilder value​(long value)
        Append a number. A comma is appended first if needed.
        Specified by:
        value in interface JsopWriter
        Parameters:
        value - the value
        Returns:
        this
      • value

        public JsopBuilder value​(boolean value)
        Append the boolean value 'true' or 'false'. A comma is appended first if needed.
        Specified by:
        value in interface JsopWriter
        Parameters:
        value - the value
        Returns:
        this
      • value

        public JsopBuilder value​(String value)
        Append a string or null. A comma is appended first if needed.
        Specified by:
        value in interface JsopWriter
        Parameters:
        value - the value
        Returns:
        this
      • encodedValue

        public JsopBuilder encodedValue​(String value)
        Append an already encoded value. A comma is appended first if needed.
        Specified by:
        encodedValue in interface JsopWriter
        Parameters:
        value - the value
        Returns:
        this
      • toString

        public String toString()
        Get the generated string.
        Overrides:
        toString in class Object
      • encode

        public static String encode​(String s)
        Convert a string to a quoted Json literal using the correct escape sequences. The literal is enclosed in double quotes. Characters outside the range 32..127 are encoded using escape(String, StringBuilder)). Null is encoded as "null" (without quotes).
        Parameters:
        s - the text to convert
        Returns:
        the Json representation (including double quotes)
      • escape

        public static void escape​(String s,
                                  StringBuilder buff)
        Escape a string into the target buffer.
        Parameters:
        s - the string to escape
        buff - the target buffer
      • length

        public int length()
        Get the buffer length.
        Returns:
        the length
      • prettyPrint

        public static String prettyPrint​(String jsop)
        Beautify (format) the json / jsop string.
        Parameters:
        jsop - the jsop string
        Returns:
        the formatted string