Package com.unboundid.util.json
Class JSONBuffer
java.lang.Object
com.unboundid.util.json.JSONBuffer
- All Implemented Interfaces:
Serializable
@Mutable
@ThreadSafety(level=NOT_THREADSAFE)
public final class JSONBuffer
extends Object
implements Serializable
This class provides a mechanism for constructing the string representation of
one or more JSON objects by appending elements of those objects into a byte
string buffer.
Note that the caller is responsible for proper usage to ensure that the buffer results in a valid JSON encoding. This includes ensuring that the object begins with the appropriate opening curly brace, that all objects and arrays are properly closed, that raw values are not used outside of arrays, that named fields are not added into arrays, etc.
JSONBuffer instances may be cleared and reused any
number of times. They are not threadsafe and should not be accessed
concurrently by multiple threads.
Note that the caller is responsible for proper usage to ensure that the buffer results in a valid JSON encoding. This includes ensuring that the object begins with the appropriate opening curly brace, that all objects and arrays are properly closed, that raw values are not used outside of arrays, that named fields are not added into arrays, etc.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum buffer size. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of this JSON buffer with the default maximum buffer size.JSONBuffer(int maxBufferSize) Creates a new instance of this JSON buffer with an optional maximum retained size.JSONBuffer(ByteStringBuffer buffer, int maxBufferSize, boolean multiLine) Creates a new instance of this JSON buffer that wraps the provided byte string buffer (if provided) and that has an optional maximum retained size. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendBoolean(boolean value) Appends the provided Boolean value.voidappendBoolean(String fieldName, boolean value) Appends a JSON field with the specified name and the provided Boolean value.voidappendField(JSONField field) Appends the provided JSON field.voidAppends the provided JSON null value.voidappendNull(String fieldName) Appends a JSON field with the specified name and a null value.voidappendNumber(int value) Appends the provided JSON number value.voidappendNumber(long value) Appends the provided JSON number value.voidappendNumber(String value) Appends the provided JSON number value.voidappendNumber(String fieldName, int value) Appends a JSON field with the specified name and a number value.voidappendNumber(String fieldName, long value) Appends a JSON field with the specified name and a number value.voidappendNumber(String fieldName, String value) Appends a JSON field with the specified name and a number value.voidappendNumber(String fieldName, BigDecimal value) Appends a JSON field with the specified name and a number value.voidappendNumber(BigDecimal value) Appends the provided JSON number value.voidappendString(String value) Appends the provided JSON string value.voidappendString(String fieldName, String value) Appends a JSON field with the specified name and a null value.voidappendValue(JSONValue value) Appends the provided JSON value.voidappendValue(String fieldName, JSONValue value) Appends a field with the given name and value.voidAppends the open curly brace needed to signify the beginning of a JSON array.voidbeginArray(String fieldName) Begins a new JSON array that will be used as the value of the specified field.voidAppends the open curly brace needed to signify the beginning of a JSON object.voidbeginObject(String fieldName) Begins a new JSON object that will be used as the value of the specified field.voidclear()Clears the contents of this buffer.voidendArray()Appends the close square bracket needed to signify the end of a JSON array.voidAppends the close curly brace needed to signify the end of a JSON object.Retrieves the byte string buffer that backs this JSON buffer.intlength()Retrieves the current length of this buffer in bytes.voidsetBuffer(ByteStringBuffer buffer) Replaces the underlying buffer to which the JSON object data will be written.Retrieves the current contents of this JSON buffer as a JSON object.toString()Retrieves a string representation of the current contents of this JSON buffer.voidwriteTo(OutputStream outputStream) Writes the current contents of this JSON buffer to the provided output stream.
-
Field Details
-
DEFAULT_MAX_BUFFER_SIZE
The default maximum buffer size.- See Also:
-
-
Constructor Details
-
JSONBuffer
public JSONBuffer()Creates a new instance of this JSON buffer with the default maximum buffer size. -
JSONBuffer
Creates a new instance of this JSON buffer with an optional maximum retained size. If a maximum size is defined, then this buffer may be used to hold elements larger than that, but when the buffer is cleared it will be shrunk to the maximum size.- Parameters:
maxBufferSize- The maximum buffer size that will be retained by this JSON buffer. A value less than or equal to zero indicates that no maximum size should be enforced.
-
JSONBuffer
Creates a new instance of this JSON buffer that wraps the provided byte string buffer (if provided) and that has an optional maximum retained size. If a maximum size is defined, then this buffer may be used to hold elements larger than that, but when the buffer is cleared it will be shrunk to the maximum size.- Parameters:
buffer- The buffer to wrap. It may benullif a new buffer should be created.maxBufferSize- The maximum buffer size that will be retained by this JSON buffer. A value less than or equal to zero indicates that no maximum size should be enforced.multiLine- Indicates whether to format JSON objects using a user-friendly, formatted, multi-line representation rather than constructing the entire element without any line breaks. Note that regardless of the value of this argument, there will not be an end-of-line marker at the very end of the object.
-
-
Method Details
-
clear
Clears the contents of this buffer. -
setBuffer
Replaces the underlying buffer to which the JSON object data will be written.- Parameters:
buffer- The underlying buffer to which the JSON object data will be written.
-
length
Retrieves the current length of this buffer in bytes.- Returns:
- The current length of this buffer in bytes.
-
beginObject
Appends the open curly brace needed to signify the beginning of a JSON object. This will not include a field name, so it should only be used to start the outermost JSON object, or to start a JSON object contained in an array. -
beginObject
Begins a new JSON object that will be used as the value of the specified field.- Parameters:
fieldName- The name of the field
-
endObject
Appends the close curly brace needed to signify the end of a JSON object. -
beginArray
Appends the open curly brace needed to signify the beginning of a JSON array. This will not include a field name, so it should only be used to start a JSON array contained in an array. -
beginArray
Begins a new JSON array that will be used as the value of the specified field.- Parameters:
fieldName- The name of the field
-
endArray
Appends the close square bracket needed to signify the end of a JSON array. -
appendBoolean
Appends the provided Boolean value. This will not include a field name, so it should only be used for Boolean value elements in an array.- Parameters:
value- The Boolean value to append.
-
appendBoolean
Appends a JSON field with the specified name and the provided Boolean value.- Parameters:
fieldName- The name of the field.value- The Boolean value.
-
appendNull
Appends the provided JSON null value. This will not include a field name, so it should only be used for null value elements in an array. -
appendNull
Appends a JSON field with the specified name and a null value.- Parameters:
fieldName- The name of the field.
-
appendNumber
Appends the provided JSON number value. This will not include a field name, so it should only be used for number elements in an array.- Parameters:
value- The number to add.
-
appendNumber
Appends the provided JSON number value. This will not include a field name, so it should only be used for number elements in an array.- Parameters:
value- The number to add.
-
appendNumber
Appends the provided JSON number value. This will not include a field name, so it should only be used for number elements in an array.- Parameters:
value- The number to add.
-
appendNumber
Appends the provided JSON number value. This will not include a field name, so it should only be used for number elements in an array.- Parameters:
value- The string representation of the number to add. It must be properly formed.
-
appendNumber
Appends a JSON field with the specified name and a number value.- Parameters:
fieldName- The name of the field.value- The number value.
-
appendNumber
Appends a JSON field with the specified name and a number value.- Parameters:
fieldName- The name of the field.value- The number value.
-
appendNumber
Appends a JSON field with the specified name and a number value.- Parameters:
fieldName- The name of the field.value- The number value.
-
appendNumber
Appends a JSON field with the specified name and a number value.- Parameters:
fieldName- The name of the field.value- The string representation of the number ot add. It must be properly formed.
-
appendString
Appends the provided JSON string value. This will not include a field name, so it should only be used for string elements in an array.- Parameters:
value- The value to add.
-
appendString
Appends a JSON field with the specified name and a null value.- Parameters:
fieldName- The name of the field.value- The value to add.
-
appendValue
Appends the provided JSON value. This will not include a field name, so it should only be used for elements in an array.- Parameters:
value- The value to append.
-
appendValue
Appends a field with the given name and value.- Parameters:
fieldName- The name of the field.value- The value to append.
-
appendField
Appends the provided JSON field.- Parameters:
field- The JSON field to be appended.
-
getBuffer
Retrieves the byte string buffer that backs this JSON buffer.- Returns:
- The byte string buffer that backs this JSON buffer.
-
writeTo
Writes the current contents of this JSON buffer to the provided output stream. Note that based on the current contents of this buffer and the way it has been used so far, it may not represent a valid JSON object.- Parameters:
outputStream- The output stream to which the current contents of this JSON buffer should be written.- Throws:
IOException- If a problem is encountered while writing to the provided output stream.
-
toString
Retrieves a string representation of the current contents of this JSON buffer. Note that based on the current contents of this buffer and the way it has been used so far, it may not represent a valid JSON object. -
toJSONObject
Retrieves the current contents of this JSON buffer as a JSON object.- Returns:
- The JSON object decoded from the contents of this JSON buffer.
- Throws:
JSONException- If the buffer does not currently contain exactly one valid JSON object.
-