@momo-kits/native-kits 0.112.1-beta.7 → 0.112.1-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,6 @@ import vn.momo.kits.application.ApplicationContext
27
27
  import vn.momo.kits.application.ComposeApi
28
28
  import vn.momo.kits.application.PlatformApi
29
29
  import vn.momo.kits.const.Colors
30
- import vn.momo.kits.utils.ModelConvertUtils
31
30
 
32
31
  data class HeaderRightData(
33
32
  val useOnBoarding: Boolean = false,
@@ -62,6 +61,10 @@ object Spacing {
62
61
  val S = 8.dp
63
62
  val XXS = 2.dp
64
63
  }
64
+ object ComposeApiUtils {
65
+ fun parseParams(vararg params: Any) = ModelConvertUtils.encodeToJsonFromAny(params).toString()
66
+ }
67
+
65
68
 
66
69
  @Composable
67
70
  fun HeaderRight(
@@ -93,7 +96,7 @@ fun ToolkitHeaderRight(headerRight: HeaderRightData? = null) {
93
96
  "icon" to context?.icon,
94
97
  "description" to context?.description
95
98
  )
96
- val params = ModelConvertUtils.encodeToJsonFromAny(paramMap).toString()
99
+ val params = ComposeApiUtils.parseParams(paramMap)
97
100
  api?.request("showHelpCenter", params, { _ -> })
98
101
  }
99
102
 
@@ -113,7 +116,7 @@ fun ToolkitHeaderRight(headerRight: HeaderRightData? = null) {
113
116
  )
114
117
  )
115
118
 
116
- val params = ModelConvertUtils.encodeToJsonFromAny(param).toString()
119
+ val params = ComposeApiUtils.parseParams(param)
117
120
  api?.request("showTools", params, { _ -> })
118
121
  }
119
122
 
