krtko
03/16/2018, 12:06 AMexpect class Json {
fun getInt(id: String): Int
But with JS, the native Json class is only an interface so implementing it like:
actual class Json: kotlin.js.Json {
actual fun getInt(id: String): Int {
return this[id] as? Int ?: 0
}
Does not work. Any suggestions?wickedev
03/16/2018, 2:37 AMactual typealias Json = JsonJS
class JsonJS : kotlin.js.Json {
override fun get(propertyName: String): Any? {
TODO("not implemented")
}
override fun set(propertyName: String, value: Any?) {
TODO("not implemented")
}
fun getInt(id: String): Int {
return this[id] as? Int ?: 0
}
}
krtko
03/16/2018, 3:25 AMkrtko
03/16/2018, 3:26 AMkrtko
03/16/2018, 3:26 AMkrtko
03/16/2018, 5:53 AMwickedev
03/16/2018, 6:16 AMprintln((js("""({"hello":"world!"})""") as Json)["hello"])
krtko
03/16/2018, 6:19 AMwickedev
03/16/2018, 6:20 AMdata class Hello(val hello: String)
val hello = JSON.parse<Hello>("""({"hello":"world!"})""")
krtko
03/16/2018, 6:20 AMkrtko
03/16/2018, 6:22 AMkrtko
03/16/2018, 6:22 AMexpect class Json() {
fun getInt(key: String): Int
companion object {
fun FromString(string: String): Json?
}
}
krtko
03/16/2018, 6:24 AMactual typealias Json = JsonJs
class JsonJs: kotlin.js.Json {
override operator fun get(propertyName: String): Any? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override operator fun set(key: String, value: Any?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
fun getInt(key: String): Int {
return this[key] as? Int ?: 0
}
companion object {
fun FromString(string: String): JsonJs? {
return JSON.parse<JsonJs>(string)
}
}
}
krtko
03/16/2018, 6:27 AMval json = JsonJs.FromString("{\"hello\":2}")
println(json["hello"])
Causes an error because json doesn’t have the “get” methodkrtko
03/16/2018, 7:07 AMwickedev
03/16/2018, 8:39 AMwickedev
03/16/2018, 8:39 AMkrtko
03/16/2018, 8:59 AMkrtko
03/16/2018, 8:59 AMgildor
03/20/2018, 3:25 AM