Class JsopBuilder
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.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
Constructors Constructor Description JsopBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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, 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)
Set the line length, after which a newline is added (to improve readability).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.
-
-
-
Method Detail
-
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
public JsopBuilder append(JsopWriter buffer)
Append all entries of the given buffer.- Specified by:
append
in interfaceJsopWriter
- Parameters:
buffer
- the buffer- Returns:
- this
-
tag
public JsopBuilder tag(char tag)
Append a Jsop tag character.- Specified by:
tag
in interfaceJsopWriter
- Parameters:
tag
- the string to append- Returns:
- this
-
newline
public JsopBuilder newline()
Append a newline character.- Specified by:
newline
in interfaceJsopWriter
- Returns:
- this
-
object
public JsopBuilder object()
Append '{'. A comma is appended first if needed.- Specified by:
object
in interfaceJsopWriter
- Returns:
- this
-
endObject
public JsopBuilder endObject()
Append '}'.- Specified by:
endObject
in interfaceJsopWriter
- Returns:
- this
-
array
public JsopBuilder array()
Append '['. A comma is appended first if needed.- Specified by:
array
in interfaceJsopWriter
- Returns:
- this
-
endArray
public JsopBuilder endArray()
Append ']'.- Specified by:
endArray
in interfaceJsopWriter
- 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 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:
value
in 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:
value
in interfaceJsopWriter
- 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 interfaceJsopWriter
- 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 interfaceJsopWriter
- Parameters:
value
- the value- Returns:
- this
-
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 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(String s, 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
-
-