aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-03-08 08:39:20 +0100
committerRobert Jaros <rjaros@finn.pl>2018-03-08 08:39:20 +0100
commit09d81e12fd4a41132cb711c282e98425dc1334cf (patch)
treef6b581cfaddfe4a154b666ee0b31b8c7c3cbc8e0 /src/main
parentccf8095516c7cfae68f2ad612bcdb1e2d6420786 (diff)
downloadkvision-09d81e12fd4a41132cb711c282e98425dc1334cf.tar.gz
kvision-09d81e12fd4a41132cb711c282e98425dc1334cf.tar.bz2
kvision-09d81e12fd4a41132cb711c282e98425dc1334cf.zip
Extension function to convert Json to Map
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/form/Form.kt14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
index 1b06156e..4ce21ec6 100644
--- a/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/form/Form.kt
@@ -263,10 +263,22 @@ fun Map<String, Any?>.bool(key: String): Boolean? = this[key] as? Boolean
fun Map<String, Any?>.date(key: String): Date? = this[key] as? Date
/**
- * Returns map values in JSON format.
+ * Extension function to convert Map to JSON.
+ * @return Json object
*/
fun Map<String, Any?>.asJson(): Json {
val array = this.entries.map { it.component1() to it.component2() }.toTypedArray()
@Suppress("SpreadOperator")
return kotlin.js.json(*array)
}
+
+/**
+ * Extension function to convert JSON to Map.
+ * @return map object
+ */
+fun Json.asMap(): Map<String, Any?> {
+ val map = mutableMapOf<String, Any?>()
+ @Suppress("UnsafeCastFromDynamic")
+ for (key in js("Object").keys(this)) map[key] = this[key]
+ return map
+}