Android Open Source - JsonPullParser Json Parse Util






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

/*
 * Copyright 2011 vvakame <vvakame@gmail.com>
 */* w w  w  .  j a  va  2 s .  co m*/
 * Licensed 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 net.vvakame.util.jsonpullparser.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

/**
 * JSON parse facility.<br>
 * Generated codes make use of it too.
 * @author vvakame
 */
public class JsonParseUtil {

  /**
   * Parses the current token as an integer.
   * @param parser
   * @return {@link Integer}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Integer parserInteger(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_LONG) {
      return (int) parser.getValueLong();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a long.
   * @param parser
   * @return {@link Long}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Long parserLong(JsonPullParser parser) throws IOException, JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_LONG) {
      return parser.getValueLong();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a byte.
   * @param parser
   * @return {@link Byte}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Byte parserByte(JsonPullParser parser) throws IOException, JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_LONG) {
      return (byte) parser.getValueLong();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a short.
   * @param parser
   * @return {@link Short}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Short parserShort(JsonPullParser parser) throws IOException, JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_LONG) {
      return (short) parser.getValueLong();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a boolean.
   * @param parser
   * @return {@link Boolean}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Boolean parserBoolean(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_BOOLEAN) {
      return parser.getValueBoolean();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_BOOLEAN, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a character.
   * @param parser
   * @return {@link Character}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Character parserCharacter(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_STRING) {
      String str = parser.getValueString();
      if (str.length() != 1) {
        throw new IllegalStateException(
            "unexpected value. expecte string size is 1. but get=" + str);
      }
      return str.charAt(0);
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_STRING, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a double-precision floating point value.
   * @param parser
   * @return {@link Double}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Double parserDouble(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_DOUBLE) {
      return parser.getValueDouble();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_DOUBLE, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a single-precision floating point value.
   * @param parser
   * @return {@link Float}
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static Float parserFloat(JsonPullParser parser) throws IOException, JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    } else if (eventType == State.VALUE_DOUBLE) {
      return (float) parser.getValueDouble();
    } else {
      throw new IllegalStateException("unexpected state. expected=VALUE_DOUBLE, but get="
          + eventType.toString());
    }
  }

  /**
   * Parses the current token as a list of integers.
   * @param parser
   * @return List of {@link Integer}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Integer> parserIntegerList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Integer> list = new ArrayList<Integer>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_LONG) {
        list.add((int) parser.getValueLong());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of longs.
   * @param parser
   * @return List of {@link Long}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Long> parserLongList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Long> list = new ArrayList<Long>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_LONG) {
        list.add(parser.getValueLong());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of bytes.
   * @param parser
   * @return List of {@link Byte}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Byte> parserByteList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Byte> list = new ArrayList<Byte>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_LONG) {
        list.add((byte) parser.getValueLong());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of shorts.
   * @param parser
   * @return List of {@link Short}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Short> parserShortList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Short> list = new ArrayList<Short>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_LONG) {
        list.add((short) parser.getValueLong());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of booleans.
   * @param parser
   * @return List of {@link Boolean}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Boolean> parserBooleanList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Boolean> list = new ArrayList<Boolean>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_BOOLEAN) {
        list.add(parser.getValueBoolean());
      } else {
        throw new IllegalStateException(
            "unexpected state. expected=VALUE_BOOLEAN, but get=" + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of characters.
   * @param parser
   * @return List of {@link Character}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Character> parserCharacterList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Character> list = new ArrayList<Character>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_STRING) {
        String str = parser.getValueString();
        if (str.length() != 1) {
          throw new IllegalStateException(
              "unexpected value. expecte string size is 1. but get=" + str);
        }
        list.add(str.charAt(0));
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_STRING, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of double-precision floating point numbers.
   * @param parser
   * @return List of {@link Double}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Double> parserDoubleList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Double> list = new ArrayList<Double>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_DOUBLE) {
        list.add(parser.getValueDouble());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_DOUBLE, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of single-precision floating point numbers.
   * @param parser
   * @return List of {@link Float}.
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Float> parserFloatList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Float> list = new ArrayList<Float>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_DOUBLE) {
        list.add((float) parser.getValueDouble());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_DOUBLE, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of strings.
   * @param parser
   * @return List of {@link String}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<String> parserStringList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<String> list = new ArrayList<String>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_STRING) {
        list.add(parser.getValueString());
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_STRING, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * JSON????????Date?????????????????.
   * Parses the current token as a list of Dates.
   * @param parser
   * @return List of {@link Date}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static List<Date> parserDateList(JsonPullParser parser) throws IOException,
      JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<Date> list = new ArrayList<Date>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_LONG) {
        list.add(new Date(parser.getValueLong()));
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_LONG, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }

  /**
   * Parses the current token as a list of Enums.
   * @param <T> 
   * @param parser
   * @param clazz 
   * @return List of {@link Enum}s
   * @throws IOException
   * @throws JsonFormatException
   * @author vvakame
   */
  public static <T extends Enum<T>>List<T> parserEnumList(JsonPullParser parser, Class<T> clazz)
      throws IOException, JsonFormatException {

    State eventType = parser.getEventType();
    if (eventType == State.VALUE_NULL) {
      return null;
    }
    if (eventType != State.START_ARRAY) {
      throw new IllegalStateException("not started brace!");
    }
    List<T> list = new ArrayList<T>();
    while (parser.lookAhead() != State.END_ARRAY) {
      eventType = parser.getEventType();
      if (eventType == State.VALUE_NULL) {
        list.add(null);
      } else if (eventType == State.VALUE_STRING) {
        T obj = Enum.valueOf(clazz, parser.getValueString());
        list.add(obj);
      } else {
        throw new IllegalStateException("unexpected state. expected=VALUE_STRING, but get="
            + eventType.toString());
      }
    }
    parser.getEventType();
    return list;
  }
}




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