Android Open Source - json-interface Json Object






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 ww  .j a v a2s .  c o  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.util.Log;
import com.fine47.json.JsonObjectInterface;
import java.util.Map;
import java.util.Set;

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

  final org.json.JSONObject data;

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

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

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

  /**
   * Creates a new empty JSON object.
   */
  public JsonObject() {
    data = JsonUtil.toJsonObject(null);
  }

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

  public JsonObject(Map map) {
    this();
    mergeImpl(map);
  }

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

  private JsonObject(Parcel source) {
    data = JsonUtil.toJsonObject(source.readString());
  }

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

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

  @Override
  public synchronized boolean contains(String key) {
    return data.has(key);
  }

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

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

  @Override
  public synchronized void add(String key, Object value) {
    try {
      data.put(key, value);
    } catch(org.json.JSONException error) {
      Log.e(JsonObjectInterface.LOG_TAG,
        "Error while adding a value for key: " + key,
        error);
    }
  }

  @Override
  public synchronized void remove(String key) {
    data.remove(key);
  }

  @Override
  public synchronized void clear() {
    final String[] keys = JsonUtil.getKeys(data);
    int length = keys.length, pos = -1;
    while(++pos < length) {
      data.remove(keys[pos]);
    }
  }

  @Override
  public synchronized void merge(org.json.JSONObject json) {
    try {
      final String[] keys = JsonUtil.getKeys(json);
      for(final String key : keys) {
        data.put(key, json.get(key));
      }
    } catch(org.json.JSONException error) {
      Log.e(
        JsonObjectInterface.LOG_TAG,
        "Error while merging in a native JSON object.",
        error);
    }
  }

  @Override
  public synchronized void merge(JsonObjectInterface json) {
    try {
      final String[] keys = json.keys();
      for(final String key : keys) {
        data.put(key, json.get(key));
      }
    } catch(org.json.JSONException error) {
      Log.e(
        JsonObjectInterface.LOG_TAG,
        "Error while merging in a JSON object.",
        error);
    }
  }

  @Override
  public synchronized void merge(Map map) {
    mergeImpl(map);
  }

  @Override
  public synchronized JsonObjectInterface clone() {
    return new JsonObject(data.toString());
  }

  public org.json.JSONObject getNative() {
    return data;
  }

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

  @Override
  public synchronized String[] keys() {
    return JsonUtil.getKeys(data);
  }

  @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 JsonObject &&
      data.equals(((JsonObject)obj).data);
  }

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

  /*
   * Private methods.
   */

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

  private void mergeImpl(Map list) {
    try {
      Set keys = list.keySet();
      for(final Object key : keys) {
        data.put(key.toString(), list.get(key));
      }
    } catch(org.json.JSONException error) {
      Log.e(
        JsonObjectInterface.LOG_TAG,
        "Error while merging in a List object.",
        error);
    }
  }
}




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