@momo-kits/native-kits 0.112.1-beta.8 → 0.112.1-beta.9
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.
- package/compose/src/commonMain/kotlin/vn/momo/kits/{components → application}/HeaderRight.kt +10 -14
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/NavigationContainer.kt +13 -30
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Screen.kt +0 -1
- package/package.json +1 -1
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/AnyConverter.kt +0 -86
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/CollectionConverter.kt +0 -67
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/JsonConverter.kt +0 -175
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/ModelConvertUtils.kt +0 -188
package/compose/src/commonMain/kotlin/vn/momo/kits/{components → application}/HeaderRight.kt
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package vn.momo.kits.
|
|
1
|
+
package vn.momo.kits.application
|
|
2
2
|
|
|
3
3
|
import androidx.compose.foundation.background
|
|
4
4
|
import androidx.compose.foundation.border
|
|
@@ -23,9 +23,9 @@ import androidx.compose.ui.Modifier
|
|
|
23
23
|
import androidx.compose.ui.draw.clip
|
|
24
24
|
import androidx.compose.ui.graphics.Color
|
|
25
25
|
import androidx.compose.ui.unit.dp
|
|
26
|
-
import vn.momo.kits.
|
|
27
|
-
import vn.momo.kits.
|
|
28
|
-
import vn.momo.kits.
|
|
26
|
+
import vn.momo.kits.components.BadgeDot
|
|
27
|
+
import vn.momo.kits.components.DotSize
|
|
28
|
+
import vn.momo.kits.components.Icon
|
|
29
29
|
import vn.momo.kits.const.Colors
|
|
30
30
|
|
|
31
31
|
data class HeaderRightData(
|
|
@@ -61,10 +61,6 @@ object Spacing {
|
|
|
61
61
|
val S = 8.dp
|
|
62
62
|
val XXS = 2.dp
|
|
63
63
|
}
|
|
64
|
-
object ComposeApiUtils {
|
|
65
|
-
fun parseParams(vararg params: Any) = ModelConvertUtils.encodeToJsonFromAny(params).toString()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
64
|
|
|
69
65
|
@Composable
|
|
70
66
|
fun HeaderRight(
|
|
@@ -85,19 +81,20 @@ fun ToolkitHeaderRight(headerRight: HeaderRightData? = null) {
|
|
|
85
81
|
val context = ApplicationContext.current
|
|
86
82
|
|
|
87
83
|
fun onPressShortcut() {
|
|
88
|
-
|
|
84
|
+
api?.request("onToolAction", mapOf("item" to mapOf("key" to "onFavorite"))) {
|
|
85
|
+
isFavorite = !isFavorite
|
|
86
|
+
}
|
|
89
87
|
}
|
|
90
88
|
|
|
91
89
|
fun onPressHelpCenter() {
|
|
92
|
-
val paramMap
|
|
90
|
+
val paramMap = mapOf(
|
|
93
91
|
"appId" to context?.appId,
|
|
94
92
|
"code" to context?.code,
|
|
95
93
|
"name" to context?.name,
|
|
96
94
|
"icon" to context?.icon,
|
|
97
95
|
"description" to context?.description
|
|
98
96
|
)
|
|
99
|
-
|
|
100
|
-
api?.request("showHelpCenter", params, { _ -> })
|
|
97
|
+
api?.request("showHelpCenter", paramMap, { _ -> })
|
|
101
98
|
}
|
|
102
99
|
|
|
103
100
|
fun onPressClose() {
|
|
@@ -105,7 +102,7 @@ fun ToolkitHeaderRight(headerRight: HeaderRightData? = null) {
|
|
|
105
102
|
}
|
|
106
103
|
|
|
107
104
|
fun onPressMore() {
|
|
108
|
-
val
|
|
105
|
+
val params = listOf(
|
|
109
106
|
headerRight?.tools ?: emptyList<ToolGroup>(),
|
|
110
107
|
mapOf(
|
|
111
108
|
"appId" to context?.appId,
|
|
@@ -116,7 +113,6 @@ fun ToolkitHeaderRight(headerRight: HeaderRightData? = null) {
|
|
|
116
113
|
)
|
|
117
114
|
)
|
|
118
115
|
|
|
119
|
-
val params = ComposeApiUtils.parseParams(param)
|
|
120
116
|
api?.request("showTools", params, { _ -> })
|
|
121
117
|
}
|
|
122
118
|
|
|
@@ -23,42 +23,25 @@ import vn.momo.kits.platform.disableOverscroll
|
|
|
23
23
|
import vn.momo.kits.platform.getStatusBarHeight
|
|
24
24
|
|
|
25
25
|
interface ComposeApi {
|
|
26
|
-
fun request(funcName: String, params:
|
|
27
|
-
fun request(funcName: String, params:
|
|
28
|
-
fun requestCallback(funcName: String, params:
|
|
26
|
+
fun request(funcName: String, params: Any?): String
|
|
27
|
+
fun request(funcName: String, params: Any?, onResponse: ((String) -> Unit)?): String
|
|
28
|
+
fun requestCallback(funcName: String, params: Any?, onResponse: ((String) -> Unit)?)
|
|
29
29
|
fun removeCallback(id: String)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
class Navigator {
|
|
33
|
-
fun push(content: @Composable () -> Unit) {
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
fun
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
fun reset(content: @Composable () -> Unit) {
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fun pop() {
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
fun replace(content: @Composable () -> Unit) {
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
fun popToTop() {
|
|
51
|
-
}
|
|
52
|
-
|
|
33
|
+
fun push(content: @Composable () -> Unit) {}
|
|
34
|
+
fun present(content: @Composable () -> Unit) {}
|
|
35
|
+
fun reset(content: @Composable () -> Unit) {}
|
|
36
|
+
fun pop() {}
|
|
37
|
+
fun replace(content: @Composable () -> Unit) {}
|
|
38
|
+
fun popToTop() {}
|
|
53
39
|
fun showModal(
|
|
54
40
|
onClose: () -> Unit = {},
|
|
55
41
|
canBackgroundClose: Boolean = true,
|
|
56
42
|
content: @Composable () -> Unit,
|
|
57
|
-
) {
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
fun showBottomSheet(content: @Composable () -> Unit) {
|
|
61
|
-
}
|
|
43
|
+
) {}
|
|
44
|
+
fun showBottomSheet(content: @Composable () -> Unit) {}
|
|
62
45
|
}
|
|
63
46
|
|
|
64
47
|
data class MiniAppContext(
|
|
@@ -85,7 +68,7 @@ fun ApplicationContainer(
|
|
|
85
68
|
composeApi: ComposeApi? = null,
|
|
86
69
|
statusBarHeight: Dp? = AppStatusBar.current ?: getStatusBarHeight(),
|
|
87
70
|
applicationContext: MiniAppContext? = null,
|
|
88
|
-
content: @Composable () -> Unit
|
|
71
|
+
content: @Composable () -> Unit,
|
|
89
72
|
) {
|
|
90
73
|
var appTheme by remember { mutableStateOf(theme) }
|
|
91
74
|
val coroutineScope = rememberCoroutineScope()
|
|
@@ -121,7 +104,7 @@ fun ApplicationContainer(
|
|
|
121
104
|
PlatformApi provides composeApi,
|
|
122
105
|
AppNavigator provides Navigator(),
|
|
123
106
|
AppStatusBar provides statusBarHeight,
|
|
124
|
-
ApplicationContext provides applicationContext
|
|
107
|
+
ApplicationContext provides applicationContext,
|
|
125
108
|
) {
|
|
126
109
|
content()
|
|
127
110
|
}
|
package/package.json
CHANGED
|
@@ -1,86 +0,0 @@
|
|
|
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
|
-
|
|
@@ -1,67 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,175 +0,0 @@
|
|
|
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,188 +0,0 @@
|
|
|
1
|
-
package vn.momo.kits.utils
|
|
2
|
-
|
|
3
|
-
import kotlinx.serialization.KSerializer
|
|
4
|
-
import kotlinx.serialization.builtins.ListSerializer
|
|
5
|
-
import kotlinx.serialization.encodeToString
|
|
6
|
-
import kotlinx.serialization.json.Json
|
|
7
|
-
import kotlinx.serialization.json.JsonArray
|
|
8
|
-
import kotlinx.serialization.json.JsonElement
|
|
9
|
-
import kotlinx.serialization.json.JsonNull
|
|
10
|
-
import kotlinx.serialization.json.JsonObject
|
|
11
|
-
import kotlinx.serialization.json.JsonPrimitive
|
|
12
|
-
import kotlinx.serialization.json.jsonObject
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
object ModelConvertUtils {
|
|
16
|
-
private const val discriminator = "momoClassDiscriminator"
|
|
17
|
-
val formatter = Json {
|
|
18
|
-
encodeDefaults = true
|
|
19
|
-
ignoreUnknownKeys = true
|
|
20
|
-
classDiscriminator = discriminator
|
|
21
|
-
}
|
|
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
|
-
|
|
53
|
-
fun encodeToJson(data: String?): JsonElement? = try {
|
|
54
|
-
formatter.parseToJsonElement(data ?: "")
|
|
55
|
-
} catch (e: Throwable) {
|
|
56
|
-
handleJsonError(e)
|
|
57
|
-
null
|
|
58
|
-
}
|
|
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
|
-
|
|
68
|
-
fun encodeToJson(data: Collection<*>): JsonElement {
|
|
69
|
-
val parsedData = mutableListOf<JsonElement>()
|
|
70
|
-
data.forEach { value ->
|
|
71
|
-
when (value) {
|
|
72
|
-
is Map<*, *> -> parsedData.add(encodeToJson(value))
|
|
73
|
-
is Collection<*> -> parsedData.add(encodeToJson(value))
|
|
74
|
-
is Number -> parsedData.add(JsonPrimitive(value))
|
|
75
|
-
is Boolean -> parsedData.add(JsonPrimitive(value))
|
|
76
|
-
is String -> parsedData.add(JsonPrimitive(value))
|
|
77
|
-
is Enum<*> -> encodeToJson(value.name)?.let { parsedData.add(it) }
|
|
78
|
-
is JsonPrimitive -> parsedData.add(value)
|
|
79
|
-
is JsonElement -> parsedData.add(value)
|
|
80
|
-
null -> parsedData.add(JsonNull)
|
|
81
|
-
else -> throw IllegalStateException("Can't serialize unknown collection type: $value")
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return JsonArray(parsedData)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
fun encodeToJson(data: Map<*, *>): JsonElement {
|
|
88
|
-
val parsedData = mutableMapOf<String, JsonElement>()
|
|
89
|
-
data.forEach { (key, value) ->
|
|
90
|
-
if (key is String && value != null) {
|
|
91
|
-
when (value) {
|
|
92
|
-
is Map<*, *> -> parsedData[key] = encodeToJson(value)
|
|
93
|
-
is Collection<*> -> parsedData[key] = encodeToJson(value)
|
|
94
|
-
is Number -> parsedData[key] = JsonPrimitive(value)
|
|
95
|
-
is Boolean -> parsedData[key] = JsonPrimitive(value)
|
|
96
|
-
is String -> parsedData[key] = JsonPrimitive(value)
|
|
97
|
-
is Enum<*> -> encodeToJson(value.name)?.let { parsedData[key] = it }
|
|
98
|
-
is Array<*> -> parsedData[key] = encodeToJsonFromArray(value)
|
|
99
|
-
is JsonPrimitive -> parsedData[key] = value
|
|
100
|
-
is JsonElement -> parsedData[key] = value
|
|
101
|
-
else -> throw IllegalStateException("Can't serialize unknown type: $value")
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return JsonObject(parsedData)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
fun encodeToJsonFromAny(data: Any): JsonElement {
|
|
109
|
-
return when (data) {
|
|
110
|
-
is Map<*, *> -> encodeToJson(data)
|
|
111
|
-
is Collection<*> -> encodeToJson(data)
|
|
112
|
-
is Number -> JsonPrimitive(data)
|
|
113
|
-
is Boolean -> JsonPrimitive(data)
|
|
114
|
-
is String -> JsonPrimitive(data)
|
|
115
|
-
is Enum<*> -> encodeToJson(data.name) ?: JsonNull
|
|
116
|
-
is Array<*> -> encodeToJsonFromArray(data)
|
|
117
|
-
is JsonElement -> data
|
|
118
|
-
else -> JsonNull
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private fun encodeToJsonFromArray(data: Array<*>): JsonArray =
|
|
123
|
-
JsonArray(data.filterNotNull().map { encodeToJsonFromAny(it) })
|
|
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
|
-
|
|
188
|
-
}
|