Skip to content

Instantly share code, notes, and snippets.

@OmidID
Last active August 31, 2017 13:48
Show Gist options
  • Save OmidID/bbb19962a66603b786416fedccb88cdd to your computer and use it in GitHub Desktop.
Save OmidID/bbb19962a66603b786416fedccb88cdd to your computer and use it in GitHub Desktop.
JSONHelper to manage Json value from JSONObject more easy and relax
package com.omidid.utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by OmidID on 8/22/2017.
*/
public class JSONHelper {
public static final int DEFAULT_INT = -1;
public static final long DEFAULT_LONG = -1;
public static final double DEFAULT_DOUBLE = -1;
public static final boolean DEFAULT_BOOLEAN = false;
public static final String DEFAULT_STRING = null;
public static final Object DEFAULT_OBJECT = null;
public static final JSONObject DEFAULT_JSONOBJECT = null;
public static final JSONArray DEFAULT_JSONARRAY = null;
JSONObject json;
public JSONHelper() {
this.json = new JSONObject();
}
public JSONHelper(JSONObject json) {
this.json = json;
}
public JSONObject getJSONObject() {
return json;
}
/**
* get the same helper again but for the specific key
* @return return will be <code>null</code> if it's not exists otherwise you get {@link JSONHelper}
*/
public JSONHelper get(String key) {
JSONObject val = getJSONObject(key, DEFAULT_JSONOBJECT);
if (val == null) {
return null;
}
return new JSONHelper(val);
}
//region Get Native Json object
/**
* get direct json Object, RECOMMENDED to use {@link JSONHelper#get}
* @return return will be {@link JSONHelper#DEFAULT_JSONOBJECT} if it's not exists
*/
public JSONObject getJSONObject(String key) {
return getJSONObject(key, DEFAULT_JSONOBJECT);
}
/**
* get direct json Object, RECOMMENDED to use @getJson
* @return return will be <code>def</code> if it's not exists
*/
public JSONObject getJSONObject(String key, JSONObject def) {
if (!json.has(key)) {
return def;
}
try {
return json.getJSONObject(key);
} catch (JSONException e) {
return def;
}
}
/**
* get direct {@link JSONArray}
* @return return will be {@link JSONHelper#DEFAULT_JSONARRAY} if it's not exists
*/
public JSONArray getJSONArray(String key) {
return getJSONArray(key, DEFAULT_JSONARRAY);
}
/**
* get direct json array
*/
public JSONArray getJSONArray(String key, JSONArray def) {
if (!json.has(key)) {
return def;
}
try {
return json.getJSONArray(key);
} catch (JSONException e) {
return def;
}
}
//endregion
//region Get method(s)
/**
* get string value from the key if it's exists
* @return return will be {@link JSONHelper#DEFAULT_STRING} if it's not exists
*/
public String getString(String key) {
return getString(key, DEFAULT_STRING);
}
/**
* get string value from the key if it's exists
* @return return will be <code>def</code> if it's not exists
*/
public String getString(String key, String def) {
if (!json.has(key)) {
return def;
}
try {
return json.getString(key);
} catch (JSONException e) {
return def;
}
}
/**
* get boolean value from the key if it's exists
* @return return will be {@link JSONHelper#DEFAULT_BOOLEAN} if it's not exists
*/
public boolean getBoolean(String key) {
return getBoolean(key, DEFAULT_BOOLEAN);
}
/**
* get boolean value from the key if it's exists
* @return return will be <code>def</code> if it's not exists
*/
public boolean getBoolean(String key, boolean def) {
if (!json.has(key)) {
return def;
}
try {
return json.getBoolean(key);
} catch (JSONException e) {
return def;
}
}
/**
* get boolean value from the key check the int value should be equal to the true or false values
* @return return will be <code>def</code> if it's not exists
*/
public boolean getBoolean(String key, boolean def, int trueValue, int falseValue) {
if (!json.has(key)) {
return def;
}
try {
int val = json.getInt(key);
if (val == trueValue) return true;
if (val == falseValue) return false;
return def;
} catch (JSONException e) {
return def;
}
}
/**
* get boolean value from the key check the string value should be equal to the true or false values
* @return return will be <code>def</code> if it's not exists
*/
public boolean getBoolean(String key, boolean def, String trueValue, String falseValue) {
if (!json.has(key)) {
return def;
}
try {
String val = json.getString(key);
if (val.equalsIgnoreCase(trueValue)) return true;
if (val.equalsIgnoreCase(falseValue)) return false;
return def;
} catch (JSONException e) {
return def;
}
}
/**
* get int value from the key if it's exists
* @return return will be {@link JSONHelper#DEFAULT_INT} if it's not exists
*/
public int getInt(String key) {
return getInt(key, DEFAULT_INT);
}
/**
* get int value from the key if it's exists
* @return return will be <code>def</code> if it's not exists
*/
public int getInt(String key, int def) {
if (!json.has(key)) {
return def;
}
try {
return json.getInt(key);
} catch (JSONException e) {
return def;
}
}
/**
* get long value from the key if it's exists
* @return return will be {@link JSONHelper#DEFAULT_LONG} if it's not exists
*/
public long getLong(String key) {
return getLong(key, DEFAULT_LONG);
}
/**
* get long value from the key if it's exists
* @return return will be <code>def</code> if it's not exists
*/
public long getLong(String key, long def) {
if (!json.has(key)) {
return def;
}
try {
return json.getLong(key);
} catch (JSONException e) {
return def;
}
}
/**
* get double value from the key if it's exists
* @return return will be {@link JSONHelper#DEFAULT_DOUBLE} if it's not exists
*/
public double getDouble(String key) {
return getDouble(key, DEFAULT_DOUBLE);
}
/**
* get double value from the key if it's exists
* @return return will be <code>def</code> if it's not exists
*/
public double getDouble(String key, double def) {
if (!json.has(key)) {
return def;
}
try {
return json.getDouble(key);
} catch (JSONException e) {
return def;
}
}
public Object getObject(String key) {
return getObject(key, DEFAULT_OBJECT);
}
public Object getObject(String key, Object def) {
if (!json.has(key)) {
return def;
}
try {
return json.get(key);
} catch (JSONException e) {
return def;
}
}
//endregion
//region Get array
/**
* get string[] if the key exists
* @return return will be null in any problem or value not exists
*/
public String[] getArrayString(String key) {
try {
JSONArray array = json.getJSONArray(key);
String[] list = new String[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.getString(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get int[] if the key exists
* @return return will be null in any problem or value not exists
*/
public int[] getArrayInt(String key) {
try {
JSONArray array = json.getJSONArray(key);
int[] list = new int[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.getInt(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get long[] if the key exists
* @return return will be null in any problem or value not exists
*/
public long[] getArrayLong(String key) {
try {
JSONArray array = json.getJSONArray(key);
long[] list = new long[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.getLong(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get double[] if the key exists
* @return return will be null in any problem or value not exists
*/
public double[] getArrayDouble(String key) {
try {
JSONArray array = json.getJSONArray(key);
double[] list = new double[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.getDouble(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get boolean[] if the key exists
* @return return will be null in any problem or value not exists
*/
public boolean[] getArrayBoolean(String key) {
try {
JSONArray array = json.getJSONArray(key);
boolean[] list = new boolean[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.getBoolean(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get boolean[] if the key exists
* @return return will be null in any problem or value not exists
*/
public boolean[] getArrayBoolean(String key, boolean defValue, int trueValue, int falseValue) {
try {
JSONArray array = json.getJSONArray(key);
boolean[] list = new boolean[array.length()];
for (int i = 0; i < array.length(); i++) {
int val = array.getInt(i);
if (val == trueValue) list[i] = true;
else if (val == falseValue) list[i] = false;
else list[i] = defValue;
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get boolean[] if the key exists
* @return return will be null in any problem or value not exists
*/
public boolean[] getArrayBoolean(String key, boolean defValue, String trueValue, String falseValue) {
try {
JSONArray array = json.getJSONArray(key);
boolean[] list = new boolean[array.length()];
for (int i = 0; i < array.length(); i++) {
String val = array.getString(i);
if (val.equalsIgnoreCase(trueValue)) list[i] = true;
else if (val.equalsIgnoreCase(falseValue)) list[i] = false;
else list[i] = defValue;
}
return list;
} catch (Exception ex) {
return null;
}
}
/**
* get object[] if the key exists
* @return return will be null in any problem or value not exists
*/
public Object[] getArrayObject(String key) {
try {
JSONArray array = json.getJSONArray(key);
Object[] list = new Object[array.length()];
for (int i = 0; i < array.length(); i++) {
list[i] = array.get(i);
}
return list;
} catch (Exception ex) {
return null;
}
}
//endregion
//region Put method(s)
/**
* put string value for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, String value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put int value for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, int value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put double value for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, double value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put long value for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, long value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put boolean value for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, boolean value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put Another helper here
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, JSONHelper value) {
try {
json.put(key, value.json);
} catch (JSONException e) {
return null;
}
return this;
}
/**
* put Object for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, Object value) {
try {
json.put(key, value);
} catch (JSONException e) {
return null;
}
return this;
}
//endregion
//region Put array
/**
* put JSONArray from String[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, String[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from int[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, int[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from long[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, long[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from double[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, double[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from boolean[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, boolean[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from sub json of JSONHelper[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, JSONHelper[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i].json);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
/**
* put JSONArray from Object[] for the key
* @return return will be <code>null</code> if any problem happen otherwise the same class
*/
public JSONHelper put(String key, Object[] values) {
try {
JSONArray array = new JSONArray();
for (int i = 0; i < values.length; i++) {
array.put(i, values[i]);
}
json.put(key, array);
return this;
} catch (Exception ex) {
return null;
}
}
//endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment