https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

krtko

03/15/2018, 11:31 PM
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?
s

spand

03/16/2018, 8:26 AM
If you read up a bit then Nat Pryce seems to have the same problem. I am sure he found a good solution other than not using multiplatform.
k

krtko

03/16/2018, 9:02 AM
I will look up Nat Pryce and see what their solution was. Thanks for that recommendation. My own ended up having me create an access helper class. Not perfect, but it seemed the only dependable way to resolve it.
g

gildor

03/16/2018, 10:37 AM
Do you really need Json interface from kotlin-js? Just found it not very useful, maybe own implementation (just in a different package) would be good enough
k

krtko

03/16/2018, 9:33 PM
I am sharing my JSON de/serialization across Android, Server, and now JS
One day I hope to bring this to our iOS app
g

gildor

03/18/2018, 4:31 AM
Yes, I understand, but why do you need Json interface itself, it's quite useless. If I would use serialization across different platforms I probably will try to use Kotlinx.serialization or just some simple Json serialization interface implemented on each platform to map Json to data classes (using Json on just and something like Gson on JVM)
k

krtko

03/19/2018, 1:12 AM
I find things like Gson, Jackson, etc obtuse to use. I prefer to create a Json Map class directly out of the String and then map that to my data classes. I haven’t looked into Kotlinx.serialization. I will check that out.
g

gildor

03/19/2018, 7:48 AM
Map to data class manually?
7 Views