Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.avro.io;
 
 
An Encoder for Avro's JSON data encoding.
 
 public class JsonEncoder extends ParsingEncoder implements Parser.ActionHandler {
   final Parser parser;
   private JsonGenerator out;
  
Has anything been written into the collections?
 
   protected BitSet isEmpty = new BitSet();
 
   public JsonEncoder(Schema scOutputStream outthrows IOException {
     this(scgetJsonGenerator(out));
   }
 
   public JsonEncoder(Schema scJsonGenerator outthrows IOException {
     this. = out;
     this. =
       new Parser(new JsonGrammarGenerator().generate(sc), this);
   }
 
   @Override
   public void flush() throws IOException {
     if ( != null) {
       .flush();
     }
   }
 
   @Override
   public void init(OutputStream outthrows IOException {
     flush();
     this. = getJsonGenerator(out);
   }
 
   private static JsonGenerator getJsonGenerator(OutputStream out)
     throws IOException {
     return out == null ? null :
       new JsonFactory().createJsonGenerator(out.);
   }
 
   @Override
   public void writeNull() throws IOException {
     .writeNull();
   }
 
   @Override
   public void writeBoolean(boolean bthrows IOException {
     .writeBoolean(b);
   }
 
   @Override
   public void writeInt(int nthrows IOException {
     .writeNumber(n);
   }
 
   @Override
   public void writeLong(long nthrows IOException {
     .writeNumber(n);
   }
 
   @Override
   public void writeFloat(float fthrows IOException {
    .writeNumber(f);
  }
  public void writeDouble(double dthrows IOException {
    .writeNumber(d);
  }
  public void writeString(Utf8 utf8throws IOException {
    if (.topSymbol() == .) {
      .writeFieldName(utf8.toString());
    } else {
      .writeString(utf8.toString());
    }
  }
  public void writeBytes(ByteBuffer bytesthrows IOException {
    if (bytes.hasArray()) {
      writeBytes(bytes.array(), bytes.position(), bytes.remaining());
    } else {
      byte[] b = new byte[bytes.remaining()];
      for (int i = 0; i < b.lengthi++) {
        b[i] = bytes.get();
      }
      writeBytes(b);
    }
  }
  public void writeBytes(byte[] bytesint startint lenthrows IOException {
    writeByteArray(bytesstartlen);
  }
  private void writeByteArray(byte[] bytesint startint len)
    throws IOException {
        new String(bytesstartlen.));
  }
  public void writeFixed(byte[] bytesint startint lenthrows IOException {
    if (len != top.size) {
      throw new AvroTypeException(
        "Incorrect length for fixed binary: expected " +
        top.size + " but received " + len + " bytes.");
    }
    writeByteArray(bytesstartlen);
  }
  public void writeEnum(int ethrows IOException {
    if (e < 0 || e >= top.size) {
      throw new AvroTypeException(
          "Enumeration out of range: max is " +
          top.size + " but received " + e);
    }
    .writeString(top.getLabel(e));
  }
  public void writeArrayStart() throws IOException {
    push();
    .set(depth());
  }
  public void writeArrayEnd() throws IOException {
    if (! .get()) {
    }
    pop();
  }
  public void writeMapStart() throws IOException {
    push();
    .set(depth());
  }
  public void writeMapEnd() throws IOException {
    if (! .get()) {
    }
    pop();
  }
  public void startItem() throws IOException {
    if (! .get()) {
    }
    super.startItem();
    .clear(depth());
  }
  public void writeIndex(int unionIndexthrows IOException {
    Symbol symbol = top.getSymbol(unionIndex);
    if (symbol != .) {
      .writeStartObject();
      .writeFieldName(top.getLabel(unionIndex));
    }
    .pushSymbol(symbol);
  }
  public Symbol doAction(Symbol inputSymbol topthrows IOException {
    if (top instanceof Symbol.FieldAdjustAction) {
      .writeFieldName(fa.fname);
    } else if (top == .) {
      .writeStartObject();
    } else if (top == . || top == .) {
      .writeEndObject();
    } else {
      throw new AvroTypeException("Unknown action symbol " + top);
    }
    return null;
  }
New to GrepCode? Check out our FAQ X