Class JsopBuilder
- java.lang.Object
 - 
- org.apache.jackrabbit.oak.commons.json.JsopBuilder
 
 
- 
- All Implemented Interfaces:
 JsopWriter
public class JsopBuilder extends java.lang.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
Constructors Constructor Description JsopBuilder() 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsopBuilderappend(JsopWriter buffer)Append all entries of the given buffer.JsopBuilderarray()Append '['.static java.lang.Stringencode(java.lang.String s)Convert a string to a quoted Json literal using the correct escape sequences.JsopBuilderencodedValue(java.lang.String value)Append an already encoded value.JsopBuilderendArray()Append ']'.JsopBuilderendObject()Append '}'.static voidescape(java.lang.String s, java.lang.StringBuilder buff)Escape a string into the target buffer.JsopBuilderkey(java.lang.String name)Append the key (in quotes) plus a colon.intlength()Get the buffer length.JsopBuildernewline()Append a newline character.JsopBuilderobject()Append '{'.static java.lang.StringprettyPrint(java.lang.String jsop)Beautify (format) the json / jsop string.voidresetWriter()Resets this instance.voidsetLineLength(int length)Set the line length, after which a newline is added (to improve readability).JsopBuildertag(char tag)Append a Jsop tag character.java.lang.StringtoString()Get the generated string.JsopBuildervalue(boolean value)Append the boolean value 'true' or 'false'.JsopBuildervalue(long value)Append a number.JsopBuildervalue(java.lang.String value)Append a string or null. 
 - 
 
- 
- 
Method Detail
- 
resetWriter
public void resetWriter()
Resets this instance.- Specified by:
 resetWriterin interfaceJsopWriter
 
- 
setLineLength
public void setLineLength(int length)
Description copied from interface:JsopWriterSet the line length, after which a newline is added (to improve readability).- Specified by:
 setLineLengthin interfaceJsopWriter- Parameters:
 length- the length
 
- 
append
public JsopBuilder append(JsopWriter buffer)
Append all entries of the given buffer.- Specified by:
 appendin interfaceJsopWriter- Parameters:
 buffer- the buffer- Returns:
 - this
 
 
- 
tag
public JsopBuilder tag(char tag)
Append a Jsop tag character.- Specified by:
 tagin interfaceJsopWriter- Parameters:
 tag- the string to append- Returns:
 - this
 
 
- 
newline
public JsopBuilder newline()
Append a newline character.- Specified by:
 newlinein interfaceJsopWriter- Returns:
 - this
 
 
- 
object
public JsopBuilder object()
Append '{'. A comma is appended first if needed.- Specified by:
 objectin interfaceJsopWriter- Returns:
 - this
 
 
- 
endObject
public JsopBuilder endObject()
Append '}'.- Specified by:
 endObjectin interfaceJsopWriter- Returns:
 - this
 
 
- 
array
public JsopBuilder array()
Append '['. A comma is appended first if needed.- Specified by:
 arrayin interfaceJsopWriter- Returns:
 - this
 
 
- 
endArray
public JsopBuilder endArray()
Append ']'.- Specified by:
 endArrayin interfaceJsopWriter- Returns:
 - this
 
 
- 
key
public JsopBuilder key(java.lang.String name)
Append the key (in quotes) plus a colon. A comma is appended first if needed.- Specified by:
 keyin interfaceJsopWriter- Parameters:
 name- the key- Returns:
 - this
 
 
- 
value
public JsopBuilder value(long value)
Append a number. A comma is appended first if needed.- Specified by:
 valuein interfaceJsopWriter- 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:
 valuein interfaceJsopWriter- Parameters:
 value- the value- Returns:
 - this
 
 
- 
value
public JsopBuilder value(java.lang.String value)
Append a string or null. A comma is appended first if needed.- Specified by:
 valuein interfaceJsopWriter- Parameters:
 value- the value- Returns:
 - this
 
 
- 
encodedValue
public JsopBuilder encodedValue(java.lang.String value)
Append an already encoded value. A comma is appended first if needed.- Specified by:
 encodedValuein interfaceJsopWriter- Parameters:
 value- the value- Returns:
 - this
 
 
- 
toString
public java.lang.String toString()
Get the generated string.- Overrides:
 toStringin classjava.lang.Object
 
- 
encode
public static java.lang.String encode(java.lang.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 usingescape(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(java.lang.String s, java.lang.StringBuilder buff)Escape a string into the target buffer.- Parameters:
 s- the string to escapebuff- the target buffer
 
- 
length
public int length()
Get the buffer length.- Returns:
 - the length
 
 
- 
prettyPrint
public static java.lang.String prettyPrint(java.lang.String jsop)
Beautify (format) the json / jsop string.- Parameters:
 jsop- the jsop string- Returns:
 - the formatted string
 
 
 - 
 
 -