@@ -0,0 +1,86 @@
1
+ package vn.momo.kits.utils
2
+
3
+ import kotlinx.serialization.json.JsonElement
4
+ import kotlinx.serialization.json.JsonNull
5
+ import kotlinx.serialization.json.JsonPrimitive
6
+
7
+ internal fun Any?.forceToBoolean() = when (this) {
8
+ is Number -> this == 1
9
+ is Boolean -> this
10
+ is String -> this == "true" || this == "1"
11
+ else -> null
12
+ }
13
+
14
+ fun Any?.forceToString() = when (this) {
15
+ null -> this
16
+ is Number -> this.toLong().toString()
17
+ is String -> this
18
+ else -> this.toString()
19
+ }
20
+
21
+ internal fun Any?.forceToInt() = when (this) {
22
+ null -> this
23
+ is Number -> this.toInt()
24
+ is String -> this.toInt()
25
+ is Boolean -> if (this) 1 else 0
26
+ else -> null
27
+ }
28
+
29
+ internal fun Any?.forceToDouble() = when (this) {
30
+ null -> this
31
+ is Number -> this.toDouble()
32
+ is String -> this.toDouble()
33
+ else -> null
34
+ }
35
+
36
+ internal fun Any?.forceToLong() = when (this) {
37
+ null -> this
38
+ is Number -> this.toLong()
39
+ is String -> this.toLong()
40
+ else -> null
41
+ }
42
+
43
+
44
+ fun Any?.toJsonElement(): JsonElement {
45
+ return when (this) {
46
+ is Number -> JsonPrimitive(this)
47
+ is Boolean -> JsonPrimitive(this)
48
+ is String -> JsonPrimitive(this)
49
+ is Array<*> -> this.toJsonArray()
50
+ is List<*> -> this.toJsonArray()
51
+ is Map<*, *> -> this.toJsonObject()
52
+ is JsonElement -> this
53
+ else -> JsonNull
54
+ }
55
+ }
56
+
57
+ fun Any?.tryForceToBoolean() = try {
58
+ this.forceToBoolean()
59
+ } catch (e: Exception) {
60
+ false
61
+ }
62
+
63
+ fun Any?.tryForceToString() = try {
64
+ this.forceToString()
65
+ } catch (e: Exception) {
66
+ ""
67
+ }
68
+
69
+ fun Any?.tryForceToInt() = try {
70
+ this.forceToInt()
71
+ } catch (e: Exception) {
72
+ 0
73
+ }
74
+
75
+ fun Any?.tryForceToDouble() = try {
76
+ this.forceToDouble()
77
+ } catch (e: Exception) {
78
+ 0.0
79
+ }
80
+
81
+ fun Any?.tryForceToLong() = try {
82
+ this.forceToLong()
83
+ } catch (e: Exception) {
84
+ 0L
85
+ }
86
+
@@ -0,0 +1,67 @@
1
+ package vn.momo.kits.utils
2
+
3
+ import kotlinx.serialization.json.JsonArray
4
+ import kotlinx.serialization.json.JsonElement
5
+ import kotlinx.serialization.json.JsonNull
6
+ import kotlinx.serialization.json.JsonObject
7
+ import kotlinx.serialization.json.JsonPrimitive
8
+
9
+ fun Array<*>.toJsonArray(): JsonArray {
10
+ val array = mutableListOf<JsonElement>()
11
+ this.forEach { array.add(it.toJsonElement()) }
12
+ return JsonArray(array)
13
+ }
14
+
15
+ fun List<*>.toJsonArray(): JsonArray {
16
+ val array = mutableListOf<JsonElement>()
17
+ this.forEach { array.add(it.toJsonElement()) }
18
+ return JsonArray(array)
19
+ }
20
+
21
+ fun Map<*, *>.toJsonObject(): JsonObject {
22
+ val map = mutableMapOf<String, JsonElement>()
23
+ this.forEach {
24
+ if (it.key is String) {
25
+ map[it.key as String] = it.value.toJsonElement()
26
+ }
27
+ }
28
+ return JsonObject(map)
29
+ }
30
+
31
+ internal fun Collection<*>.toJsonElement(): JsonElement {
32
+ val list: MutableList<JsonElement> = mutableListOf()
33
+ this.forEach { value ->
34
+ when (value) {
35
+ null -> list.add(JsonNull)
36
+ is Map<*, *> -> list.add(value.toJsonElement())
37
+ is Collection<*> -> list.add(value.toJsonElement())
38
+ is Boolean -> list.add(JsonPrimitive(value))
39
+ is Number -> list.add(JsonPrimitive(value))
40
+ is String -> list.add(JsonPrimitive(value))
41
+ is Enum<*> -> list.add(JsonPrimitive(value.toString()))
42
+ is JsonPrimitive -> list.add(value)
43
+ else -> throw IllegalStateException("Can't serialize unknown collection type: $value")
44
+ }
45
+ }
46
+ return JsonArray(list)
47
+ }
48
+
49
+ // clear
50
+ internal fun Map<*, *>.toJsonElement(): JsonElement {
51
+ val map: MutableMap<String, JsonElement> = mutableMapOf()
52
+ this.forEach { (key, value) ->
53
+ key as String
54
+ when (value) {
55
+ null -> map[key] = JsonNull
56
+ is Map<*, *> -> map[key] = value.toJsonElement()
57
+ is Collection<*> -> map[key] = value.toJsonElement()
58
+ is Boolean -> map[key] = JsonPrimitive(value)
59
+ is Number -> map[key] = JsonPrimitive(value)
60
+ is String -> map[key] = JsonPrimitive(value)
61
+ is Enum<*> -> map[key] = JsonPrimitive(value.toString())
62
+ is JsonPrimitive -> map[key] = value
63
+ else -> throw IllegalStateException("Can't serialize unknown type: $value")
64
+ }
65
+ }
66
+ return JsonObject(map)
67
+ }
@@ -0,0 +1,175 @@
1
+ package vn.momo.kits.utils
2
+
3
+ import kotlinx.serialization.json.JsonArray
4
+ import kotlinx.serialization.json.JsonElement
5
+ import kotlinx.serialization.json.JsonNull
6
+ import kotlinx.serialization.json.JsonObject
7
+ import kotlinx.serialization.json.JsonPrimitive
8
+ import kotlinx.serialization.json.booleanOrNull
9
+ import kotlinx.serialization.json.contentOrNull
10
+ import kotlinx.serialization.json.doubleOrNull
11
+ import kotlinx.serialization.json.floatOrNull
12
+ import kotlinx.serialization.json.intOrNull
13
+ import kotlinx.serialization.json.jsonArray
14
+ import kotlinx.serialization.json.jsonObject
15
+ import kotlinx.serialization.json.jsonPrimitive
16
+ import kotlinx.serialization.json.longOrNull
17
+
18
+ fun emptyJsonObject() = JsonObject(mapOf())
19
+
20
+ val JsonObject.Companion.EMPTY
21
+ get() = JsonObject(emptyMap())
22
+
23
+ val JsonArray.Companion.EMPTY
24
+ get() = JsonArray(emptyList())
25
+
26
+ fun JsonObject.tryToGetObject(key: String): JsonObject? {
27
+ return try {
28
+ this[key]?.toJsonElement()?.jsonObject
29
+ } catch (_: Exception) {
30
+ JsonObject.EMPTY
31
+ }
32
+ }
33
+
34
+ fun JsonObject.tryToGetArray(key: String): JsonArray? {
35
+ return try {
36
+ this[key]?.toJsonElement()?.jsonArray
37
+ } catch (_: Exception) {
38
+ JsonArray.EMPTY
39
+ }
40
+ }
41
+
42
+ fun JsonObject.tryToGetString(key: String): String {
43
+ return try {
44
+ this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.content
45
+ } catch (_: Exception) {
46
+ ""
47
+ }
48
+ }
49
+
50
+ fun JsonObject.tryToGetStringNullable(key: String): String? {
51
+ return try {
52
+ this.getOrElse(key) { null }.toJsonElement().jsonPrimitive.contentOrNull
53
+ } catch (_: Exception) {
54
+ null
55
+ }
56
+ }
57
+
58
+ fun JsonObject.tryToGetLong(key: String): Long? {
59
+ return try {
60
+ this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.longOrNull
61
+ } catch (_: Exception) {
62
+ null
63
+ }
64
+ }
65
+
66
+ fun JsonObject.tryToGetLong(key: String, default: Long = 0L): Long {
67
+ return try {
68
+ this.getOrElse(key) { default }.toJsonElement().jsonPrimitive.longOrNull ?: default
69
+ } catch (_: Exception) {
70
+ default
71
+ }
72
+ }
73
+
74
+ fun JsonObject.tryToGetDoubleNullable(key: String): Double? {
75
+ return try {
76
+ this.getOrElse(key) { "" }.toJsonElement().jsonPrimitive.doubleOrNull
77
+ } catch (_: Exception) {
78
+ null
79
+ }
80
+ }
81
+
82
+ fun JsonObject.tryToGetDouble(key: String, default: Double = 0.0): Double {
83
+ return try {
84
+ this.getOrElse(key) { default }.toJsonElement().jsonPrimitive.doubleOrNull ?: default
85
+ } catch (_: Exception) {
86
+ default
87
+ }
88
+ }
89
+
90
+ fun JsonObject.tryToGetBoolean(key: String): Boolean {
91
+ return try {
92
+ this[key]?.jsonPrimitive?.booleanOrNull ?: false
93
+ } catch (_: Exception) {
94
+ false
95
+ }
96
+ }
97
+
98
+ fun JsonObject.tryToGetBooleanNullable(key: String): Boolean? {
99
+ return try {
100
+ this[key]?.jsonPrimitive?.booleanOrNull
101
+ } catch (_: Exception) {
102
+ null
103
+ }
104
+ }
105
+
106
+ internal fun JsonElement.toPureMap(): Map<String, Any?> {
107
+ return (this as? JsonObject)?.toPureMap() ?: emptyMap()
108
+ }
109
+
110
+ fun JsonObject.toPureMap(): Map<String, Any?> {
111
+ val map: MutableMap<String, Any?> = mutableMapOf()
112
+ this.jsonObject.entries.forEach { (key, value) ->
113
+ when (value) {
114
+ is Map<*, *> -> map[key] = value.toPureMap()
115
+ is Collection<*> -> map[key] = value.toList().map { it.toJsonElement().toPureMap() }
116
+ is JsonPrimitive -> map[key] =
117
+ if (value.isString) value.content else value.intOrNull ?: value.doubleOrNull
118
+ ?: value.floatOrNull ?: value.booleanOrNull
119
+ is JsonNull -> map[key] = null
120
+ else -> throw IllegalStateException("Can't serialize unknown type: $value")
121
+ }
122
+ }
123
+ return map
124
+ }
125
+
126
+ fun JsonElement.tryToString(): String {
127
+ return try {
128
+ this.jsonPrimitive.content
129
+ } catch (_: Exception) {
130
+ ""
131
+ }
132
+ }
133
+
134
+ internal fun JsonElement.toCollection(): Collection<Any?> {
135
+ val list: MutableList<Any?> = mutableListOf()
136
+ (this as? JsonArray)?.forEach { value ->
137
+ when (value) {
138
+ is Map<*, *> -> list.add(value.toMap())
139
+ is Collection<*> -> list.add(value.toCollection())
140
+ is JsonPrimitive -> list.add(
141
+ if (value.isString) value.content else value.booleanOrNull ?: value.intOrNull
142
+ ?: value.longOrNull ?: value.doubleOrNull ?: value.floatOrNull
143
+ )
144
+
145
+ is JsonNull -> list.add(null)
146
+ else -> throw IllegalStateException("Can't serialize unknown type: $value")
147
+ }
148
+ }
149
+ return list
150
+ }
151
+
152
+ internal fun JsonElement.toMap(): Map<String, Any?> {
153
+ val map: MutableMap<String, Any?> = mutableMapOf()
154
+ (this as? JsonObject)?.entries?.forEach { (key, value) ->
155
+ when (value) {
156
+ is Map<*, *> -> map[key] = value.toMap()
157
+ is Collection<*> -> map[key] = value.toCollection()
158
+ is JsonPrimitive -> map[key] =
159
+ if (value.isString) value.content else value.booleanOrNull ?: value.intOrNull
160
+ ?: value.longOrNull ?: value.doubleOrNull ?: value.floatOrNull
161
+
162
+ is JsonNull -> map[key] = null
163
+ else -> throw IllegalStateException("Can't serialize unknown type: $value")
164
+ }
165
+ }
166
+ return map
167
+ }
168
+
169
+ fun JsonElement.tryForceToStringStringMap(): Map<String, String?>? = try {
170
+ this.toMap() as Map<String, String?>?
171
+ } catch (e: Exception) {
172
+ null
173
+ }
174
+
175
+
@@ -1,11 +1,15 @@
1
1
  package vn.momo.kits.utils
