Android Open Source - json-interface Json Array






From Project

Back to project page json-interface.

License

The source code is released under:

MIT License

If you think the Android project json-interface 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

/**
 * This file is part of JSON Interface library.
 * Copyright (C) 2014 Noor Dawod. All rights reserved.
 * https://github.com/noordawod/json-interface
 *//from  w  w  w. j  a  va2 s  .  co m
 * Released under the MIT license
 * http://en.wikipedia.org/wiki/MIT_License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package com.fine47.json.builtin;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.util.Log;
import com.fine47.json.JsonArrayInterface;
import java.util.List;

/**
 * A JSON array implementation which uses Android's builtin org.json.* package
 * to manipulate JSON.
 *
 * @since 1.0
 */
public class JsonArray implements JsonArrayInterface {

  final org.json.JSONArray data;

  /**
   * Used by {@link Parcelable} class to recreate an instance of this class.
   */
  public static final Creator<JsonArray> CREATOR = new Creator<JsonArray>() {

    @Override
    public JsonArray createFromParcel(Parcel source) {
      return new JsonArray(source);
    }

    @Override
    public JsonArray[] newArray(int size) {
      return new JsonArray[size];
    }
  };

  /**
   * Creates a new empty JSON array.
   */
  public JsonArray() {
    data = JsonUtil.toJsonArray(null);
  }

  /**
   * Creates a new JSON array and initialize it with the specified JSON-encoded
   * string.
   *
   * @param stringified JSON-encoded string to initialize a new array
   */
  public JsonArray(String stringified) {
    data = JsonUtil.toJsonArray(stringified);
  }

  public JsonArray(List list) {
    this();
    mergeImpl(list);
  }

  JsonArray(org.json.JSONArray json, boolean internal) {
    data = null == json ? new org.json.JSONArray() : json;
  }

  private JsonArray(Parcel source) {
    data = JsonUtil.toJsonArray(source.readString());
  }

  @Override
  public synchronized int size() {
    return data.length();
  }

  @Override
  public synchronized boolean isEmpty() {
    return 1 > data.length();
  }

  @Override
  public boolean contains(Object value) {
    return -1 < indexOfImpl(value);
  }

  @Override
  public synchronized int indexOf(Object value) {
    return indexOfImpl(value);
  }

  @Override
  public synchronized <T> T get(int position) {
    return this.<T>getImpl(position, null);
  }

  @Override
  public synchronized <T> T get(int position, T defValue) {
    return this.<T>getImpl(position, defValue);
  }

  @Override
  public synchronized void add(Object value) {
    addImpl(data.length(), value);
  }

  @Override
  public synchronized void add(int position, Object value) {
    addImpl(position, value);
  }

  @Override
  public synchronized void remove(int position) {
    data.remove(position);
  }

  @Override
  public synchronized void remove(Object value) {
    boolean removed;
    do {
      removed = null != data.remove(indexOfImpl(value));
    } while(removed);
  }

  @Override
  public synchronized void clear() {
    while(0 < data.length()) {
      data.remove(0);
    }
  }

  @Override
  public synchronized void merge(org.json.JSONArray json) {
    try {
      int length = json.length(), pos = -1;
      while(++pos < length) {
        data.put(json.get(pos));
      }
    } catch(org.json.JSONException error) {
      Log.e(
        JsonArrayInterface.LOG_TAG,
        "Error while merging in a native JSON array.",
        error);
    }
  }

  @Override
  public synchronized void merge(JsonArrayInterface json) {
    int length = json.size(), pos = -1;
    while(++pos < length) {
      data.put(json.get(pos));
    }
  }

  @Override
  public synchronized void merge(List list) {
    mergeImpl(list);
  }

  @Override
  public synchronized JsonArrayInterface clone() {
    return new JsonArray(data.toString());
  }

  @Override
  public synchronized org.json.JSONArray getNative() {
    return data;
  }

  @Override
  public synchronized String getAsString() {
    return data.toString();
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public synchronized void writeToParcel(Parcel out, int flags) {
    out.writeString(data.toString());
  }

  @Override
  public synchronized String toString() {
    return getAsString();
  }

  @Override
  public synchronized boolean equals(Object obj) {
    return
      null != obj &&
      obj instanceof JsonArray &&
      data.equals(((JsonArray)obj).data);
  }

  @Override
  public synchronized int hashCode() {
    return data.hashCode();
  }

  /*
   * Private methods.
   */

  private int indexOfImpl(Object value) {
    if(null != value) {
      int length = data.length(), pos = -1;
      while(++pos < length) {
        if(value.equals(getImpl(pos, null))) {
          return pos;
        }
      }
    }
    return -1;
  }

  private void addImpl(int position, Object value) {
    try {
      data.put(position, value);
    } catch(org.json.JSONException error) {
      Log.e(
        JsonArrayInterface.LOG_TAG,
        "Error while adding a new value at position: " + position,
        error);
    }
  }

  private <T> T getImpl(int position, T defValue) {
    try {
      return (T)JsonUtil.normalize(data.get(position));
    } catch(org.json.JSONException error) {
      Log.e(
        JsonArrayInterface.LOG_TAG,
        "Error while getting value at position: " + position,
        error);
    } catch(ClassCastException error) {
      Log.e(
        JsonArrayInterface.LOG_TAG,
        "Casting error while getting value at position: " + position,
        error);
    }
    return defValue;
  }

  private void mergeImpl(List list) {
    int length = list.size(), pos = -1;
    while(++pos < length) {
      data.put(list.get(pos));
    }
  }
}




Java Source Code List

com.fine47.json.JsonArrayInterface.java
com.fine47.json.JsonInterface.java
com.fine47.json.JsonObjectInterface.java
com.fine47.json.Marshaller.java
com.fine47.json.builtin.JsonArray.java
com.fine47.json.builtin.JsonObject.java
com.fine47.json.builtin.JsonUtil.java
com.fine47.json.simple.JsonArray.java
com.fine47.json.simple.JsonObject.java
com.fine47.json.simple.JsonUtil.java