-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONUtils.kt
253 lines (220 loc) · 6.03 KB
/
JSONUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import org.json.JSONArray
import org.json.JSONObject
import java.util.ArrayList
val JSONObject.asJsonSkeleton
get() = JSONUtils.convertJsonToSkeleton(this)
val String.asJsonSkeleton
get() = JSONUtils.convertJsonToSkeleton(this)
val String.asSkeleton
get() = JSONUtils.convertJsonToSkeletonString(this)
inline val String.asJsonArrayOrNull: JSONArray?
get()
{
var jsonArray: JSONArray? = null
if (this.isEmpty())
{
return jsonArray
}
try
{
jsonArray = JSONArray(this)
}
catch (e: Exception)
{
// KotlinUtils.printStackTrace(e)
}
return jsonArray
}
inline val String.asJsonObjOrNull: JSONObject?
get()
{
var jsonObj: JSONObject? = null
if (this.isEmpty())
{
return jsonObj
}
try
{
jsonObj = JSONObject(this)
}
catch (e: Exception)
{
// KotlinUtils.printStackTrace(e)
}
return jsonObj
}
inline val String.asJsonObj: JSONObject
get()
{
return JSONObject(this)
}
inline val String.asJsonArray: JSONArray
get()
{
return JSONArray(this)
}
fun JSONArray.isEmpty(): Boolean {
return this.length() == 0
}
fun JSONArray.isNotEmpty(): Boolean {
return this.length() > 0
}
object JSONUtils
{
private val importantKeys = setOf("code",
"status",
"error")
fun convertJsonToSkeleton(jsonObject: JSONObject): JSONObject
{
val jsonSkeleton = JSONObject()
val keys = jsonObject.keys()
while (keys.hasNext()) {
val key = keys.next()
val value = anyToSkeleton(key, jsonObject.get(key))
jsonSkeleton.put(key, value)
}
return jsonSkeleton
}
fun convertJsonToSkeleton(apiResponse: String?): JSONObject
{
return JSONObject().also {
it.put("skeleton", convertJsonToSkeletonString(apiResponse))
}
}
fun convertJsonToSkeletonString(apiResponse: String?): Any
{
if (apiResponse.isNullOrBlank())
{
return "Empty Response"
}
else
{
var any: Any?
any = apiResponse.asJsonObjOrNull
if (any == null) {
any = apiResponse.asJsonArrayOrNull
}
if (any == null) {
if (apiResponse == "true") {
any = true
} else if (apiResponse == "false") {
any = false
}
}
if (any == null) {
any = apiResponse.toIntOrNull()
}
if (any == null) {
any = apiResponse.toLongOrNull()
}
if (any == null) {
any = apiResponse.toDoubleOrNull()
}
if (any == null) {
any = apiResponse
}
return anyToSkeleton(null, any)
}
}
private fun anyToSkeleton(key: String?, anything: Any?): Any
{
var any: Any? = anything
if (anything is String) {
any = anything.asJsonObjOrNull
if (any == null) {
any = anything.asJsonArrayOrNull
}
if (any == null) {
any = anything
}
}
if (any != null)
{
when (any)
{
is JSONObject ->
{
return jsonObjectToSkeleton(any)
}
is JSONArray ->
{
return jsonArrayToSkeleton(any)
}
}
if (importantKeys.contains(key))
{
return any
}
else
{
return when (any)
{
is String ->
{
"String ${any.toString().length}"
}
is Int ->
{
"Int ${any.toString().length}"
}
is Long ->
{
"Long ${any.toString().length}"
}
is Double ->
{
"Double: ${any.toString().length}"
}
is Boolean ->
{
return any
}
else ->
{
any.toString()
}
}
}
}
else
{
return "null"
}
}
fun String.redactDigits(): String {
val value = this
val digits = value.filter { it.isDigit() }
if (digits.length > 3) {
return value.filter { !it.isDigit() } + ", Digits ${digits.length}, hash: ${digits.hashCode()}"
}
return value
}
private fun jsonObjectToSkeleton(jsonObject: JSONObject): JSONObject
{
val skeletonJsonObject = JSONObject()
jsonObject.keys().forEach { key ->
skeletonJsonObject.put(key.redactDigits(), anyToSkeleton(key, jsonObject.get(key)))
}
return skeletonJsonObject
}
private fun jsonArrayToSkeleton(jsonArray: JSONArray): JSONArray
{
val skeletonJsonArray = JSONArray()
for (i in 0 until jsonArray.length())
{
skeletonJsonArray.put(i, anyToSkeleton(null, jsonArray[i]))
}
return skeletonJsonArray
}
fun merge(vararg jsonObjects: JSONObject): JSONObject {
val jsonObject = JSONObject()
for (temp in jsonObjects) {
val keys = temp.keys()
while (keys.hasNext()) {
val key = keys.next()
jsonObject.put(key, temp[key])
}
}
return jsonObject
}
}