Android Open Source - JsonPullParser Jpp Factory






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;
//from   w  ww  .ja v  a  2 s .  com
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;

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

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

/**
 * Implementation about {@link JsonFactory} by JsonPullParser.
 * @author vvakame
 */
public class JppFactory extends JsonFactory {

  @Override
  public JsonParser createJsonParser(InputStream in) {
    if (in == null) {
      throw new NullPointerException("in is required");
    }
    JsonPullParser parser = JsonPullParser.newParser(in);
    return new JppParser(this, parser);
  }

  @Override
  public JsonParser createJsonParser(InputStream in, Charset charset) {
    if (in == null) {
      throw new NullPointerException("in is required");
    }
    JsonPullParser parser = JsonPullParser.newParser(in, charset);
    return new JppParser(this, parser);
  }

  @Override
  public JsonParser createJsonParser(String value) {
    if (value == null) {
      throw new NullPointerException("value is required");
    }
    // vs invalid format JSON data.
    if (value.indexOf("{") == -1) {
      StubParser parser = adhocSolutions(value);
      if (parser != null) {
        return parser;
      }
    }

    JsonPullParser parser = JsonPullParser.newParser(value);
    return new JppParser(this, parser);
  }

  // adhoc solutions...
  // Why support for invalid JSON data at JsonFactory!? It's crazy!! >:(
  StubParser adhocSolutions(String value) {
    if (Boolean.toString(true).equals(value)) {
      return new StubParser(this) {

        @Override
        public JsonToken nextToken() {
          return JsonToken.VALUE_TRUE;
        }

        @Override
        public JsonToken getCurrentToken() {
          return JsonToken.VALUE_TRUE;
        }
      };
    } else if (Boolean.toString(false).equals(value)) {
      return new StubParser(this) {

        @Override
        public JsonToken nextToken() {
          return JsonToken.VALUE_FALSE;
        }

        @Override
        public JsonToken getCurrentToken() {
          return JsonToken.VALUE_FALSE;
        }
      };
    } else if ("null".equals(value)) {
      return new StubParser(this) {

        @Override
        public JsonToken nextToken() {
          return JsonToken.VALUE_NULL;
        }

        @Override
        public JsonToken getCurrentToken() {
          return JsonToken.VALUE_NULL;
        }
      };
    } else if (value.startsWith("\"") && value.endsWith("\"")) {
      final StringBuilder builder = new StringBuilder(value);
      builder.deleteCharAt(0);
      builder.setLength(builder.length() - 1);
      return new StubParser(this) {

        @Override
        public JsonToken nextToken() {
          return JsonToken.VALUE_STRING;
        }

        @Override
        public JsonToken getCurrentToken() {
          return JsonToken.VALUE_STRING;
        }

        @Override
        public String getText() {
          return builder.toString();
        }
      };
    } else {
      try {
        final BigDecimal decimal = new BigDecimal(value);
        return new StubParser(this) {

          @Override
          public JsonToken nextToken() {
            try {
              decimal.longValueExact();
              return JsonToken.VALUE_NUMBER_INT;
            } catch (ArithmeticException e) {
              return JsonToken.VALUE_NUMBER_FLOAT;
            }
          }

          @Override
          public JsonToken getCurrentToken() {
            try {
              decimal.longValueExact();
              return JsonToken.VALUE_NUMBER_INT;
            } catch (ArithmeticException e) {
              return JsonToken.VALUE_NUMBER_FLOAT;
            }
          }

          @Override
          public short getShortValue() {
            return decimal.shortValue();
          }

          @Override
          public int getIntValue() {
            return decimal.intValue();
          }

          @Override
          public float getFloatValue() {
            return decimal.floatValue();
          }

          @Override
          public long getLongValue() {
            return decimal.longValue();
          }

          @Override
          public double getDoubleValue() {
            return decimal.doubleValue();
          }

          @Override
          public BigInteger getBigIntegerValue() {
            return decimal.toBigInteger();
          }

          @Override
          public BigDecimal getDecimalValue() {
            return decimal;
          }
        };
      } catch (NumberFormatException e) {
      }
    }
    return null;
  }

  @Override
  public JsonParser createJsonParser(Reader reader) {
    if (reader == null) {
      throw new NullPointerException("reader is required");
    }
    JsonPullParser parser = JsonPullParser.newParser(reader);
    return new JppParser(this, parser);
  }

  @Override
  public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) {
    return new JppGenerator(this, out, enc);
  }

  @Override
  public JsonGenerator createJsonGenerator(Writer writer) {
    return new JppGenerator(this, writer);
  }

  static JsonToken convert(State state, JsonPullParser parser) {
    if (state == null) {
      return null;
    }
    switch (state) {
      case END_ARRAY:
        return JsonToken.END_ARRAY;
      case START_ARRAY:
        return JsonToken.START_ARRAY;
      case END_HASH:
        return JsonToken.END_OBJECT;
      case START_HASH:
        return JsonToken.START_OBJECT;
      case VALUE_BOOLEAN:
        if (parser.getValueBoolean()) {
          return JsonToken.VALUE_TRUE;
        } else {
          return JsonToken.VALUE_FALSE;
        }
      case VALUE_NULL:
        return JsonToken.VALUE_NULL;
      case VALUE_STRING:
        return JsonToken.VALUE_STRING;
      case VALUE_DOUBLE:
        return JsonToken.VALUE_NUMBER_FLOAT;
      case VALUE_LONG:
        return JsonToken.VALUE_NUMBER_INT;
      case KEY:
        return JsonToken.FIELD_NAME;
      default:
        return JsonToken.NOT_AVAILABLE;
    }
  }


  @SuppressWarnings("unused")
  static class StubParser extends JsonParser {

    JsonFactory factory;


    StubParser(JsonFactory factory) {
      this.factory = factory;
    }

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

    @Override
    public void close() throws IOException {
    }

    @Override
    public JsonToken nextToken() throws IOException {
      return null;
    }

    @Override
    public JsonToken getCurrentToken() {
      return null;
    }

    @Override
    public String getCurrentName() throws IOException {
      return null;
    }

    @Override
    public JsonParser skipChildren() throws IOException {
      return null;
    }

    @Override
    public String getText() throws IOException {
      return null;
    }

    @Override
    public byte getByteValue() throws IOException {
      return 0;
    }

    @Override
    public short getShortValue() throws IOException {
      return 0;
    }

    @Override
    public int getIntValue() throws IOException {
      return 0;
    }

    @Override
    public float getFloatValue() throws IOException {
      return 0;
    }

    @Override
    public long getLongValue() throws IOException {
      return 0;
    }

    @Override
    public double getDoubleValue() throws IOException {
      return 0;
    }

    @Override
    public BigInteger getBigIntegerValue() throws IOException {
      return null;
    }

    @Override
    public BigDecimal getDecimalValue() throws IOException {
      return null;
    }
  }
}




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