public final class JsonWriter extends Object
After the processing is done, in case then stream was used as target, flush() must be called to copy the remaining of the buffer into stream. When entire response was buffered in memory, buffer can be copied to stream or resulting byte[] can be used directly.
For maximum performance JsonWriter instances should be reused (to avoid allocation of new byte[] buffer instances). They should not be shared across threads (concurrently) so for Thread reuse it's best to use patterns such as ThreadLocal.
| Modifier and Type | Class and Description |
|---|---|
static interface |
JsonWriter.WriteObject<T>
Custom objects can be serialized based on the implementation specified through this interface.
|
| Modifier and Type | Field and Description |
|---|---|
static byte |
ARRAY_END
Helper for writing JSON array end: ]
|
static byte |
ARRAY_START
Helper for writing JSON array start: [
|
static byte |
COMMA
Helper for writing comma separator: ,
|
static byte |
ESCAPE
Helper for writing JSON escape: \\
|
static byte |
OBJECT_END
Helper for writing JSON object end: }
|
static byte |
OBJECT_START
Helper for writing JSON object start: {
|
static byte |
QUOTE
Helper for writing JSON quote: "
|
static byte |
SEMI
Helper for writing semicolon: :
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Deprecated.
|
void |
flush()
If stream was used, copies the buffer to stream and resets the position in buffer to 0.
|
long |
flushed()
Total bytes currently flushed to stream
|
byte[] |
getByteBuffer()
Current buffer.
|
void |
reset()
Resets the writer - same as calling reset(OutputStream = null)
|
void |
reset(OutputStream stream)
Resets the writer - specifies the target stream and sets the position in buffer to 0.
|
<T> void |
serialize(Collection<T> collection,
JsonWriter.WriteObject<T> encoder)
Convenience method for serializing collection through instance serializer (WriteObject).
|
<T extends JsonObject> |
serialize(List<T> list)
Convenience method for serializing list of JsonObject's.
|
<T> void |
serialize(List<T> list,
JsonWriter.WriteObject<T> encoder)
Convenience method for serializing list through instance serializer (WriteObject).
|
<K,V> void |
serialize(Map<K,V> map,
JsonWriter.WriteObject<K> keyEncoder,
JsonWriter.WriteObject<V> valueEncoder) |
<T extends JsonObject> |
serialize(T[] array)
Convenience method for serializing array of JsonObject's.
|
<T extends JsonObject> |
serialize(T[] array,
int len)
Convenience method for serializing only part of JsonObject's array.
|
<T> void |
serialize(T[] array,
JsonWriter.WriteObject<T> encoder)
Convenience method for serializing array through instance serializer (WriteObject).
|
void |
serializeObject(Object value)
Generic object serializer which is used for "unknown schema" objects.
|
void |
serializeRaw(Collection collection,
JsonWriter.WriteObject encoder) |
void |
serializeRaw(List list,
JsonWriter.WriteObject encoder) |
void |
serializeRaw(Map map,
JsonWriter.WriteObject keyEncoder,
JsonWriter.WriteObject valueEncoder) |
int |
size()
Current position in the buffer.
|
byte[] |
toByteArray()
Content of buffer can be copied to another array of appropriate size.
|
void |
toStream(OutputStream stream)
When JsonWriter does not target stream, this method should be used to copy content of the buffer into target stream.
|
String |
toString() |
void |
writeAscii(byte[] buf)
Copy bytes into JSON as is.
|
void |
writeAscii(byte[] buf,
int len)
Copy part of byte buffer into JSON as is.
|
void |
writeAscii(String value)
Write string consisting of only ascii characters.
|
void |
writeAscii(String value,
int len)
Write part of string consisting of only ascii characters.
|
void |
writeBinary(byte[] value)
Encode bytes as Base 64.
|
void |
writeByte(byte value)
Write a single byte into the JSON.
|
void |
writeNull()
Optimized method for writing 'null' into the JSON.
|
<T> void |
writeQuoted(JsonWriter.WriteObject<T> keyWriter,
T key) |
void |
writeRaw(byte[] buf,
int offset,
int len)
Copy part of byte buffer into JSON as is.
|
void |
writeString(CharSequence value)
Write a quoted string into the JSON.
|
void |
writeString(String value)
Write a quoted string into the JSON.
|
public static final byte OBJECT_START
public static final byte OBJECT_END
public static final byte ARRAY_START
public static final byte ARRAY_END
public static final byte COMMA
public static final byte SEMI
public static final byte QUOTE
public static final byte ESCAPE
public final void writeNull()
public final void writeByte(byte value)
value - byte to write into the JSONpublic final void writeString(String value)
value - string to writepublic final void writeString(CharSequence value)
value - char sequence to writepublic final void writeAscii(String value)
value - ascii stringpublic final void writeAscii(String value, int len)
value - ascii stringlen - part of the provided string to usepublic final void writeAscii(byte[] buf)
buf - byte buffer to copypublic final void writeAscii(byte[] buf,
int len)
buf - byte buffer to copylen - part of buffer to copypublic final void writeRaw(byte[] buf,
int offset,
int len)
buf - byte buffer to copyoffset - in buffer to start fromlen - part of buffer to copypublic final void writeBinary(byte[] value)
value - bytes to encodepublic final byte[] toByteArray()
public final void toStream(OutputStream stream) throws IOException
stream - target streamIOException - propagates from stream.writepublic final byte[] getByteBuffer()
public final int size()
public final long flushed()
public final void reset()
public final void reset(@Nullable OutputStream stream)
stream - sets/clears the target streampublic final void flush()
To reset the stream to null use reset() or reset(OutputStream) methods.
@Deprecated public void close() throws IOException
IOException - unable to write to target streampublic <T extends JsonObject> void serialize(T[] array)
T - type of objectsarray - input objectspublic <T extends JsonObject> void serialize(T[] array, int len)
T - type of objectsarray - input objectslen - size of array which should be serializedpublic <T extends JsonObject> void serialize(List<T> list)
T - type of objectslist - input objectspublic <T> void serialize(@Nullable T[] array, JsonWriter.WriteObject<T> encoder)
T - type of objectarray - array to serializeencoder - instance serializerpublic <T> void serialize(@Nullable List<T> list, JsonWriter.WriteObject<T> encoder)
T - type of objectlist - list to serializeencoder - instance serializerpublic void serializeRaw(@Nullable List list, JsonWriter.WriteObject encoder)
public <T> void serialize(@Nullable Collection<T> collection, JsonWriter.WriteObject<T> encoder)
T - type of objectcollection - collection to serializeencoder - instance serializerpublic void serializeRaw(@Nullable Collection collection, JsonWriter.WriteObject encoder)
public <K,V> void serialize(@Nullable Map<K,V> map, JsonWriter.WriteObject<K> keyEncoder, JsonWriter.WriteObject<V> valueEncoder)
public void serializeRaw(@Nullable Map map, JsonWriter.WriteObject keyEncoder, JsonWriter.WriteObject valueEncoder)
public <T> void writeQuoted(JsonWriter.WriteObject<T> keyWriter, T key)
public void serializeObject(@Nullable Object value)
value - instance to serializeCopyright © 2023. All rights reserved.