@Mutable @ThreadSafety(level=NOT_THREADSAFE) public final class JSONBuffer extends java.lang.Object implements java.io.Serializable
JSONBuffer
instances may be cleared and reused any
number of times. They are not threadsafe and should not be accessed
concurrently by multiple threads.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_BUFFER_SIZE
The default maximum buffer size.
|
Constructor and Description |
---|
JSONBuffer()
Creates a new instance of this JSON buffer with the default maximum buffer
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.
|
JSONBuffer(int maxBufferSize)
Creates a new instance of this JSON buffer with an optional maximum
retained size.
|
Modifier and Type | Method and Description |
---|---|
void |
appendBoolean(boolean value)
Appends the provided Boolean value.
|
void |
appendBoolean(java.lang.String fieldName,
boolean value)
Appends a JSON field with the specified name and the provided Boolean
value.
|
void |
appendField(JSONField field)
Appends the provided JSON field.
|
void |
appendNull()
Appends the provided JSON null value.
|
void |
appendNull(java.lang.String fieldName)
Appends a JSON field with the specified name and a null value.
|
void |
appendNumber(java.math.BigDecimal value)
Appends the provided JSON number value.
|
void |
appendNumber(int value)
Appends the provided JSON number value.
|
void |
appendNumber(long value)
Appends the provided JSON number value.
|
void |
appendNumber(java.lang.String value)
Appends the provided JSON number value.
|
void |
appendNumber(java.lang.String fieldName,
java.math.BigDecimal value)
Appends a JSON field with the specified name and a number value.
|
void |
appendNumber(java.lang.String fieldName,
int value)
Appends a JSON field with the specified name and a number value.
|
void |
appendNumber(java.lang.String fieldName,
long value)
Appends a JSON field with the specified name and a number value.
|
void |
appendNumber(java.lang.String fieldName,
java.lang.String value)
Appends a JSON field with the specified name and a number value.
|
void |
appendString(java.lang.String value)
Appends the provided JSON string value.
|
void |
appendString(java.lang.String fieldName,
java.lang.String value)
Appends a JSON field with the specified name and a null value.
|
void |
appendValue(JSONValue value)
Appends the provided JSON value.
|
void |
appendValue(java.lang.String fieldName,
JSONValue value)
Appends a field with the given name and value.
|
void |
beginArray()
Appends the open curly brace needed to signify the beginning of a JSON
array.
|
void |
beginArray(java.lang.String fieldName)
Begins a new JSON array that will be used as the value of the specified
field.
|
void |
beginObject()
Appends the open curly brace needed to signify the beginning of a JSON
object.
|
void |
beginObject(java.lang.String fieldName)
Begins a new JSON object that will be used as the value of the specified
field.
|
void |
clear()
Clears the contents of this buffer.
|
void |
endArray()
Appends the close square bracket needed to signify the end of a JSON array.
|
void |
endObject()
Appends the close curly brace needed to signify the end of a JSON object.
|
ByteStringBuffer |
getBuffer()
Retrieves the byte string buffer that backs this JSON buffer.
|
int |
length()
Retrieves the current length of this buffer in bytes.
|
void |
setBuffer(ByteStringBuffer buffer)
Replaces the underlying buffer to which the JSON object data will be
written.
|
JSONObject |
toJSONObject()
Retrieves the current contents of this JSON buffer as a JSON object.
|
java.lang.String |
toString()
Retrieves a string representation of the current contents of this JSON
buffer.
|
void |
writeTo(java.io.OutputStream outputStream)
Writes the current contents of this JSON buffer to the provided output
stream.
|
public static final int DEFAULT_MAX_BUFFER_SIZE
public JSONBuffer()
public JSONBuffer(int maxBufferSize)
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.public JSONBuffer(@Nullable ByteStringBuffer buffer, int maxBufferSize, boolean multiLine)
buffer
- The buffer to wrap. It may be null
if 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.public void clear()
public void setBuffer(@Nullable ByteStringBuffer buffer)
buffer
- The underlying buffer to which the JSON object data will be
written.public int length()
public void beginObject()
public void beginObject(@NotNull java.lang.String fieldName)
fieldName
- The name of the fieldpublic void endObject()
public void beginArray()
public void beginArray(@NotNull java.lang.String fieldName)
fieldName
- The name of the fieldpublic void endArray()
public void appendBoolean(boolean value)
value
- The Boolean value to append.public void appendBoolean(@NotNull java.lang.String fieldName, boolean value)
fieldName
- The name of the field.value
- The Boolean value.public void appendNull()
public void appendNull(@NotNull java.lang.String fieldName)
fieldName
- The name of the field.public void appendNumber(@NotNull java.math.BigDecimal value)
value
- The number to add.public void appendNumber(int value)
value
- The number to add.public void appendNumber(long value)
value
- The number to add.public void appendNumber(@NotNull java.lang.String value)
value
- The string representation of the number to add. It must be
properly formed.public void appendNumber(@NotNull java.lang.String fieldName, @NotNull java.math.BigDecimal value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(@NotNull java.lang.String fieldName, int value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(@NotNull java.lang.String fieldName, long value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(@NotNull java.lang.String fieldName, @NotNull java.lang.String value)
fieldName
- The name of the field.value
- The string representation of the number ot add. It must
be properly formed.public void appendString(@NotNull java.lang.String value)
value
- The value to add.public void appendString(@NotNull java.lang.String fieldName, @NotNull java.lang.String value)
fieldName
- The name of the field.value
- The value to add.public void appendValue(@NotNull JSONValue value)
value
- The value to append.public void appendValue(@NotNull java.lang.String fieldName, @NotNull JSONValue value)
fieldName
- The name of the field.value
- The value to append.public void appendField(@NotNull JSONField field)
field
- The JSON field to be appended.@NotNull public ByteStringBuffer getBuffer()
public void writeTo(@NotNull java.io.OutputStream outputStream) throws java.io.IOException
outputStream
- The output stream to which the current contents of
this JSON buffer should be written.java.io.IOException
- If a problem is encountered while writing to the
provided output stream.@NotNull public java.lang.String toString()
toString
in class java.lang.Object
@NotNull public JSONObject toJSONObject() throws JSONException
JSONException
- If the buffer does not currently contain exactly
one valid JSON object.