In an mpp, I am serializing to and from JSON, but ...
# javascript
h
In an mpp, I am serializing to and from JSON, but using a map internally. I have been having trouble on the deserialization end in JS, but solved most of it with a custom objectToMap function that could populate a map from the JSON object in javascript. But. Lists go into JSON with an array-like notation and are re-read as arrays. From there, I need to get them back into kotlin lists. Could anyone help me get it right? Here's my last attempt (which doesn't work, since toList() isn't a function in js:
d
Kotlin arrays are javascript arrays, so just check if
value is Array<*>
and then call
value.toList()
h
You mean by first casting the dynamic type
obj[key]
to an array? Yes, that might work. I'll check.
d
If you do an
is
check it should smartcast for you
h
Yes, it should, but it doesn't. Maybe because I'm dealing with a dynamic type object.
d
Your example code does not work, since
obj[key]
has a dynamic type, so the function call is translated to javascript as-is. But
toList
is an extension function, so it needs special handling at compile time, which only works if the type is known
h
Yes. But explicitly casting to an array seems to work, so thanks!