/
JSObject.kt
153 lines (132 loc) · 3.5 KB
/
JSObject.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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
package app.tauri.plugin
import org.json.JSONException
import org.json.JSONObject
class JSObject : JSONObject {
constructor() : super()
constructor(json: String) : super(json)
constructor(obj: JSONObject, names: Array<String>) : super(obj, names)
override fun getString(key: String): String {
return getString(key, "")
}
fun getString(key: String, defaultValue: String): String {
try {
val value = super.getString(key)
if (!super.isNull(key)) {
return value
}
} catch (_: JSONException) {
}
return defaultValue
}
fun getInteger(key: String): Int? {
return getIntegerInternal(key, null)
}
fun getInteger(key: String, defaultValue: Int): Int {
return getIntegerInternal(key, defaultValue)!!
}
private fun getIntegerInternal(key: String, defaultValue: Int?): Int? {
try {
return super.getInt(key)
} catch (_: JSONException) {
}
return defaultValue
}
override fun getBoolean(key: String): Boolean {
return getBooleanInternal(key, false)!!
}
fun getBoolean(key: String, defaultValue: Boolean?): Boolean {
return getBooleanInternal(key, defaultValue)!!
}
private fun getBooleanInternal(key: String, defaultValue: Boolean?): Boolean? {
try {
return super.getBoolean(key)
} catch (_: JSONException) {
}
return defaultValue
}
fun getJSObject(name: String): JSObject? {
try {
return getJSObjectInternal(name, null)
} catch (_: JSONException) {
}
return null
}
fun getJSObject(name: String, defaultValue: JSObject): JSObject {
return getJSObjectInternal(name, defaultValue)!!
}
private fun getJSObjectInternal(name: String, defaultValue: JSObject?): JSObject? {
try {
val obj = get(name)
if (obj is JSONObject) {
val keysIter = obj.keys()
val keys: MutableList<String> = ArrayList()
while (keysIter.hasNext()) {
keys.add(keysIter.next())
}
return JSObject(obj, keys.toTypedArray())
}
} catch (_: JSONException) {
}
return defaultValue
}
override fun put(key: String, value: Boolean): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
override fun put(key: String, value: Int): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
override fun put(key: String, value: Long): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
override fun put(key: String, value: Double): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
override fun put(key: String, value: Any?): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
fun put(key: String, value: String?): JSObject {
try {
super.put(key, value)
} catch (_: JSONException) {
}
return this
}
companion object {
/**
* Convert a pathetic JSONObject into a JSObject
* @param obj
*/
@Throws(JSONException::class)
fun fromJSONObject(obj: JSONObject): JSObject {
val keysIter = obj.keys()
val keys: MutableList<String> = ArrayList()
while (keysIter.hasNext()) {
keys.add(keysIter.next())
}
return JSObject(obj, keys.toTypedArray())
}
}
}