hallvard
01/25/2019, 3:32 PMtime=1548429717649
. But I am generating JSON, and am converting my map to an object before doing JSON.stringify()
in order to avoid cyclic references from the map, and I see the long values represented as objects, and in the final JSON, they are represented thus: "time":{"low_":-2053476207,"high_":360}
. I guess this has to do with dealing with 64bit numbers in javascript, but my question is: How do I detect this and how do I convert them? Using typeof
as I pull them out of my map, I only get object
...gildor
01/26/2019, 10:04 AMgildor
01/26/2019, 10:05 AMgildor
01/26/2019, 10:05 AMSvyatoslav Kuzmich [JB]
01/26/2019, 10:32 AMx instanceof Kotlin.Long
to detect instance of Kotlin Long on JS side.hallvard
01/26/2019, 2:28 PMinternal actual fun getTimeImpl(): Long = Date().getTime().toLong()
It gets compiled into this javascript code:
function getTimeImpl() {
return Kotlin.Long.fromNumber((new Date()).getTime());
}
And to convert from a map to a javascript object, I have this:
internal fun mapToObject(map: Map<String, *>): dynamic {
return js("""
var toReturn = {};
var i = map.entries.iterator();
while (i.hasNext()) {
var element = i.next();
if (element.value.toNumber) {
toReturn[element.key] = element.value.toNumber();
} else {
toReturn[element.key] = element.value;
}
}
return toReturn;
"""
)
}
... Oh wait!hallvard
01/26/2019, 2:29 PMhallvard
01/26/2019, 2:30 PMmapToObject
workaround and am satisfied for now.gildor
01/27/2019, 8:06 AMhallvard
01/27/2019, 9:13 AMSvyatoslav Kuzmich [JB]
01/27/2019, 7:16 PMtoNumber
field, which you might not want to call.
3. It will miss `Long`s in nested structures like mapOf("foo" to arrayOf(1L, 2L))
Also, it can be written in Kotlin:
val toReturn = js("{}")
for ((key, value) in map) {
toReturn[key] = when(value) {
is Long -> value.toDouble()
else -> value
}
}
return toReturn
hallvard
01/28/2019, 7:51 AM