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 String
Convert a string to a quoted Json literal using the correct escape sequences.encodedValue
(String value) Append an already encoded value.endArray()
Append ']'.Append '}'.static void
escape
(String s, StringBuilder buff) Escape a string into the target buffer.Append the key (in quotes) plus a colon.int
length()
Get the buffer length.newline()
Append a newline character.object()
Append '{'.static String
prettyPrint
(String jsop) Beautify (format) the json / jsop string.void
Resets this instance.void
setLineLength
(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:
resetWriter
in interfaceJsopWriter
-
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 interfaceJsopWriter
- Parameters:
length
- the length
-
append
Append all entries of the given buffer.- Specified by:
append
in interfaceJsopWriter
- Parameters:
buffer
- the buffer- Returns:
- this
-
tag
Append a Jsop tag character.- Specified by:
tag
in interfaceJsopWriter
- Parameters:
tag
- the string to append- Returns:
- this
-
newline
Append a newline character.- Specified by:
newline
in interfaceJsopWriter
- Returns:
- this
-
object
Append '{'. A comma is appended first if needed.- Specified by:
object
in interfaceJsopWriter
- Returns:
- this
-
endObject
Append '}'.- Specified by:
endObject
in interfaceJsopWriter
- Returns:
- this
-
array
Append '['. A comma is appended first if needed.- Specified by:
array
in interfaceJsopWriter
- Returns:
- this
-
endArray
Append ']'.- Specified by:
endArray
in interfaceJsopWriter
- Returns:
- this
-
key
Append the key (in quotes) plus a colon. A comma is appended first if needed.- Specified by:
key
in interfaceJsopWriter
- Parameters:
name
- the key- Returns:
- this
-
value
Append a number. A comma is appended first if needed.- Specified by:
value
in interfaceJsopWriter
- Parameters:
value
- the value- Returns:
- this
-
value
Append the boolean value 'true' or 'false'. A comma is appended first if needed.- Specified by:
value
in interfaceJsopWriter
- Parameters:
value
- the value- Returns:
- this
-
value
Append a string or null. A comma is appended first if needed.- Specified by:
value
in interfaceJsopWriter
- Parameters:
value
- the value- Returns:
- this
-
encodedValue
Append an already encoded value. A comma is appended first if needed.- Specified by:
encodedValue
in 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
-