public class JsonEncoder extends ParsingEncoder implements Parser.ActionHandler
Encoder
for Avro's JSON data encoding.
Construct using EncoderFactory
.
JsonEncoder buffers output, and data may not appear on the output
until Flushable.flush()
is called.
JsonEncoder is not thread-safe.Modifier and Type | Field and Description |
---|---|
protected BitSet |
isEmpty
Has anything been written into the collections?
|
pos
Modifier and Type | Method and Description |
---|---|
JsonEncoder |
configure(JsonGenerator generator)
Reconfigures this JsonEncoder to output to the JsonGenerator provided.
|
JsonEncoder |
configure(OutputStream out)
Reconfigures this JsonEncoder to use the output stream provided.
|
Symbol |
doAction(Symbol input,
Symbol top)
Handle the action symbol top when the input is
sought to be taken off the stack.
|
void |
flush() |
void |
startItem()
Start a new item of an array or map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
Call this method to start writing an array.
|
void |
writeBoolean(boolean b)
Write a boolean value.
|
void |
writeBytes(byte[] bytes,
int start,
int len)
Write a byte string.
|
void |
writeBytes(ByteBuffer bytes)
Write a byte string.
|
void |
writeDouble(double d)
Write a double.
|
void |
writeEnum(int e)
Writes an enumeration.
|
void |
writeFixed(byte[] bytes,
int start,
int len)
Writes a fixed size binary object.
|
void |
writeFloat(float f)
Write a float.
|
void |
writeIndex(int unionIndex)
Call this method to write the tag of a union.
|
void |
writeInt(int n)
Writes a 32-bit integer.
|
void |
writeLong(long n)
Write a 64-bit integer.
|
void |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened
map.
|
void |
writeMapStart()
Call this to start a new map.
|
void |
writeNull()
"Writes" a null value.
|
void |
writeString(String str)
Write a Unicode character string.
|
void |
writeString(Utf8 utf8)
Write a Unicode character string.
|
depth, pop, push, setItemCount
writeBytes, writeFixed, writeFixed, writeString
protected BitSet isEmpty
public void flush() throws IOException
flush
in interface Flushable
IOException
public JsonEncoder configure(OutputStream out) throws IOException
out
- The OutputStream to direct output to. Cannot be null.IOException
public JsonEncoder configure(JsonGenerator generator) throws IOException
generator
- The JsonGenerator to direct output to. Cannot be null.IOException
public void writeNull() throws IOException
Encoder
writeNull
in class Encoder
IOException
public void writeBoolean(boolean b) throws IOException
Encoder
writeBoolean
in class Encoder
IOException
public void writeInt(int n) throws IOException
Encoder
writeInt
in class Encoder
IOException
public void writeLong(long n) throws IOException
Encoder
writeLong
in class Encoder
IOException
public void writeFloat(float f) throws IOException
Encoder
writeFloat
in class Encoder
IOException
public void writeDouble(double d) throws IOException
Encoder
writeDouble
in class Encoder
IOException
public void writeString(Utf8 utf8) throws IOException
Encoder
writeString
in class Encoder
IOException
public void writeString(String str) throws IOException
Encoder
Utf8
. Some Encoder
implementations may want to do something different as a performance optimization.writeString
in class Encoder
IOException
public void writeBytes(ByteBuffer bytes) throws IOException
Encoder
writeBytes
in class Encoder
IOException
public void writeBytes(byte[] bytes, int start, int len) throws IOException
Encoder
writeBytes
in class Encoder
IOException
public void writeFixed(byte[] bytes, int start, int len) throws IOException
Encoder
writeFixed
in class Encoder
bytes
- The contents to writestart
- The position within bytes where the contents
start.len
- The number of bytes to write.IOException
public void writeEnum(int e) throws IOException
Encoder
writeEnum
in class Encoder
IOException
public void writeArrayStart() throws IOException
Encoder
Encoder.writeArrayStart()
. Then, before writing any data for any item
call Encoder.setItemCount(long)
followed by a sequence of
Encoder.startItem()
and the item itself. The number of
Encoder.startItem()
should match the number specified in
Encoder.setItemCount(long)
.
When actually writing the data of the item, you can call any Encoder
method (e.g., Encoder.writeLong(long)
). When all items
of the array have been written, call Encoder.writeArrayEnd()
.
As an example, let's say you want to write an array of records,
the record consisting of an Long field and a Boolean field.
Your code would look something like this:
out.writeArrayStart(); out.setItemCount(list.size()); for (Record r : list) { out.startItem(); out.writeLong(r.longField); out.writeBoolean(r.boolField); } out.writeArrayEnd();
writeArrayStart
in class Encoder
IOException
public void writeArrayEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for usage information.writeArrayEnd
in class Encoder
IOException
public void writeMapStart() throws IOException
Encoder
Encoder.writeArrayStart()
for details on usage.
As an example of usage, let's say you want to write a map of
records, the record consisting of an Long field and a Boolean
field. Your code would look something like this:
out.writeMapStart(); out.setItemCount(list.size()); for (Map.Entryentry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
writeMapStart
in class Encoder
IOException
public void writeMapEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for more details.writeMapEnd
in class Encoder
IOException
public void startItem() throws IOException
Encoder
Encoder.writeArrayStart()
for usage information.startItem
in class ParsingEncoder
IOException
public void writeIndex(int unionIndex) throws IOException
Encoder
out.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
writeIndex
in class Encoder
IOException
public Symbol doAction(Symbol input, Symbol top) throws IOException
Parser.ActionHandler
doAction
in interface Parser.ActionHandler
input
- The input symbol from the caller of advancetop
- The symbol at the top the stack.IOException
Copyright © 2009-2012 The Apache Software Foundation. All Rights Reserved.