Class JsopBuilder
java.lang.Object
org.apache.jackrabbit.oak.commons.json.JsopBuilder
- All Implemented Interfaces:
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 -
Method Summary
Modifier and TypeMethodDescriptionappend(JsopWriter buffer) Append all entries of the given buffer.array()Append '['.static StringConvert a string to a quoted Json literal using the correct escape sequences.encodedValue(String value) Append an already encoded value.endArray()Append ']'.Append '}'.static voidescape(String s, StringBuilder buff) Escape a string into the target buffer.Append the key (in quotes) plus a colon.intlength()Get the buffer length.newline()Append a newline character.object()Append '{'.static StringprettyPrint(String jsop) Beautify (format) the json / jsop string.voidResets this instance.voidsetLineLength(int length) Set the line length, after which a newline is added (to improve readability).tag(char tag) Append a Jsop tag character.toString()Get the generated string.value(boolean value) Append the boolean value 'true' or 'false'.value(long value) Append a number.Append a string or null.
-
Constructor Details
-
JsopBuilder
public JsopBuilder()
-
-
Method Details
-
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
Append all entries of the given buffer.- Specified by:
appendin interfaceJsopWriter- Parameters:
buffer- the buffer- Returns:
- this
-
tag
Append a Jsop tag character.- Specified by:
tagin interfaceJsopWriter- Parameters:
tag- the string to append- Returns:
- this
-
newline
Append a newline character.- Specified by:
newlinein interfaceJsopWriter- Returns:
- this
-
object
Append '{'. A comma is appended first if needed.- Specified by:
objectin interfaceJsopWriter- Returns:
- this
-
endObject
Append '}'.- Specified by:
endObjectin interfaceJsopWriter- Returns:
- this
-
array
Append '['. A comma is appended first if needed.- Specified by:
arrayin interfaceJsopWriter- Returns:
- this
-
endArray
Append ']'.- Specified by:
endArrayin interfaceJsopWriter- Returns:
- this
-
key
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
Append a number. A comma is appended first if needed.- Specified by:
valuein interfaceJsopWriter- Parameters:
value- the value- Returns:
- this
-
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
Append a string or null. A comma is appended first if needed.- Specified by:
valuein interfaceJsopWriter- Parameters:
value- the value- Returns:
- this
-
encodedValue
Append an already encoded value. A comma is appended first if needed.- Specified by:
encodedValuein interfaceJsopWriter- Parameters:
value- the value- Returns:
- this
-
toString
Get the generated string. -
encode
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
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
Beautify (format) the json / jsop string.- Parameters:
jsop- the jsop string- Returns:
- the formatted string
-