Package org.apache.avro.io
Class JsonEncoder
java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.ParsingEncoder
org.apache.avro.io.JsonEncoder
- All Implemented Interfaces:
Flushable
,Parser.ActionHandler
An
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.-
Field Summary
Modifier and TypeFieldDescriptionprotected BitSet
Has anything been written into the collections?Fields inherited from class org.apache.avro.io.ParsingEncoder
pos
-
Method Summary
Modifier and TypeMethodDescriptionconfigure
(OutputStream out) Reconfigures this JsonEncoder to use the output stream provided.configure
(OutputStream out, boolean autoflush) Reconfigures this JsonEncoder to use the output stream provided.Handle the action symbol top when the input is sought to be taken off the stack.void
flush()
boolean
void
setIncludeNamespace
(boolean includeNamespace) void
Start a new item of an array or map.void
Call this method to finish writing an array.void
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
Call this method to terminate the inner-most, currently-opened map.void
Call this to start a new map.void
"Writes" a null value.void
writeString
(String str) Write a Unicode character string.void
writeString
(Utf8 utf8) Write a Unicode character string.Methods inherited from class org.apache.avro.io.ParsingEncoder
depth, pop, push, setItemCount
Methods inherited from class org.apache.avro.io.Encoder
writeBytes, writeFixed, writeFixed, writeString
-
Field Details
-
isEmpty
Has anything been written into the collections?
-
-
Method Details
-
flush
- Specified by:
flush
in interfaceFlushable
- Throws:
IOException
-
isIncludeNamespace
public boolean isIncludeNamespace() -
setIncludeNamespace
public void setIncludeNamespace(boolean includeNamespace) -
configure
Reconfigures this JsonEncoder to use the output stream provided. If the OutputStream provided is null, a NullPointerException is thrown. Otherwise, this JsonEncoder will flush its current output and then reconfigure its output to use a default UTF8 JsonGenerator that writes to the provided OutputStream.- Parameters:
out
- The OutputStream to direct output to. Cannot be null.- Returns:
- this JsonEncoder
- Throws:
IOException
NullPointerException
- ifout
isnull
-
configure
Reconfigures this JsonEncoder to use the output stream provided. If the OutputStream provided is null, a NullPointerException is thrown. Otherwise, this JsonEncoder will flush its current output and then reconfigure its output to use a default UTF8 JsonGenerator that writes to the provided OutputStream.- Parameters:
out
- The OutputStream to direct output to. Cannot be null.- Returns:
- this JsonEncoder
- Throws:
IOException
NullPointerException
- ifout
isnull
-
writeNull
Description copied from class:Encoder
"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)- Specified by:
writeNull
in classEncoder
- Throws:
IOException
-
writeBoolean
Description copied from class:Encoder
Write a boolean value.- Specified by:
writeBoolean
in classEncoder
- Throws:
IOException
-
writeInt
Description copied from class:Encoder
Writes a 32-bit integer.- Specified by:
writeInt
in classEncoder
- Throws:
IOException
-
writeLong
Description copied from class:Encoder
Write a 64-bit integer.- Specified by:
writeLong
in classEncoder
- Throws:
IOException
-
writeFloat
Description copied from class:Encoder
Write a float.- Specified by:
writeFloat
in classEncoder
- Throws:
IOException
-
writeDouble
Description copied from class:Encoder
Write a double.- Specified by:
writeDouble
in classEncoder
- Throws:
IOException
-
writeString
Description copied from class:Encoder
Write a Unicode character string.- Specified by:
writeString
in classEncoder
- Throws:
IOException
-
writeString
Description copied from class:Encoder
Write a Unicode character string. The default implementation converts the String to aUtf8
. Some Encoder implementations may want to do something different as a performance optimization.- Overrides:
writeString
in classEncoder
- Throws:
IOException
-
writeBytes
Description copied from class:Encoder
Write a byte string.- Specified by:
writeBytes
in classEncoder
- Throws:
IOException
-
writeBytes
Description copied from class:Encoder
Write a byte string.- Specified by:
writeBytes
in classEncoder
- Throws:
IOException
-
writeFixed
Description copied from class:Encoder
Writes a fixed size binary object.- Specified by:
writeFixed
in classEncoder
- Parameters:
bytes
- The contents to writestart
- The position within bytes where the contents start.len
- The number of bytes to write.- Throws:
IOException
-
writeEnum
Description copied from class:Encoder
Writes an enumeration.- Specified by:
writeEnum
in classEncoder
- Parameters:
e
-- Throws:
IOException
-
writeArrayStart
Description copied from class:Encoder
Call this method to start writing an array. When starting to serialize an array, callEncoder.writeArrayStart()
. Then, before writing any data for any item callEncoder.setItemCount(long)
followed by a sequence ofEncoder.startItem()
and the item itself. The number ofEncoder.startItem()
should match the number specified inEncoder.setItemCount(long)
. When actually writing the data of the item, you can call anyEncoder
method (e.g.,Encoder.writeLong(long)
). When all items of the array have been written, callEncoder.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();
- Specified by:
writeArrayStart
in classEncoder
- Throws:
IOException
-
writeArrayEnd
Description copied from class:Encoder
Call this method to finish writing an array. SeeEncoder.writeArrayStart()
for usage information.- Specified by:
writeArrayEnd
in classEncoder
- Throws:
IOException
-
writeMapStart
Description copied from class:Encoder
Call this to start a new map. SeeEncoder.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.Entryinvalid input: '<'String, Record> entry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
- Specified by:
writeMapStart
in classEncoder
- Throws:
IOException
-
writeMapEnd
Description copied from class:Encoder
Call this method to terminate the inner-most, currently-opened map. SeeEncoder.writeArrayStart()
for more details.- Specified by:
writeMapEnd
in classEncoder
- Throws:
IOException
-
startItem
Description copied from class:Encoder
Start a new item of an array or map. SeeEncoder.writeArrayStart()
for usage information.- Overrides:
startItem
in classParsingEncoder
- Throws:
IOException
-
writeIndex
Description copied from class:Encoder
Call this method to write the tag of a union. As an example of usage, let's say you want to write a union, whose second branch is a record consisting of an Long field and a Boolean field. Your code would look something like this:out.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
- Specified by:
writeIndex
in classEncoder
- Throws:
IOException
-
doAction
Description copied from interface:Parser.ActionHandler
Handle the action symbol top when the input is sought to be taken off the stack.- Specified by:
doAction
in interfaceParser.ActionHandler
- Parameters:
input
- The input symbol from the caller of advancetop
- The symbol at the top the stack.- Returns:
- null if advance() is to continue processing the stack. If not null the return value will be returned by advance().
- Throws:
IOException
-