|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.unboundid.util.json.JSONBuffer
@Mutable @ThreadSafety(level=NOT_THREADSAFE) public final class JSONBuffer
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. 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.
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
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 |
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 the provided JSON 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
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(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.Method Detail |
---|
public void clear()
public void setBuffer(ByteStringBuffer buffer)
buffer
- The underlying buffer to which the JSON object data will be
written.public int length()
public void beginObject()
public void beginObject(java.lang.String fieldName)
fieldName
- The name of the fieldpublic void endObject()
public void beginArray()
public void beginArray(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(java.lang.String fieldName, boolean value)
fieldName
- The name of the field.value
- The Boolean value.public void appendNull()
public void appendNull(java.lang.String fieldName)
fieldName
- The name of the field.public void appendNumber(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(java.lang.String value)
value
- The string representation of the number to add. It must be
properly formed.public void appendNumber(java.lang.String fieldName, java.math.BigDecimal value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(java.lang.String fieldName, int value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(java.lang.String fieldName, long value)
fieldName
- The name of the field.value
- The number value.public void appendNumber(java.lang.String fieldName, 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(java.lang.String value)
value
- The value to add.public void appendString(java.lang.String fieldName, java.lang.String value)
fieldName
- The name of the field.value
- The value to add.public void appendValue(JSONValue value)
value
- The value to append.public void appendValue(java.lang.String fieldName, JSONValue value)
fieldName
- The name of the field.value
- The value to append.public ByteStringBuffer getBuffer()
public void writeTo(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.public java.lang.String toString()
toString
in class java.lang.Object
public JSONObject toJSONObject() throws JSONException
JSONException
- If the buffer does not currently contain exactly
one valid JSON object.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |