org.apache.avro.io
Class JsonEncoder

java.lang.Object
  extended by org.apache.avro.io.Encoder
      extended by org.apache.avro.io.ParsingEncoder
          extended by org.apache.avro.io.JsonEncoder
All Implemented Interfaces:
Flushable, Parser.ActionHandler

public class JsonEncoder
extends ParsingEncoder
implements Parser.ActionHandler

An Encoder for Avro's JSON data encoding.


Field Summary
protected  BitSet isEmpty
          Has anything been written into the collections?
 
Fields inherited from class org.apache.avro.io.ParsingEncoder
pos
 
Constructor Summary
JsonEncoder(Schema sc, JsonGenerator out)
           
JsonEncoder(Schema sc, OutputStream out)
           
 
Method Summary
 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 init(OutputStream out)
          Redirect output (and reset the parser state if we're checking).
 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(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, writeString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isEmpty

protected BitSet isEmpty
Has anything been written into the collections?

Constructor Detail

JsonEncoder

public JsonEncoder(Schema sc,
                   OutputStream out)
            throws IOException
Throws:
IOException

JsonEncoder

public JsonEncoder(Schema sc,
                   JsonGenerator out)
            throws IOException
Throws:
IOException
Method Detail

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Throws:
IOException

init

public void init(OutputStream out)
          throws IOException
Description copied from class: Encoder
Redirect output (and reset the parser state if we're checking).

Specified by:
init in class Encoder
Throws:
IOException

writeNull

public void writeNull()
               throws IOException
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 class Encoder
Throws:
IOException

writeBoolean

public void writeBoolean(boolean b)
                  throws IOException
Description copied from class: Encoder
Write a boolean value.

Specified by:
writeBoolean in class Encoder
Throws:
IOException

writeInt

public void writeInt(int n)
              throws IOException
Description copied from class: Encoder
Writes a 32-bit integer.

Specified by:
writeInt in class Encoder
Throws:
IOException

writeLong

public void writeLong(long n)
               throws IOException
Description copied from class: Encoder
Write a 64-bit integer.

Specified by:
writeLong in class Encoder
Throws:
IOException

writeFloat

public void writeFloat(float f)
                throws IOException
Description copied from class: Encoder
Write a float.

Specified by:
writeFloat in class Encoder
Throws:
IOException

writeDouble

public void writeDouble(double d)
                 throws IOException
Description copied from class: Encoder
Write a double.

Specified by:
writeDouble in class Encoder
Throws:
IOException

writeString

public void writeString(Utf8 utf8)
                 throws IOException
Description copied from class: Encoder
Write a Unicode character string.

Specified by:
writeString in class Encoder
Throws:
IOException

writeBytes

public void writeBytes(ByteBuffer bytes)
                throws IOException
Description copied from class: Encoder
Write a byte string.

Specified by:
writeBytes in class Encoder
Throws:
IOException

writeBytes

public void writeBytes(byte[] bytes,
                       int start,
                       int len)
                throws IOException
Description copied from class: Encoder
Write a byte string.

Specified by:
writeBytes in class Encoder
Throws:
IOException

writeFixed

public void writeFixed(byte[] bytes,
                       int start,
                       int len)
                throws IOException
Description copied from class: Encoder
Writes a fixed size binary object.

Specified by:
writeFixed in class Encoder
Parameters:
bytes - The contents to write
start - The position within bytes where the contents start.
len - The number of bytes to write.
Throws:
IOException

writeEnum

public void writeEnum(int e)
               throws IOException
Description copied from class: Encoder
Writes an enumeration.

Specified by:
writeEnum in class Encoder
Throws:
IOException

writeArrayStart

public void writeArrayStart()
                     throws IOException
Description copied from class: Encoder
Call this method to start writing an array. When starting to serialize an array, call 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();
  

Specified by:
writeArrayStart in class Encoder
Throws:
IOException

writeArrayEnd

public void writeArrayEnd()
                   throws IOException
Description copied from class: Encoder
Call this method to finish writing an array. See Encoder.writeArrayStart() for usage information.

Specified by:
writeArrayEnd in class Encoder
Throws:
IOException

writeMapStart

public void writeMapStart()
                   throws IOException
Description copied from class: Encoder
Call this to start a new map. See 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.Entry 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 class Encoder
Throws:
IOException

writeMapEnd

public void writeMapEnd()
                 throws IOException
Description copied from class: Encoder
Call this method to terminate the inner-most, currently-opened map. See Encoder.writeArrayStart() for more details.

Specified by:
writeMapEnd in class Encoder
Throws:
IOException

startItem

public void startItem()
               throws IOException
Description copied from class: Encoder
Start a new item of an array or map. See Encoder.writeArrayStart() for usage information.

Overrides:
startItem in class ParsingEncoder
Throws:
IOException

writeIndex

public void writeIndex(int unionIndex)
                throws IOException
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 class Encoder
Throws:
IOException

doAction

public Symbol doAction(Symbol input,
                       Symbol top)
                throws IOException
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 interface Parser.ActionHandler
Parameters:
input - The input symbol from the caller of advance
top - 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


Copyright © 2010 The Apache Software Foundation