Android Open Source - JsonPullParser Jpp Generator






From Project

Back to project page JsonPullParser.

License

The source code is released under:

Apache License

If you think the Android project JsonPullParser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.vvakame.util.jsonpullparser.googleapiclient;
// ww  w  .  ja  va2  s.co m
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Deque;

import net.vvakame.util.jsonpullparser.JsonPullParser.State;
import net.vvakame.util.jsonpullparser.util.JsonUtil;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonGenerator;

/**
 * Implementation for {@link JsonGenerator} by JsonPullParser.
 * @author vvakame
 */
public class JppGenerator extends JsonGenerator {

  final JppFactory factory;

  final Writer writer;


  /**
   * the constructor.
   * @param factory
   * @param out
   * @param enc
   * @category constructor
   */
  public JppGenerator(JppFactory factory, OutputStream out, Charset enc) {
    this.factory = factory;
    this.writer = new OutputStreamWriter(out, enc);
  }

  /**
   * the constructor.
   * @param factory
   * @param writer
   * @category constructor
   */
  public JppGenerator(JppFactory factory, Writer writer) {
    this.factory = factory;
    this.writer = writer;
  }

  @Override
  public JsonFactory getFactory() {
    return factory;
  }


  // START_ARRAY or START_HASH
  Deque<State> stateList = new ArrayDeque<State>();

  Deque<Boolean> firstList = new ArrayDeque<Boolean>();


  /**
   * inc depth
   * @param state
   * @author vvakame
   */
  void push(State state) {
    stateList.addLast(state);
    firstList.addLast(true);
  }

  /**
   * dec depth
   * @author vvakame
   */
  void pop() {
    stateList.removeLast();
    firstList.removeLast();
  }

  void eat(boolean key) throws IOException {
    if (firstList.isEmpty()) {
      return;
    }
    boolean value = firstList.getLast();
    if (value) {
      firstList.removeLast();
      firstList.addLast(false);
    } else if (isInArray()) {
      JsonUtil.addSeparator(writer);
    } else if (isInObject() && key) {
      JsonUtil.addSeparator(writer);
    }
  }

  boolean isInArray() {
    return stateList.getLast() == State.START_ARRAY;
  }

  boolean isInObject() {
    return stateList.getLast() == State.START_HASH;
  }

  @Override
  public void writeStartArray() throws IOException {
    eat(false);
    JsonUtil.startArray(writer);
    push(State.START_ARRAY);
  }

  @Override
  public void writeEndArray() throws IOException {
    JsonUtil.endArray(writer);
    pop();
  }

  @Override
  public void writeStartObject() throws IOException {
    eat(false);
    JsonUtil.startHash(writer);
    push(State.START_HASH);
  }

  @Override
  public void writeEndObject() throws IOException {
    JsonUtil.endHash(writer);
    pop();
  }

  @Override
  public void writeFieldName(String name) throws IOException {
    eat(true);
    JsonUtil.putKey(writer, name);
  }

  @Override
  public void writeNull() throws IOException {
    eat(false);
    JsonUtil.put(writer, (Object) null);
  }

  @Override
  public void writeString(String value) throws IOException {
    eat(false);
    JsonUtil.put(writer, value);
  }

  @Override
  public void writeBoolean(boolean state) throws IOException {
    eat(false);
    JsonUtil.put(writer, state);
  }

  @Override
  public void writeNumber(int v) throws IOException {
    eat(false);
    JsonUtil.put(writer, v);
  }

  @Override
  public void writeNumber(long v) throws IOException {
    JsonUtil.put(writer, v);
  }

  @Override
  public void writeNumber(BigInteger v) throws IOException {
    eat(false);
    writer.write(v.toString());
  }

  @Override
  public void writeNumber(float v) throws IOException {
    eat(false);
    JsonUtil.put(writer, v);
  }

  @Override
  public void writeNumber(double v) throws IOException {
    eat(false);
    JsonUtil.put(writer, v);
  }

  @Override
  public void writeNumber(BigDecimal v) throws IOException {
    eat(false);
    writer.write(v.toString());
  }

  @Override
  public void writeNumber(String encodedValue) throws IOException {
    eat(false);
    writer.write(encodedValue);
  }

  @Override
  public void flush() throws IOException {
    writer.flush();
  }

  @Override
  public void close() throws IOException {
    writer.close();
  }
}




Java Source Code List

net.vvakame.apt.AptUtil.java
net.vvakame.jsonpullparser.android.sample.MainActivity.java
net.vvakame.sample.BaseData.java
net.vvakame.sample.BuilderData.java
net.vvakame.sample.ComplexData2.java
net.vvakame.sample.ComplexData.java
net.vvakame.sample.ContainsAnotherPackageObjectData.java
net.vvakame.sample.ConverterData.java
net.vvakame.sample.ExtendsData1.java
net.vvakame.sample.ExtendsData2.java
net.vvakame.sample.ForInnerClassData.java
net.vvakame.sample.GenToPackagePrivateData.java
net.vvakame.sample.JsonMetaToPackagePrivateValidData.java
net.vvakame.sample.MiniData.java
net.vvakame.sample.NestDepth1Data.java
net.vvakame.sample.NestDepth2Data.java
net.vvakame.sample.NestParentData.java
net.vvakame.sample.PrimitiveTypeData.java
net.vvakame.sample.PrimitiveWrapperData.java
net.vvakame.sample.PrimitiveWrapperListData.java
net.vvakame.sample.SampleEnum.java
net.vvakame.sample.SortOrderData1.java
net.vvakame.sample.SortOrderData2.java
net.vvakame.sample.StoreJsonData1.java
net.vvakame.sample.StoreJsonData2.java
net.vvakame.sample.TestData.java
net.vvakame.sample.anotherpackage.AnotherPackageClass.java
net.vvakame.sample.converter.IntFlattenConverter.java
net.vvakame.sample.converter.StringDiscardConverter.java
net.vvakame.sample.duma.ItemMapConverter.java
net.vvakame.sample.duma.Item.java
net.vvakame.sample.duma.ReadItLater.java
net.vvakame.sample.issue25.Issue25.java
net.vvakame.sample.issue28.Issue28.java
net.vvakame.sample.issue2.Child0ValueP0.java
net.vvakame.sample.issue2.Child0ValueP1.java
net.vvakame.sample.issue2.Child1ValueP0.java
net.vvakame.sample.issue2.Child1ValueP1.java
net.vvakame.sample.issue2.Grandchild0ValueC0P0.java
net.vvakame.sample.issue2.Grandchild0ValueC0P1.java
net.vvakame.sample.issue2.Grandchild0ValueC1P0.java
net.vvakame.sample.issue2.Grandchild0ValueC1P1.java
net.vvakame.sample.issue2.Grandchild1ValueC0P0.java
net.vvakame.sample.issue2.Grandchild1ValueC0P1.java
net.vvakame.sample.issue2.Grandchild1ValueC1P0.java
net.vvakame.sample.issue2.Grandchild1ValueC1P1.java
net.vvakame.sample.issue2.Parent0Value.java
net.vvakame.sample.issue2.Parent1Value.java
net.vvakame.sample.issue30.PrimitiveList.java
net.vvakame.sample.issue30.RecursiveStructure.java
net.vvakame.sample.issue31.BaseData.java
net.vvakame.sample.issue31.ExtendData.java
net.vvakame.sample.twitter.Place.java
net.vvakame.sample.twitter.Place.java
net.vvakame.sample.twitter.Tweet.java
net.vvakame.sample.twitter.Tweet.java
net.vvakame.sample.twitter.User.java
net.vvakame.sample.twitter.User.java
net.vvakame.twitter.ResultTweet.java
net.vvakame.twitter.SearchResult.java
net.vvakame.util.jsonpullparser.JsonFormatException.java
net.vvakame.util.jsonpullparser.JsonPullParser.java
net.vvakame.util.jsonpullparser.JsonSlice.java
net.vvakame.util.jsonpullparser.Stack.java
net.vvakame.util.jsonpullparser.annotation.JsonKey.java
net.vvakame.util.jsonpullparser.annotation.JsonModel.java
net.vvakame.util.jsonpullparser.annotation.StoreJson.java
net.vvakame.util.jsonpullparser.builder.JsonCoderRouter.java
net.vvakame.util.jsonpullparser.builder.JsonModelBuilder.java
net.vvakame.util.jsonpullparser.builder.JsonModelCoder.java
net.vvakame.util.jsonpullparser.builder.JsonPropertyBuilderCreator.java
net.vvakame.util.jsonpullparser.builder.JsonPropertyBuilder.java
net.vvakame.util.jsonpullparser.builder.JsonPropertyCoder.java
net.vvakame.util.jsonpullparser.builder.JsonPropertyMeta.java
net.vvakame.util.jsonpullparser.factory.JsonAnnotationProcessor.java
net.vvakame.util.jsonpullparser.factory.JsonKeyModel.java
net.vvakame.util.jsonpullparser.factory.JsonModelGenerator.java
net.vvakame.util.jsonpullparser.factory.JsonModelModel.java
net.vvakame.util.jsonpullparser.factory.Log.java
net.vvakame.util.jsonpullparser.factory.StandardTypeKindVisitor.java
net.vvakame.util.jsonpullparser.factory.StoreJsonModel.java
net.vvakame.util.jsonpullparser.factory.template.MvelTemplate.java
net.vvakame.util.jsonpullparser.factory.template.Template.java
net.vvakame.util.jsonpullparser.googleapiclient.JppFactory.java
net.vvakame.util.jsonpullparser.googleapiclient.JppGenerator.java
net.vvakame.util.jsonpullparser.googleapiclient.JppParser.java
net.vvakame.util.jsonpullparser.util.JsonArray.java
net.vvakame.util.jsonpullparser.util.JsonHash.java
net.vvakame.util.jsonpullparser.util.JsonParseUtil.java
net.vvakame.util.jsonpullparser.util.JsonSliceUtil.java
net.vvakame.util.jsonpullparser.util.JsonUtil.java
net.vvakame.util.jsonpullparser.util.OnJsonObjectAddListener.java
net.vvakame.util.jsonpullparser.util.TokenConverter.java
net.vvakame.util.jsonpullparser.util.Type.java