Android Open Source - JsonPullParser Json Slice 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

package net.vvakame.util.jsonpullparser.util;
//from  ww w.j  a  va  2  s.c o m
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;

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

/**
 * An utility class for {@link JsonSlice}
 * @author vvakame
 */
public class JsonSliceUtil {

  /**
   * Encodes the given list of {@link JsonSlice}s to valid JSON.
   *
   * @param slices List of {@link JsonSlice}s to encode
   * @return Valid JSON-formatted string
   * @throws JsonFormatException
   * @throws IOException
   * @author vvakame
   */
  public static String slicesToString(List<JsonSlice> slices) throws JsonFormatException,
      IOException {
    if (slices == null || slices.size() == 0) {
      throw new JsonFormatException("slices is null or empty.");
    }

    StringWriter writer = new StringWriter();

    int cnt = slices.size();
    for (int i = 0; i < cnt; i++) {
      JsonSlice slice = slices.get(i);
      if (slice == null) {
        throw new JsonFormatException("slice is null. i=" + i);
      }
      switch (slice.getState()) {
        case START_ARRAY:
          i = slicesToStringInArray(slices, i, writer);
          break;
        case START_HASH:
          i = slicesToStringInHash(slices, i, writer);
          break;
        default:
          throw new JsonFormatException("invalid state. i=" + i + " is "
              + slice.getState());
      }
    }

    return writer.toString();
  }

  static int slicesToStringInArray(List<JsonSlice> slices, int index, Writer writer)
      throws JsonFormatException, IOException {

    if (slices.get(index).getState() != State.START_ARRAY) {
      throw new JsonFormatException("invalid state. i=" + index + " is "
          + slices.get(index).getState());
    }
    JsonUtil.startArray(writer);
    index++;

    int cnt = slices.size();
    for (int i = index; i < cnt; i++) {
      JsonSlice slice = slices.get(i);
      if (slice == null) {
        throw new JsonFormatException("slice is null. i=" + i);
      }
      switch (slice.getState()) {
        case START_ARRAY:
          i = slicesToStringInArray(slices, i, writer);
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case START_HASH:
          i = slicesToStringInHash(slices, i, writer);
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case END_ARRAY:
          JsonUtil.endArray(writer);
          return i;

        case VALUE_BOOLEAN:
          JsonUtil.put(writer, slice.getValueBoolean());
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_LONG:
          JsonUtil.put(writer, slice.getValueLong());
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_DOUBLE:
          JsonUtil.put(writer, slice.getValueDouble());
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_STRING:
          JsonUtil.put(writer, slice.getValueStr());
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_NULL:
          JsonUtil.put(writer, (Object) null);
          if (!isNextIsEndArray(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        default:
          throw new JsonFormatException("invalid state. i=" + i + " is "
              + slice.getState());
      }
    }
    throw new JsonFormatException("invalid state.");
  }

  static boolean isNextIsEndArray(List<JsonSlice> slices, int index) {
    if (slices.size() <= index + 1) {
      return false;
    } else {
      return slices.get(index + 1).getState() == State.END_ARRAY;
    }
  }

  static int slicesToStringInHash(List<JsonSlice> slices, int index, Writer writer)
      throws JsonFormatException, IOException {

    if (slices.get(index).getState() != State.START_HASH) {
      throw new JsonFormatException("invalid state. i=" + index + " is "
          + slices.get(index).getState());
    }
    JsonUtil.startHash(writer);
    index++;

    int cnt = slices.size();
    boolean isExpectKey = true;
    for (int i = index; i < cnt; i++) {
      JsonSlice slice = slices.get(i);
      if (slice == null) {
        throw new JsonFormatException("slice is null. i=" + i);
      }

      State state = slice.getState();
      switch (state) {
        case START_ARRAY:
          isExpectKey = checkExpectKey(isExpectKey, state);
          i = slicesToStringInArray(slices, i, writer);
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case START_HASH:
          isExpectKey = checkExpectKey(isExpectKey, state);
          i = slicesToStringInHash(slices, i, writer);
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case END_HASH:
          JsonUtil.endHash(writer);
          return i;

        case KEY:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.putKey(writer, slice.getValueStr());
          break;

        case VALUE_BOOLEAN:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.put(writer, slice.getValueBoolean());
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_LONG:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.put(writer, slice.getValueLong());
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_DOUBLE:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.put(writer, slice.getValueDouble());
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_STRING:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.put(writer, slice.getValueStr());
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        case VALUE_NULL:
          isExpectKey = checkExpectKey(isExpectKey, state);
          JsonUtil.put(writer, (Object) null);
          if (!isNextIsEndHash(slices, i)) {
            JsonUtil.addSeparator(writer);
          }
          break;

        default:
          throw new JsonFormatException("invalid state. i=" + i + " is "
              + slice.getState());
      }
    }
    throw new JsonFormatException("invalid state.");
  }

  static boolean checkExpectKey(boolean isExpectKey, State state) throws JsonFormatException {
    if (isExpectKey && state != State.KEY) {
      throw new JsonFormatException("expected State is KEY, but get=" + state);
    } else if (!isExpectKey && state == State.KEY) {
      throw new JsonFormatException("expected State is not KEY.");
    }
    return !isExpectKey;
  }

  static boolean isNextIsEndHash(List<JsonSlice> slices, int index) {
    if (slices.size() <= index + 1) {
      return false;
    } else {
      return slices.get(index + 1).getState() == State.END_HASH;
    }
  }

}




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