1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.utils
3 
4 import org.json.JSONArray
5 import org.json.JSONException
6 import org.json.JSONObject
7 import java.util.ArrayList
8 import java.util.HashMap
9 
10 object JSONUtils {
11   @Throws(JSONException::class)
getJSONStringnull12   @JvmStatic fun getJSONString(item: Any): String {
13     if (item is HashMap<*, *>) {
14       return getJSONFromHashMap(item).toString()
15     } else if (item is ArrayList<*>) {
16       return getJSONFromArrayList(item).toString()
17     }
18     return item.toString()
19   }
20 
21   @Throws(JSONException::class)
getJSONFromArrayListnull22   private fun getJSONFromArrayList(array: ArrayList<*>): JSONArray {
23     val json = JSONArray()
24     for (value in array) {
25       var newValue = value
26       if (value is HashMap<*, *>) {
27         newValue = getJSONFromHashMap(value)
28       } else if (value is ArrayList<*>) {
29         newValue = getJSONFromArrayList(value)
30       }
31       json.put(newValue)
32     }
33     return json
34   }
35 
36   @Throws(JSONException::class)
getJSONFromHashMapnull37   private fun getJSONFromHashMap(map: HashMap<*, *>): JSONObject {
38     val json = JSONObject()
39     for (key in map.keys) {
40       var value = map[key]
41       if (value is HashMap<*, *>) {
42         value = getJSONFromHashMap(value)
43       } else if (value is ArrayList<*>) {
44         value = getJSONFromArrayList(value)
45       }
46       json.put(key.toString(), value)
47     }
48     return json
49   }
50 }
51