Top Description Fields Constructors Methods
org.apache.avro.io

public Class JsonEncoder

extends ParsingEncoder
implements ActionHandler
Class Inheritance
All Implemented Interfaces
org.apache.avro.io.parsing.Parser.ActionHandler
Imports
java.io.IOException, .OutputStream, java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.util.BitSet, .Objects, org.apache.avro.AvroTypeException, .Schema, org.apache.avro.io.parsing.JsonGrammarGenerator, .Parser, .Symbol, org.apache.avro.util.Utf8, com.fasterxml.jackson.core.JsonEncoding, .JsonFactory, .JsonGenerator, com.fasterxml.jackson.core.util.DefaultPrettyPrinter, .MinimalPrettyPrinter

An Encoder for Avro's JSON data encoding.

Construct using EncoderFactory.

JsonEncoder buffers output, and data may not appear on the output until Encoder#flush() is called.

JsonEncoder is not thread-safe.

Field Summary

Modifier and TypeField and Description
private boolean
protected BitSet
isEmpty

Has anything been written into the collections?

private static final String
private JsonGenerator
pack-priv final Parser
Inherited from org.apache.avro.io.ParsingEncoder:
pos

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
JsonEncoder(Schema sc, OutputStream out, boolean pretty)

pack-priv

Method Summary

Modifier and TypeMethod and Description
public JsonEncoder

Returns:

this JsonEncoder
configure
(OutputStream
The OutputStream to direct output to. Cannot be null.
out
)

Reconfigures this JsonEncoder to use the output stream provided.

private JsonEncoder

Returns:

this JsonEncoder
configure
(JsonGenerator
The JsonGenerator to direct output to. Cannot be null.
generator
)

Reconfigures this JsonEncoder to output to the JsonGenerator provided.

public Symbol
doAction(Symbol
The input symbol from the caller of advance
input
,
Symbol
The symbol at the top the stack.
top
)

Implements org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Handle the action symbol top when the input is sought to be taken off the stack.
public void
flush()

Implements java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.
private static JsonGenerator
getJsonGenerator(OutputStream out, boolean pretty)

public boolean
public void
setIncludeNamespace(final boolean includeNamespace)

public void
startItem()

Overrides org.apache.avro.io.ParsingEncoder.startItem.

Start a new item of an array or map.
public void
writeArrayEnd()

Implements abstract org.apache.avro.io.Encoder.writeArrayEnd.

Call this method to finish writing an array.
public void
writeArrayStart()

Implements abstract org.apache.avro.io.Encoder.writeArrayStart.

Call this method to start writing an array.
public void
writeBoolean(boolean b)

Implements abstract org.apache.avro.io.Encoder.writeBoolean.

Write a boolean value.
private void
writeByteArray(byte[] bytes, int start, int len)

public void
writeBytes(ByteBuffer bytes)

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Write a byte string.
public void
writeBytes(byte[] bytes, int start, int len)

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Write a byte string.
public void
writeDouble(double d)

Implements abstract org.apache.avro.io.Encoder.writeDouble.

Write a double.
public void
writeEnum(int e)

Implements abstract org.apache.avro.io.Encoder.writeEnum.

Writes an enumeration.
public void
writeFixed(byte[]
The contents to write
bytes
,
int
The position within bytes where the contents start.
start
,
int
The number of bytes to write.
len
)

Implements abstract org.apache.avro.io.Encoder.writeFixed.

Writes a fixed size binary object.
public void
writeFloat(float f)

Implements abstract org.apache.avro.io.Encoder.writeFloat.

Write a float.
public void
writeIndex(int unionIndex)

Implements abstract org.apache.avro.io.Encoder.writeIndex.

Call this method to write the tag of a union.
public void
writeInt(int n)

Implements abstract org.apache.avro.io.Encoder.writeInt.

Writes a 32-bit integer.
public void
writeLong(long n)

Implements abstract org.apache.avro.io.Encoder.writeLong.

Write a 64-bit integer.
public void
writeMapEnd()

Implements abstract org.apache.avro.io.Encoder.writeMapEnd.

Call this method to terminate the inner-most, currently-opened map.
public void
writeMapStart()

Implements abstract org.apache.avro.io.Encoder.writeMapStart.

Call this to start a new map.
public void
writeNull()

Implements abstract org.apache.avro.io.Encoder.writeNull.

"Writes" a null value.
public void
writeString(Utf8 utf8)

Implements abstract org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.
public void
writeString(String str)

Overrides org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.
Inherited from org.apache.avro.io.ParsingEncoder:
depthpoppushsetItemCount

Field Detail

includeNamespaceback to summary
private boolean includeNamespace
isEmptyback to summary
protected BitSet isEmpty

Has anything been written into the collections?

LINE_SEPARATORback to summary
private static final String LINE_SEPARATOR
outback to summary
private JsonGenerator out
parserback to summary
pack-priv final Parser parser

Constructor Detail

JsonEncoderback to summary
pack-priv JsonEncoder(Schema sc, OutputStream out) throws IOException
JsonEncoderback to summary
pack-priv JsonEncoder(Schema sc, OutputStream out, boolean pretty) throws IOException
JsonEncoderback to summary
pack-priv JsonEncoder(Schema sc, JsonGenerator out) throws IOException

Method Detail

configureback to summary
public JsonEncoder configure(OutputStream out) throws IOException

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:OutputStream

The OutputStream to direct output to. Cannot be null.

Returns:JsonEncoder

this JsonEncoder

Exceptions
NullPointerException:
if out is null
configureback to summary
private JsonEncoder configure(JsonGenerator generator) throws IOException

Reconfigures this JsonEncoder to output to the JsonGenerator provided.

If the JsonGenerator provided is null, a NullPointerException is thrown.

Otherwise, this JsonEncoder will flush its current output and then reconfigure its output to use the provided JsonGenerator.

Parameters
generator:JsonGenerator

The JsonGenerator to direct output to. Cannot be null.

Returns:JsonEncoder

this JsonEncoder

Exceptions
NullPointerException:
if generator is null
doActionback to summary
public Symbol doAction(Symbol input, Symbol top) throws IOException

Implements org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Doc from org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Handle the action symbol top when the input is sought to be taken off the stack.

Parameters
input:Symbol

The input symbol from the caller of advance

top:Symbol

The symbol at the top the stack.

Returns:Symbol

null if advance() is to continue processing the stack. If not null the return value will be returned by advance().

Annotations
@Override
flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Doc from java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getJsonGeneratorback to summary
private static JsonGenerator getJsonGenerator(OutputStream out, boolean pretty) throws IOException
isIncludeNamespaceback to summary
public boolean isIncludeNamespace()
setIncludeNamespaceback to summary
public void setIncludeNamespace(final boolean includeNamespace)
startItemback to summary
public void startItem() throws IOException

Overrides org.apache.avro.io.ParsingEncoder.startItem.

Doc from org.apache.avro.io.Encoder.startItem.

Start a new item of an array or map. See writeArrayStart for usage information.

Annotations
@Override
writeArrayEndback to summary
public void writeArrayEnd() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeArrayEnd.

Doc from org.apache.avro.io.Encoder.writeArrayEnd.

Call this method to finish writing an array. See writeArrayStart for usage information.

Annotations
@Override
writeArrayStartback to summary
public void writeArrayStart() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeArrayStart.

Doc from org.apache.avro.io.Encoder.writeArrayStart.

Call this method to start writing an array. When starting to serialize an array, call writeArrayStart. Then, before writing any data for any item call setItemCount followed by a sequence of startItem() and the item itself. The number of startItem() should match the number specified in setItemCount. When actually writing the data of the item, you can call any Encoder method (e.g., writeLong). When all items of the array have been written, call 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();
Annotations
@Override
writeBooleanback to summary
public void writeBoolean(boolean b) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBoolean.

Doc from org.apache.avro.io.Encoder.writeBoolean.

Write a boolean value.

Annotations
@Override
writeByteArrayback to summary
private void writeByteArray(byte[] bytes, int start, int len) throws IOException
writeBytesback to summary
public void writeBytes(ByteBuffer bytes) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Doc from org.apache.avro.io.Encoder.writeBytes.

Write a byte string.

Annotations
@Override
writeBytesback to summary
public void writeBytes(byte[] bytes, int start, int len) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Doc from org.apache.avro.io.Encoder.writeBytes.

Write a byte string.

Annotations
@Override
writeDoubleback to summary
public void writeDouble(double d) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeDouble.

Doc from org.apache.avro.io.Encoder.writeDouble.

Write a double.

Annotations
@Override
writeEnumback to summary
public void writeEnum(int e) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeEnum.

Doc from org.apache.avro.io.Encoder.writeEnum.

Writes an enumeration.

Annotations
@Override
writeFixedback to summary
public void writeFixed(byte[] bytes, int start, int len) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeFixed.

Doc from org.apache.avro.io.Encoder.writeFixed.

Writes a fixed size binary object.

Parameters
bytes:byte[]

The contents to write

start:int

The position within bytes where the contents start.

len:int

The number of bytes to write.

Annotations
@Override
writeFloatback to summary
public void writeFloat(float f) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeFloat.

Doc from org.apache.avro.io.Encoder.writeFloat.

Write a float.

Annotations
@Override
writeIndexback to summary
public void writeIndex(int unionIndex) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeIndex.

Doc from org.apache.avro.io.Encoder.writeIndex.

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);
Annotations
@Override
writeIntback to summary
public void writeInt(int n) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeInt.

Doc from org.apache.avro.io.Encoder.writeInt.

Writes a 32-bit integer.

Annotations
@Override
writeLongback to summary
public void writeLong(long n) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeLong.

Doc from org.apache.avro.io.Encoder.writeLong.

Write a 64-bit integer.

Annotations
@Override
writeMapEndback to summary
public void writeMapEnd() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeMapEnd.

Doc from org.apache.avro.io.Encoder.writeMapEnd.

Call this method to terminate the inner-most, currently-opened map. See writeArrayStart for more details.

Annotations
@Override
writeMapStartback to summary
public void writeMapStart() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeMapStart.

Doc from org.apache.avro.io.Encoder.writeMapStart.

Call this to start a new map. See 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.Entry entry : map.entrySet()) {
  out.startItem();
  out.writeString(entry.getKey());
  out.writeLong(entry.getValue().longField);
  out.writeBoolean(entry.getValue().boolField);
}
out.writeMapEnd();
Annotations
@Override
writeNullback to summary
public void writeNull() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeNull.

Doc from org.apache.avro.io.Encoder.writeNull.

"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)

Annotations
@Override
writeStringback to summary
public void writeString(Utf8 utf8) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeString.

Doc from org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.

Annotations
@Override
writeStringback to summary
public void writeString(String str) throws IOException

Overrides org.apache.avro.io.Encoder.writeString.

Doc from org.apache.avro.io.Encoder.writeString.

Write a Unicode character string. The default implementation converts the String to a org.apache.avro.util.Utf8. Some Encoder implementations may want to do something different as a performance optimization.

Annotations
@Override