public interface ObjectWriter
ObjectReader
) without the databind support that comes with the converters. This will be very close in terms
of performance as writing manually formated data to the stream.
If you want to write the array new int[1, 2, 3] to the stream with ObjectWriter:
writer.beginArray().writeValue(1).writeValue(2).writeValue(3).endArray();And to write Person (we simplify, in practice you must handle null values):
class Person {
public static void main(String[] args) {
// we will write from Person to json string
Person p = new Person();
p.name = "eugen";
p.age = 26;
p.childrenYearOfBirth = new ArrayList<Integer>();
StringWriter sw = new StringWriter();
ObjectWriter writer = new JsonWriter(sw);
p.write(writer);
writer.flush();
writer.close();
// will write {"name":"eugen","age":26,"childrenYearOfBirth":[]}
System.out.println(sw.toString());
}
String name;
int age;
List<Integer> childrenYearOfBirth;
public void write(ObjectWriter writer) {
writer.beginObject().writeName("name");
if (name == null) writer.writeNull();
else writer.writeValue(name)
writer.writeName("age").writeAge(age)
.writeName("childrenYearOfBirth");
if (childrenYearOfBirth == null) writer.writeNull();
else {
writer.beginArray();
for (Integer year : childrenYearOfBirth)
writer.writeValue(year);
writer.endArray()
}
writer.endObject();
}
}
Be careful if you instantiate ObjectWriter your self you are responsible of flushing and closing
the stream.JsonWriter,
ObjectReader,
JsonReader| Modifier and Type | Method and Description |
|---|---|
ObjectWriter |
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.
|
ObjectWriter |
beginObject()
Starts a object, objects are a suite of name/value pairs, values may be literals, arrays or
objects.
|
void |
close() |
ObjectWriter |
endArray()
Ends the array, if beginArray was not called, implementations should throw an exception.
|
ObjectWriter |
endObject()
Ends the object being written, if beginObject was not called an exception will be throwed.
|
void |
flush() |
ObjectWriter |
writeMetadata(String name,
String value)
Metadata is a suite of name/value pairs, names will be prepended with '@' (handled by the
library).
|
ObjectWriter |
writeName(String name)
Writes the name of a property.
|
ObjectWriter |
writeNull()
Must be called when a null value is encountered.
|
ObjectWriter |
writeUnsafeValue(String value)
Writes value as is without any pre-processing, it's faster than
writeValue(String)
but should be used only if you know that it is safe. |
ObjectWriter |
writeValue(boolean value)
See
writeValue(int). |
ObjectWriter |
writeValue(byte[] value)
Writes an array of bytes as a base64 encoded string.
|
ObjectWriter |
writeValue(double value)
See
writeValue(int). |
ObjectWriter |
writeValue(float value) |
ObjectWriter |
writeValue(int value)
Writes a value to the stream.
|
ObjectWriter |
writeValue(long value)
See
writeValue(int). |
ObjectWriter |
writeValue(Number value)
See
writeValue(int). |
ObjectWriter |
writeValue(short value)
See
writeValue(int). |
ObjectWriter |
writeValue(String value)
See
writeValue(int). |
ObjectWriter beginArray()
JsonStreamException - if trying to produce invalid jsonObjectWriter endArray()
JsonStreamException - if trying to produce invalid jsonObjectWriter beginObject()
JsonStreamException - if trying to produce invalid jsonObjectWriter endObject()
JsonStreamException - if trying to produce invalid jsonObjectWriter writeName(String name)
name - a non null StringJsonStreamException - if trying to produce invalid jsonObjectWriter writeValue(int value)
value - to write.JsonStreamException - if trying to produce invalid jsonObjectWriter writeValue(double value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(long value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(short value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(float value)
writeValue(int)ObjectWriter writeValue(boolean value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(Number value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(String value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeValue(byte[] value)
writeValue(int).JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeUnsafeValue(String value)
writeValue(String)
but should be used only if you know that it is safe.JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter writeNull()
JsonStreamException - if trying to produce invalid jsonwriteValue(int)ObjectWriter beginNextObjectMetadata()
JsonStreamExceptionwriteMetadata(String, String)ObjectWriter writeMetadata(String name, String value)
// 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 wan't 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();
name - of the metadata property, should not start with '@', the library will add it.value - of the metadata property.JsonStreamExceptionbeginNextObjectMetadata()void flush()
void close()
Copyright © 2014. All Rights Reserved.