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
 *// www  .  ja  va2s  .  c  om
 * 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.simple;

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 JSON.simple's org.json.simple.*
 * package to manipulate JSON.
 *
 * @since 1.0
 * @see <a href="https://code.google.com/p/json-simple/">JSON.simple</a>
 */
public class JsonObject implements JsonObjectInterface {

  final org.json.simple.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);
  }

  public JsonObject(org.json.JSONObject json) {
    data = JsonUtil.toJsonObject(null);
    merge(json);
  }

  public JsonObject(org.json.simple.JSONObject json) {
    data = null == json ? JsonUtil.toJsonObject(null) : json;
  }

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

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

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

  @Override
  public synchronized boolean contains(String key) {
    return data.containsKey(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) {
    data.put(key, value);
  }

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

  @Override
  public synchronized void clear() {
    data.clear();
  }

  @Override
  public synchronized void merge(org.json.JSONObject json) {
    try {
      final String[] keys = com.fine47.json.builtin.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) {
    final String[] keys = json.keys();
    for(final String key : keys) {
      data.put(key, json.get(key));
    }
  }

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

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

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

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

  @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.toJSONString());
  }

  @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(ClassCastException error) {
      Log.e(
        JsonObjectInterface.LOG_TAG,
        "Casting error while getting value for key: " + key,
        error);
    }
    return defValue;
  }

  private void mergeImpl(Map list) {
    Set keys = list.keySet();
    for(final Object key : keys) {
      data.put(key.toString(), list.get(key));
    }
  }
}




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