2
2
 
3
+ import kotlinx.serialization.KSerializer
4
+ import kotlinx.serialization.builtins.ListSerializer
5
+ import kotlinx.serialization.encodeToString
3
6
  import kotlinx.serialization.json.Json
4
7
  import kotlinx.serialization.json.JsonArray
5
8
  import kotlinx.serialization.json.JsonElement
6
9
  import kotlinx.serialization.json.JsonNull
7
10
  import kotlinx.serialization.json.JsonObject
8
11
  import kotlinx.serialization.json.JsonPrimitive
12
+ import kotlinx.serialization.json.jsonObject
9
13
 
10
14
 
11
15
  object ModelConvertUtils {
@@ -16,12 +20,51 @@ object ModelConvertUtils {
16
20
  classDiscriminator = discriminator
17
21
  }
18
22
 
23
+ fun handleJsonError(e: Throwable) {
24
+ e.printStackTrace()
25
+ }
26
+
27
+ fun <T> encodeToString(kSerializer: KSerializer<T>, obj: T): String? = try {
28
+ formatter.encodeToString(kSerializer, obj)
29
+ } catch (ex: Throwable) {
30
+ handleJsonError(ex)
31
+ null
32
+ }
33
+
34
+ fun <T> encodeToMap(kSerializer: KSerializer<T>, obj: T): Map<String, Any?>? {
35
+ val value = encodeToJson(kSerializer, obj)
36
+ return if (value == null) null
37
+ else jsonToMap(value)
38
+ }
39
+
40
+
41
+ fun <T> encodeToPureMap(kSerializer: KSerializer<T>, obj: T): Map<String, Any?> {
42
+ val jsonObject = encodeToJson(kSerializer, obj) as? JsonObject
43
+ return jsonObject?.toPureMap() ?: emptyMap()
44
+ }
45
+
46
+ fun <T> encodeToJson(kSerializer: KSerializer<T>, obj: T): JsonElement? = try {
47
+ formatter.encodeToJsonElement(kSerializer, obj)
48
+ } catch (ex: Throwable) {
49
+ handleJsonError(ex)
50
+ null
51
+ }
52
+
19
53
  fun encodeToJson(data: String?): JsonElement? = try {
20
54
  formatter.parseToJsonElement(data ?: "")
21
55
  } catch (e: Throwable) {
56
+ handleJsonError(e)
22
57
  null
23
58
  }
24
59
 
60
+ fun decodeToPureMap(string: String): Map<String, Any?> = try {
61
+ formatter.parseToJsonElement(string).jsonObject.toPureMap()
62
+ } catch (ex: Throwable) {
63
+ handleJsonError(ex)
64
+ emptyMap()
65
+ }
66
+
67
+
25
68
  fun encodeToJson(data: Collection<*>): JsonElement {
26
69
  val parsedData = mutableListOf<JsonElement>()
27
70
  data.forEach { value ->
@@ -79,4 +122,67 @@ object ModelConvertUtils {
79
122
  private fun encodeToJsonFromArray(data: Array<*>): JsonArray =
80
123
  JsonArray(data.filterNotNull().map { encodeToJsonFromAny(it) })
81
124
 
125
+ fun <T> decode(kSerializer: KSerializer<T>, obj: String): T? = try {
126
+ formatter.decodeFromString(kSerializer, obj)
127
+ } catch (ex: Throwable) {
128
+ handleJsonError(ex)
129
+ null
130
+ }
131
+
132
+ fun <T> decode(kSerializer: KSerializer<T>, obj: Map<String, Any?>): T? = try {
133
+ formatter.decodeFromJsonElement(kSerializer, obj.toJsonElement())
134
+ } catch (ex: Throwable) {
135
+ handleJsonError(ex)
136
+ null
137
+ }
138
+
139
+ inline fun <reified T> decode(data: String): T? = try {
140
+ formatter.decodeFromString(data)
141
+ } catch (e: Exception) {
142
+ handleJsonError(e)
143
+ null
144
+ }
145
+
146
+ fun decode(string: String): Map<String, Any?> = try {
147
+ jsonToMap(formatter.parseToJsonElement(string))
148
+ } catch (ex: Throwable) {
149
+ handleJsonError(ex)
150
+ emptyMap()
151
+ }
152
+
153
+ fun <T> decodeFromJson(deserializer: KSerializer<T>, element: JsonElement): T? = try {
154
+ formatter.decodeFromJsonElement(deserializer, element)
155
+ } catch (e: Exception) {
156
+ handleJsonError(e)
157
+ null
158
+ }
159
+
160
+ fun jsonToMap(element: JsonElement): Map<String, Any?> = element.jsonObject.toMap()
161
+
162
+
163
+ fun <T> decodeToList(kSerializer: KSerializer<T>, jsonString: String): List<T> {
164
+ return try {
165
+ formatter.decodeFromString(ListSerializer(kSerializer), jsonString)
166
+ } catch (exception: Throwable) {
167
+ handleJsonError(exception)
168
+ emptyList()
169
+ }
170
+ }
171
+
172
+ inline fun <reified T> encodeToString(value: T): String? = try {
173
+ formatter.encodeToString(value)
174
+ } catch (e: Exception) {
175
+ handleJsonError(e)
176
+ null
177
+ }
178
+
179
+ inline fun <reified T> decodeNonNull(data: String): T =
180
+ try {
181
+ formatter.decodeFromString(data)
182
+ } catch (ex: Exception) {
183
+ ex.printStackTrace()
184
+ formatter.decodeFromString("{}")
185
+ }
186
+
187
+
82
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/native-kits",
3
- "version": "0.112.1-beta.7",
3
+ "version": "0.112.1-beta.8",
4
4
  "private": false,
5
5
  "dependencies": {},
6
6
  "devDependencies": {},