Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created January 4, 2017 07:26
Show Gist options
  • Save cattaka/49084f90cd4c9d1eed1e9dcd77a60382 to your computer and use it in GitHub Desktop.
Save cattaka/49084f90cd4c9d1eed1e9dcd77a60382 to your computer and use it in GitHub Desktop.
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Map;
/**
* Created by cattaka on 2017/01/04.
*/
public class JsonUtils {
@Nullable
public static JSONObject toJsonObject(@Nullable Bundle bundle) throws JSONException {
if (bundle == null) {
return null;
}
JSONObject result = new JSONObject();
for (String key : bundle.keySet()) {
result.put(key, toJsonValue(bundle.get(key)));
}
return result;
}
@Nullable
public static JSONObject toJsonObject(@Nullable SparseArray sparseArray) throws JSONException {
if (sparseArray == null) {
return null;
}
JSONObject result = new JSONObject();
for (int i = 0; i < sparseArray.size(); i++) {
int key = sparseArray.keyAt(i);
result.put(String.valueOf(key), toJsonValue(sparseArray.get(key)));
}
return result;
}
@Nullable
public static JSONObject toJsonObject(@Nullable Map<?, ?> map) throws JSONException {
if (map == null) {
return null;
}
JSONObject result = new JSONObject();
for (Map.Entry<?, ?> entry : map.entrySet()) {
result.put(String.valueOf(entry.getKey()), toJsonValue(entry.getValue()));
}
return result;
}
private static Object toJsonValue(Object obj) throws JSONException {
if (obj instanceof byte[]) {
JSONArray array = new JSONArray();
for (byte v : ((byte[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof char[]) {
JSONArray array = new JSONArray();
for (char v : ((char[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof String[]) {
JSONArray array = new JSONArray();
for (String v : ((String[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof CharSequence[]) {
JSONArray array = new JSONArray();
for (CharSequence v : ((CharSequence[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof float[]) {
JSONArray array = new JSONArray();
for (float v : ((float[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof short[]) {
JSONArray array = new JSONArray();
for (short v : ((short[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof int[]) {
JSONArray array = new JSONArray();
for (int v : ((int[]) obj)) {
array.put(v);
}
return array;
} else if (obj instanceof ArrayList) {
JSONArray array = new JSONArray();
for (Object v : ((ArrayList) obj)) {
array.put(toJsonValue(v));
}
return array;
} else if (obj instanceof Parcelable[]) {
JSONArray array = new JSONArray();
for (Parcelable v : ((Parcelable[]) obj)) {
array.put(toJsonValue(v));
}
return array;
} else if (obj instanceof Size) {
JSONObject v = new JSONObject();
v.put("width", ((Size) obj).getWidth());
v.put("height", ((Size) obj).getHeight());
return v;
} else if (obj instanceof SizeF) {
JSONObject v = new JSONObject();
v.put("width", ((SizeF) obj).getWidth());
v.put("height", ((SizeF) obj).getHeight());
return v;
} else if (obj instanceof SparseArray) {
return toJsonObject((SparseArray) obj);
} else if (obj instanceof Map) {
return toJsonObject((Map) obj);
} else if (obj instanceof Bundle) {
return toJsonObject((Bundle) obj);
} else if (obj instanceof CharSequence) {
return obj;
} else if (obj instanceof Number) {
return obj;
} else if (obj == null) {
return null;
} else {
return String.valueOf(obj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment