public class JsonWriter extends Object implements ObjectWriter
Constructor and Description |
---|
JsonWriter(Writer writer) |
JsonWriter(Writer writer,
boolean skipNull,
boolean htmlSafe,
boolean indentation) |
Modifier and Type | Method and Description |
---|---|
protected JsonWriter |
begin(JsonType jsonType,
char token) |
JsonWriter |
beginArray()
Starts to write an array (use it also for collections).
|
ObjectWriter |
beginNextObjectMetadata()
This method is a kind of cheat as it allows us to start writing metadata and then still be
able to call beginObject.
|
JsonWriter |
beginObject()
Starts a object, objects are a suite of name/value pairs, values may be literals, arrays or
objects.
|
void |
close() |
JsonType |
enclosingType() |
JsonWriter |
endArray()
Ends the array, if beginArray was not called, implementations should throw an exception.
|
JsonWriter |
endObject()
Ends the object being written, if beginObject was not called an exception will be throwed.
|
static char[] |
escapeString(String value) |
void |
flush() |
protected void |
indent() |
Writer |
unwrap() |
ObjectWriter |
writeBoolean(Boolean value) |
ObjectWriter |
writeBoolean(String name,
Boolean value) |
ObjectWriter |
writeBytes(byte[] value) |
ObjectWriter |
writeBytes(String name,
byte[] value) |
ObjectWriter |
writeEscapedName(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 int |
writeInt(long value) |
ObjectWriter |
writeMetadata(String name,
String value)
Metadata is a suite of name/value pairs, names will be prepended with '@' (handled by the
library).
|
JsonWriter |
writeName(String name)
Writes the name of a property.
|
ObjectWriter |
writeNull()
Must be called when a null value is encountered.
|
ObjectWriter |
writeNumber(Number value) |
ObjectWriter |
writeNumber(String name,
Number value) |
ObjectWriter |
writeString(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.
|
ObjectWriter |
writeString(String name,
String value)
Will write the name and the value, it is just a shortcut for writer.writeName("key").writeString(value).
|
JsonWriter |
writeUnsafeValue(String value)
Writes value as is without any pre-processing, it's faster than
ObjectWriter.writeValue(String)
but should be used only if you know that it is safe. |
JsonWriter |
writeValue(boolean value)
|
ObjectWriter |
writeValue(byte[] value)
Writes an array of bytes as a base64 encoded string.
|
JsonWriter |
writeValue(double value)
|
ObjectWriter |
writeValue(float value) |
JsonWriter |
writeValue(int value)
Writes a value to the stream.
|
JsonWriter |
writeValue(long value)
|
JsonWriter |
writeValue(Number value)
|
ObjectWriter |
writeValue(short value)
|
JsonWriter |
writeValue(String value)
|
public JsonWriter(Writer writer)
public JsonWriter(Writer writer, boolean skipNull, boolean htmlSafe, boolean indentation)
public JsonType enclosingType()
enclosingType
in interface ObjectWriter
public void close()
close
in interface ObjectWriter
public void flush()
flush
in interface ObjectWriter
public JsonWriter beginArray()
ObjectWriter
beginArray
in interface ObjectWriter
public JsonWriter beginObject()
ObjectWriter
beginObject
in interface ObjectWriter
protected final JsonWriter begin(JsonType jsonType, char token)
public JsonWriter endArray()
ObjectWriter
endArray
in interface ObjectWriter
public JsonWriter endObject()
ObjectWriter
endObject
in interface ObjectWriter
protected void indent()
public JsonWriter writeName(String name)
ObjectWriter
writeName
in interface ObjectWriter
name
- a non null Stringpublic ObjectWriter writeEscapedName(char[] name)
ObjectWriter
writeEscapedName
in interface ObjectWriter
name
- a non null escaped Stringpublic JsonWriter writeValue(int value)
ObjectWriter
writeValue
in interface ObjectWriter
value
- to write.public JsonWriter writeValue(double value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public JsonWriter writeValue(long value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public ObjectWriter writeValue(short value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public ObjectWriter writeValue(float value)
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public JsonWriter writeValue(boolean value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
protected final int writeInt(long value)
public JsonWriter writeValue(Number value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public ObjectWriter writeBoolean(Boolean value)
writeBoolean
in interface ObjectWriter
ObjectWriter.writeString(String)
public ObjectWriter writeNumber(Number value)
writeNumber
in interface ObjectWriter
ObjectWriter.writeString(String)
public ObjectWriter writeString(String value)
ObjectWriter
writeString
in interface ObjectWriter
public ObjectWriter writeBytes(byte[] value)
writeBytes
in interface ObjectWriter
ObjectWriter.writeString(String)
public ObjectWriter writeValue(byte[] value)
ObjectWriter
ObjectWriter.writeValue(int)
.writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public JsonWriter writeUnsafeValue(String value)
ObjectWriter
ObjectWriter.writeValue(String)
but should be used only if you know that it is safe.writeUnsafeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public JsonWriter writeValue(String value)
ObjectWriter
writeValue
in interface ObjectWriter
ObjectWriter.writeValue(int)
public static final char[] escapeString(String value)
public ObjectWriter writeNull()
ObjectWriter
writeNull
in interface ObjectWriter
ObjectWriter.writeValue(int)
public ObjectWriter beginNextObjectMetadata()
ObjectWriter
beginNextObjectMetadata
in interface ObjectWriter
ObjectWriter.writeMetadata(String, String)
public ObjectWriter writeMetadata(String name, String value)
ObjectWriter
// 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();
writeMetadata
in interface ObjectWriter
name
- of the metadata property, should not start with '@', the library will add it.value
- of the metadata property.ObjectWriter.beginNextObjectMetadata()
public ObjectWriter writeBoolean(String name, Boolean value)
writeBoolean
in interface ObjectWriter
ObjectWriter.writeString(String, String)
public ObjectWriter writeNumber(String name, Number value)
writeNumber
in interface ObjectWriter
ObjectWriter.writeString(String, String)
public ObjectWriter writeString(String name, String value)
ObjectWriter
writeString
in interface ObjectWriter
public ObjectWriter writeBytes(String name, byte[] value)
writeBytes
in interface ObjectWriter
ObjectWriter.writeString(String, String)
public Writer unwrap()
Copyright © 2019. All rights reserved.