|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.dslplatform.json.JsonWriter
public final class JsonWriter
DslJson writes JSON into JsonWriter which has two primary modes of operation: * targeting specific output stream * buffering the entire response in memory In both cases JsonWriter writes into an byte[] buffer. If stream is used as target, it will copy buffer into the stream whenever there is no more room in buffer for new data. If stream is not used as target, it will grow the buffer to hold the encoded result. To use stream as target reset(OutputStream) must be called before processing. This class provides low level methods for JSON serialization.
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.
| Nested Class Summary | |
|---|---|
static interface |
JsonWriter.WriteObject<T>
Custom objects can be serialized based on the implementation specified through this interface. |
| Field Summary | |
|---|---|
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: : |
| Constructor Summary | |
|---|---|
JsonWriter()
Deprecated. |
|
| Method Summary | ||
|---|---|---|
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. |
|
|
serialize(Collection<T> collection,
JsonWriter.WriteObject<T> encoder)
Convenience method for serializing collection through instance serializer (WriteObject). |
|
|
serialize(List<T> list)
Convenience method for serializing list of JsonObject's. |
|
|
serialize(List<T> list,
JsonWriter.WriteObject<T> encoder)
Convenience method for serializing list through instance serializer (WriteObject). |
|
|
serialize(Map<K,V> map,
JsonWriter.WriteObject<K> keyEncoder,
JsonWriter.WriteObject<V> valueEncoder)
|
|
|
serialize(T[] array)
Convenience method for serializing array of JsonObject's. |
|
|
serialize(T[] array,
int len)
Convenience method for serializing only part of JsonObject's array. |
|
|
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. |
|
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. |
|
|
writeQuoted(JsonWriter.WriteObject<T> keyWriter,
T key)
|
|
void |
writeString(CharSequence value)
Write a quoted string into the JSON. |
|
void |
writeString(String value)
Write a quoted string into the JSON. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
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
| Constructor Detail |
|---|
@Deprecated public JsonWriter()
| Method Detail |
|---|
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 string
public 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 copy
public final void writeAscii(byte[] buf,
int len)
buf - byte buffer to copylen - part of buffer to copypublic final void writeBinary(byte[] value)
value - bytes to encodepublic String toString()
toString in class Objectpublic final byte[] toByteArray()
public final void toStream(OutputStream stream)
throws IOException
stream - target stream
IOException - 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()
@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 objects
public <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 objects
public <T> void serialize(@Nullable
T[] array,
JsonWriter.WriteObject<T> encoder)
T - type of objectarray - array to serializeencoder - instance serializer
public <T> void serialize(@Nullable
List<T> list,
JsonWriter.WriteObject<T> encoder)
T - type of objectlist - list to serializeencoder - instance serializer
public <T> void serialize(@Nullable
Collection<T> collection,
JsonWriter.WriteObject<T> encoder)
T - type of objectcollection - collection to serializeencoder - instance serializer
public <K,V> void serialize(@Nullable
Map<K,V> map,
JsonWriter.WriteObject<K> keyEncoder,
JsonWriter.WriteObject<V> valueEncoder)
public <T> void writeQuoted(JsonWriter.WriteObject<T> keyWriter,
T key)
public void serializeObject(@Nullable
Object value)
value - instance to serialize
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||