From 09d81e12fd4a41132cb711c282e98425dc1334cf Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 8 Mar 2018 08:39:20 +0100 Subject: Extension function to convert Json to Map --- src/main/kotlin/pl/treksoft/kvision/form/Form.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') 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.bool(key: String): Boolean? = this[key] as? Boolean fun Map.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.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 { + val map = mutableMapOf() + @Suppress("UnsafeCastFromDynamic") + for (key in js("Object").keys(this)) map[key] = this[key] + return map +} -- cgit