Class JsonWriter
- java.lang.Object
-
- com.oracle.coherence.io.json.genson.stream.JsonWriter
-
- All Implemented Interfaces:
ObjectWriter
public class JsonWriter extends Object implements ObjectWriter
-
-
Constructor Summary
Constructors Constructor Description JsonWriter(Writer writer)JsonWriter(Writer writer, boolean skipNull, boolean htmlSafe, boolean indentation)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected JsonWriterbegin(JsonType jsonType, char token)JsonWriterbeginArray()Starts to write an array (use it also for collections).ObjectWriterbeginNextObjectMetadata()This method is a kind of cheat as it allows us to start writing metadata and then still be able to call beginObject.JsonWriterbeginObject()Starts a object, objects are a suite of name/value pairs, values may be literals, arrays or objects.voidclearMetadata()voidclose()JsonTypeenclosingType()JsonWriterendArray()Ends the array, if beginArray was not called, implementations should throw an exception.JsonWriterendObject()Ends the object being written, if beginObject was not called an exception will be throwed.static char[]escapeString(String value)voidflush()protected voidindent()Writerunwrap()ObjectWriterwriteBoolean(Boolean value)ObjectWriterwriteBoolean(String name, Boolean value)ObjectWriterwriteBytes(byte[] value)ObjectWriterwriteBytes(String name, byte[] value)ObjectWriterwriteEscapedName(char[] name)Will write the name without escaping special characters, assuming it has been done by the caller or the string doesn't contain any character needing to be escaped.protected intwriteInt(long value)ObjectWriterwriteMetadata(String name, Boolean value)ObjectWriterwriteMetadata(String name, Long value)ObjectWriterwriteMetadata(String name, String value)Metadata is a suite of name/value pairs, names will be prepended with '@' (handled by the library).JsonWriterwriteName(String name)Writes the name of a property.ObjectWriterwriteNull()Must be called when a null value is encountered.ObjectWriterwriteNumber(Number value)ObjectWriterwriteNumber(String name, Number value)ObjectWriterwriteString(String value)Similar to writeValue(String) but is null safe, meaning that if the value is null, then the write will call writeNull for you.ObjectWriterwriteString(String name, String value)Will write the name and the value, it is just a shortcut for writer.writeName("key").writeString(value).JsonWriterwriteUnsafeValue(String value)Writes value as is without any pre-processing, it's faster thanObjectWriter.writeValue(String)but should be used only if you know that it is safe.JsonWriterwriteValue(boolean value)ObjectWriterwriteValue(byte[] value)Writes an array of bytes as a base64 encoded string.JsonWriterwriteValue(double value)ObjectWriterwriteValue(float value)JsonWriterwriteValue(int value)Writes a value to the stream.JsonWriterwriteValue(long value)ObjectWriterwriteValue(short value)JsonWriterwriteValue(Number value)JsonWriterwriteValue(String value)
-
-
-
Method Detail
-
enclosingType
public JsonType enclosingType()
- Specified by:
enclosingTypein interfaceObjectWriter
-
close
public void close()
- Specified by:
closein interfaceObjectWriter
-
flush
public void flush()
- Specified by:
flushin interfaceObjectWriter
-
beginArray
public JsonWriter beginArray()
Description copied from interface:ObjectWriterStarts to write an array (use it also for collections). An array is a suite of values that may be literals, arrays or objects. When you finished writing the values don't forget to call endArray().- Specified by:
beginArrayin interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
-
beginObject
public JsonWriter beginObject()
Description copied from interface:ObjectWriterStarts a object, objects are a suite of name/value pairs, values may be literals, arrays or objects. Don't forget to call endObject.- Specified by:
beginObjectin interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
-
begin
protected final JsonWriter begin(JsonType jsonType, char token)
-
endArray
public JsonWriter endArray()
Description copied from interface:ObjectWriterEnds the array, if beginArray was not called, implementations should throw an exception.- Specified by:
endArrayin interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
-
endObject
public JsonWriter endObject()
Description copied from interface:ObjectWriterEnds the object being written, if beginObject was not called an exception will be throwed.- Specified by:
endObjectin interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
-
clearMetadata
public final void clearMetadata()
-
indent
protected void indent()
-
writeName
public JsonWriter writeName(String name)
Description copied from interface:ObjectWriterWrites the name of a property. Names can be written only in objects and must be called before writing the properties value.- Specified by:
writeNamein interfaceObjectWriter- Parameters:
name- a non null String- Returns:
- a reference to this, allowing to chain method calls.
-
writeEscapedName
public ObjectWriter writeEscapedName(char[] name)
Description copied from interface:ObjectWriterWill write the name without escaping special characters, assuming it has been done by the caller or the string doesn't contain any character needing to be escaped.- Specified by:
writeEscapedNamein interfaceObjectWriter- Parameters:
name- a non null escaped String- Returns:
- a reference to this, allowing to chain method calls.
-
writeValue
public JsonWriter writeValue(int value)
Description copied from interface:ObjectWriterWrites a value to the stream. Values can be written in arrays and in objects (after writing the name).- Specified by:
writeValuein interfaceObjectWriter- Parameters:
value- to write.- Returns:
- a reference to this, allowing to chain method calls.
-
writeValue
public JsonWriter writeValue(double value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeValue
public JsonWriter writeValue(long value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeValue
public ObjectWriter writeValue(short value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeValue
public ObjectWriter writeValue(float value)
- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeValue
public JsonWriter writeValue(boolean value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeInt
protected final int writeInt(long value)
-
writeValue
public JsonWriter writeValue(Number value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeBoolean
public ObjectWriter writeBoolean(Boolean value)
- Specified by:
writeBooleanin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String)
-
writeNumber
public ObjectWriter writeNumber(Number value)
- Specified by:
writeNumberin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String)
-
writeString
public ObjectWriter writeString(String value)
Description copied from interface:ObjectWriterSimilar to writeValue(String) but is null safe, meaning that if the value is null, then the write will call writeNull for you.- Specified by:
writeStringin interfaceObjectWriter
-
writeBytes
public ObjectWriter writeBytes(byte[] value)
- Specified by:
writeBytesin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String)
-
writeValue
public ObjectWriter writeValue(byte[] value)
Description copied from interface:ObjectWriterWrites an array of bytes as a base64 encoded string. SeeObjectWriter.writeValue(int).- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeUnsafeValue
public JsonWriter writeUnsafeValue(String value)
Description copied from interface:ObjectWriterWrites value as is without any pre-processing, it's faster thanObjectWriter.writeValue(String)but should be used only if you know that it is safe.- Specified by:
writeUnsafeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
writeValue
public JsonWriter writeValue(String value)
Description copied from interface:ObjectWriter- Specified by:
writeValuein interfaceObjectWriter- See Also:
ObjectWriter.writeValue(int)
-
escapeString
public static final char[] escapeString(String value)
-
writeNull
public ObjectWriter writeNull()
Description copied from interface:ObjectWriterMust be called when a null value is encountered. Implementations will deal with the null representation (just skip it or write null, etc).- Specified by:
writeNullin interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
- See Also:
ObjectWriter.writeValue(int)
-
beginNextObjectMetadata
public ObjectWriter beginNextObjectMetadata()
Description copied from interface:ObjectWriterThis method is a kind of cheat as it allows us to start writing metadata and then still be able to call beginObject. This is mainly intended to be used in wrapped converters that want to handle a part of the serialization and then let the chain continue. Have a look at ClassMetadataConverter- Specified by:
beginNextObjectMetadatain interfaceObjectWriter- Returns:
- a reference to this allowing to chain method calls.
- See Also:
ObjectWriter.writeMetadata(String, String)
-
writeMetadata
public ObjectWriter writeMetadata(String name, String value)
Description copied from interface:ObjectWriterMetadata is a suite of name/value pairs, names will be prepended with '@' (handled by the library). Metadata feature is experimental for the moment so things may change a bit. The signature will not, but the way it is implemented could... Actually the contract is that metadata must be written first and only in objects. If it does not respect these conditions ObjectReader won't be able to detect it as metadata. Here is an example of two ways to write object metadata.// here it is transparent for the library if you write metadata or something else, you must call // beginObject before being able to start writing its metadata. writer.beginObject().writeMetadata("doc", "Object documentation bla bla...").writeName("name") .writeNull().endObject().flush(); // previous example works fine, but if you want to write some metadata and still be able to call // beginObject (for example in a converter you want to write some metadata and have all the existing // continue to work with calling beginObject) you must use beginNextObjectMetadata. // written from a first converter writer.beginNextObjectMetadata().writeMetadata("dataFromConverter1", "writtenFromConverter1"); // written from a second converter after first one writer.beginNextObjectMetadata().writeMetadata("dataFromConverter2", "writtenFromConverter2"); // finally concrete properties will be written from a custom converter writer.beginObject().writeName("name").writeNull().endObject().flush();- Specified by:
writeMetadatain interfaceObjectWriter- Parameters:
name- of the metadata property, should not start with '@', the library will add it.value- of the metadata property.- Returns:
- a reference to this allowing to chain method calls.
- See Also:
ObjectWriter.beginNextObjectMetadata()
-
writeMetadata
public ObjectWriter writeMetadata(String name, Long value)
- Specified by:
writeMetadatain interfaceObjectWriter- See Also:
ObjectWriter.writeMetadata(String, String)
-
writeMetadata
public ObjectWriter writeMetadata(String name, Boolean value)
- Specified by:
writeMetadatain interfaceObjectWriter- See Also:
ObjectWriter.writeMetadata(String, String)
-
writeBoolean
public ObjectWriter writeBoolean(String name, Boolean value)
- Specified by:
writeBooleanin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String, String)
-
writeNumber
public ObjectWriter writeNumber(String name, Number value)
- Specified by:
writeNumberin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String, String)
-
writeString
public ObjectWriter writeString(String name, String value)
Description copied from interface:ObjectWriterWill write the name and the value, it is just a shortcut for writer.writeName("key").writeString(value). Note if the value is null, writeNull is used.- Specified by:
writeStringin interfaceObjectWriter
-
writeBytes
public ObjectWriter writeBytes(String name, byte[] value)
- Specified by:
writeBytesin interfaceObjectWriter- See Also:
ObjectWriter.writeString(String, String)
-
unwrap
public Writer unwrap()
-
-