org.apache.jackrabbit.mk.json
Class JsopBuilder

java.lang.Object
  extended by org.apache.jackrabbit.mk.json.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 Summary
JsopBuilder()
           
 
Method Summary
 JsopBuilder append(JsopWriter buffer)
          Append all entries of the given buffer.
 JsopBuilder array()
          Append '['.
static String encode(String s)
          Convert a string to a quoted Json literal using the correct escape sequences.
 JsopBuilder encodedValue(String value)
          Append an already encoded value.
 JsopBuilder endArray()
          Append ']'.
 JsopBuilder endObject()
          Append '}'.
static void escape(String s, int length, StringBuilder buff)
          Escape a string into the target buffer.
 JsopBuilder key(String name)
          Append the key (in quotes) plus a colon.
 int length()
          Get the buffer length.
 JsopBuilder newline()
          Append a newline character.
 JsopBuilder object()
          Append '{'.
static String prettyPrint(String jsop)
          Beautify (format) the json / jsop string.
 void resetWriter()
          Resets this instance.
 void setLineLength(int length)
           
 JsopBuilder tag(char tag)
          Append a Jsop tag character.
 String toString()
          Get the generated string.
 JsopBuilder value(boolean value)
          Append the boolean value 'true' or 'false'.
 JsopBuilder value(long value)
          Append a number.
 JsopBuilder value(String value)
          Append a string or null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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)
Specified by:
setLineLength in interface JsopWriter

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

newline

public JsopBuilder newline()
Append a newline character.

Specified by:
newline in interface JsopWriter
Returns:
this

object

public JsopBuilder object()
Append '{'. A comma is appended first if needed.

Specified by:
object in interface JsopWriter
Returns:
this

endObject

public JsopBuilder endObject()
Append '}'.

Specified by:
endObject 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

endArray

public JsopBuilder endArray()
Append ']'.

Specified by:
endArray 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 (backslash u xxxx). The forward slash (solidus) is not escaped. 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,
                          int length,
                          StringBuilder buff)
Escape a string into the target buffer.

Parameters:
s - the string to escape
length - the number of characters.
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


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.