|
@@ -3,6 +3,17 @@ package com.usai.util;
|
|
|
import android.app.AlertDialog;
|
|
import android.app.AlertDialog;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+
|
|
|
|
|
+import org.json.JSONArray;
|
|
|
|
|
+import org.json.JSONException;
|
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Iterator;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Created by ray on 27/06/2017.
|
|
* Created by ray on 27/06/2017.
|
|
@@ -42,4 +53,116 @@ public class RAUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static JSONObject Bundle2Json(Bundle bundle)
|
|
|
|
|
+ {
|
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
|
+ Set<String> keys = bundle.keySet();
|
|
|
|
|
+ for (String key : keys) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ json.put(key, wrap(bundle.get(key))); //see edit below
|
|
|
|
|
+// json.put(key, JSONObject.wrap(bundle.get(key)));
|
|
|
|
|
+ } catch(JSONException e) {
|
|
|
|
|
+ //Handle exception here
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return json;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object wrap(Object o) {
|
|
|
|
|
+ if (o == null) {
|
|
|
|
|
+ return JSONObject.NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o instanceof JSONArray || o instanceof JSONObject) {
|
|
|
|
|
+ return o;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o.equals(JSONObject.NULL)) {
|
|
|
|
|
+ return o;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (o instanceof Collection) {
|
|
|
|
|
+ return new JSONArray((Collection) o);
|
|
|
|
|
+ } else if (o.getClass().isArray()) {
|
|
|
|
|
+ return toJSONArray(o);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o instanceof Map) {
|
|
|
|
|
+ return new JSONObject((Map) o);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o instanceof Boolean ||
|
|
|
|
|
+ o instanceof Byte ||
|
|
|
|
|
+ o instanceof Character ||
|
|
|
|
|
+ o instanceof Double ||
|
|
|
|
|
+ o instanceof Float ||
|
|
|
|
|
+ o instanceof Integer ||
|
|
|
|
|
+ o instanceof Long ||
|
|
|
|
|
+ o instanceof Short ||
|
|
|
|
|
+ o instanceof String) {
|
|
|
|
|
+ return o;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o.getClass().getPackage().getName().startsWith("java.")) {
|
|
|
|
|
+ return o.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static JSONArray toJSONArray(Object array) throws JSONException {
|
|
|
|
|
+ JSONArray result = new JSONArray();
|
|
|
|
|
+ if (!array.getClass().isArray()) {
|
|
|
|
|
+ throw new JSONException("Not a primitive array: " + array.getClass());
|
|
|
|
|
+ }
|
|
|
|
|
+ final int length = Array.getLength(array);
|
|
|
|
|
+ for (int i = 0; i < length; ++i) {
|
|
|
|
|
+ result.put(wrap(Array.get(array, i)));
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ public static Bundle Json2Bundle(JSONObject s) {
|
|
|
|
|
+ Bundle bundle = new Bundle();
|
|
|
|
|
+
|
|
|
|
|
+ for (Iterator<String> it = s.keys(); it.hasNext(); ) {
|
|
|
|
|
+ String key = it.next();
|
|
|
|
|
+ JSONArray arr = s.optJSONArray(key);
|
|
|
|
|
+ Double num = s.optDouble(key);
|
|
|
|
|
+ String str = s.optString(key);
|
|
|
|
|
+
|
|
|
|
|
+ if (arr != null && arr.length() <= 0)
|
|
|
|
|
+ bundle.putStringArray(key, new String[]{});
|
|
|
|
|
+
|
|
|
|
|
+ else if (arr != null && !Double.isNaN(arr.optDouble(0))) {
|
|
|
|
|
+ double[] newarr = new double[arr.length()];
|
|
|
|
|
+ for (int i=0; i<arr.length(); i++)
|
|
|
|
|
+ newarr[i] = arr.optDouble(i);
|
|
|
|
|
+ bundle.putDoubleArray(key, newarr);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ else if (arr != null && arr.optString(0) != null) {
|
|
|
|
|
+ String[] newarr = new String[arr.length()];
|
|
|
|
|
+ for (int i=0; i<arr.length(); i++)
|
|
|
|
|
+ newarr[i] = arr.optString(i);
|
|
|
|
|
+ bundle.putStringArray(key, newarr);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ else if (!num.isNaN())
|
|
|
|
|
+ bundle.putDouble(key, num);
|
|
|
|
|
+
|
|
|
|
|
+ else if (str != null)
|
|
|
|
|
+ bundle.putString(key, str);
|
|
|
|
|
+
|
|
|
|
|
+ else
|
|
|
|
|
+ System.err.println("unable to transform json to bundle " + key);
|
|
|
|
|
+ }
|
|
|
|
|
+ return bundle;
|
|
|
|
|
+ }
|
|
|
|
|
+// public static Bundle Json2Bundle(JSONObject jsonObject) throws JSONException {
|
|
|
|
|
+// Bundle bundle = new Bundle();
|
|
|
|
|
+// Iterator iter = jsonObject.keys();
|
|
|
|
|
+// while(iter.hasNext()){
|
|
|
|
|
+// String key = (String)iter.next();
|
|
|
|
|
+// String value = jsonObject.getString(key);
|
|
|
|
|
+// bundle.putString(key,value);
|
|
|
|
|
+// }
|
|
|
|
|
+// return bundle;
|
|
|
|
|
+// }
|
|
|
}
|
|
}
|