Is this something <#C7A1U5PTM|serialization> can e...
# javascript
a
Is this something #serialization can easily handle?
e
Serialization indeed has a helper for that that would let you convert JSON blobs into properly typed Kotlin objects.
f
Unfortunately it is missing the very important other side of converting a typed kotlin object back to a json blob
e
typed kotlin object is a JSON blob
(just pass it to JS, that is)
f
So If I have a data class and assign that to a child property of an object,
JSON.stringify
that parent object within javascript all the attributes of my data class are getting correctly converted to JSON?
If that is true I wonder why the ticket https://github.com/Kotlin/kotlinx.serialization/issues/116 is still open and not closed with a remark like this? I would need Kotlin Data Class => JS Object => Kotlin Data Class and based on my understanding only JS Object => Kotlin Data class works with the help of the serialization library
e
Not always. For example, if you have Long values there then it will not. But if you are talking here about interop with JS, then I suppose that you already do not have Longs or other types that JS cannot handle.
So you need kotlin’s version of
JSON.stringify
if you want to preserve all the Kotlin types (and all the custom serializers you might have defined). For example, if you plan to deserialize that in your JVM backend back into those Kotlin objects.
f
My problem is I wanna use Kotlin for a Programming Game (Screeps) and Screeps is using Javascript. So for example to write to persistent memory I need to add properties to a existing Memory Object. Currently the only way would be to write my Kotlin Memory state into a string. Put that into the Game Memory and then reparse the string (obviously rather slow). Preferable I would be able to serialize and deserialize my data class into a plain JS Object so i don't have double JSON parsing and creating.
e
Hm….
If you avoid kotlin-specific type that you should be fine just passing your data objects to Javascript
On the way back you can either deserialize our use another trick — just declare
external interface
with all the properties you expect there and use it.
f
Yeah I was thinking of that too. But then I have to duplicate my data types. One for internal use and one for external. Just not very comfortable. Besides currently I am more surprised that similar JS and Kotlin code Kotlins JS output has 10x the size of plain JS with similar functionality
g
@fkrauthan If I'm understanding correctly (correct me if I'm wrong), you are programming a game that runs in the browser, and you are storing the state in the cache. The kotlin classes are javascript objects, so you could store them directly in the cache, no?
f
@gbaldeck partial right. The game is offering me a JS Object that I can write you and internal the game persists that into a mongodb and restores it for the next tick of the game (its tick based)
g
ah I see, and kotlin objects mangle their property names in certain situations so getting the values back out would be difficult
What I would do in this situation, is define your state in terms of external interfaces, then use the jsObject function defined in the kt file in the link below to initalize them
not ideal but it will give you plain javascript objects
f
Yeah I know. But like I said before that requires me to pretty much duplicate my object definitions and is not very user friendly form a development point of view.
👍 1
g
could always use
@JsName
annotation and check the compiled JS to make sure it is giving you exact property names
f
Or I just wait a bit longer until the JS compiler is getting more optimized and the serialization library implements the Data Class to JS Object mode in addition to the JS Object to Data Class mode
g
sure if you're willing to wait
out of curiosity, what js game framework are you using?
f
Not writing a game but writing a bot for the game screeps (https://docs.screeps.com/) But when I do some canvas game development I use Phaser but never tried it with Kotlin yet
g
that looks pretty cool tbh, I'm gonna try it out