Hello I am porting part of our Android app to JS v...
# javascript
k
Hello I am porting part of our Android app to JS via the Multiplatform set up. Its pretty awesome but I am having one issue with the expect/actual set up in JS. I am trying to port over my Json Serialization layer. So I created an Expect Json class like:
Copy code
expect class Json {
    fun getInt(id: String): Int
But with JS, the native Json class is only an interface so implementing it like:
Copy code
actual class Json: kotlin.js.Json {
    actual fun getInt(id: String): Int {
        return this[id] as? Int ?: 0
    }
Does not work. Any suggestions?
w
Copy code
actual 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
    }
}
k
Oh
That simple
thanks 🙂
This doesn’t seem to work with JSON.parse<JsonJs>(string)
w
You don’t need to implement Json interface. kotlin.js.Json interface is for js native Interoperability.
Copy code
println((js("""({"hello":"world!"})""") as Json)["hello"])
k
Ah I’m trying to make a cross platform implementation of JSON
w
Copy code
data class Hello(val hello: String)
val hello = JSON.parse<Hello>("""({"hello":"world!"})""")
k
and since kotlin.js treats JSON.parse just like native JSON. None of the class methods I have on my expect Json class work
Ok so my JSON layer should be totally at object level and I should mock every JSON interaction with a Data class? Theres no way to create a cross platform generalized JSON class?
for example I have
Copy code
expect class Json() {
    fun getInt(key: String): Int
    companion object {
        fun FromString(string: String): Json?
    }
}
Copy code
actual 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)
        }
    }
}
Copy code
val json = JsonJs.FromString("{\"hello\":2}")
println(json["hello"])
Causes an error because json doesn’t have the “get” method
I guess going the expect/actual route is the right direction. I’ll just make a helper class i guess
It works fine on jvm and js both platform
k
Alright I am doing a different approach that avoids reflection.
Thanks for your solution 🙂
👍 1
g
This approach from Ryan gist doesn’t use reflections (except Gson, but you can choose any solution for parsing json on JVM, or Gson with code generated adapters), it’s what I suggested on my previous comment to your problem. Just map json string directly to kotlin class using platform tools