kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Consider making decodeFromDynamic the default in Kotlin/JS

Open swankjesse opened this issue 3 years ago • 2 comments
trafficstars

I just landed a 35% performance improvement in my application by replacing this:

Json.decodeFromString(deserializer, string)

with this:

Json.decodeFromDynamic(deserializer, JSON.parse(string))

Would you consider changing decodeFromString() to delegate to JSON.parse() by default? I expect the browser’s built-in implementation to be as-fast or faster most of the time!

(I also replaced Json.encodeToString(serializer, value) with JSON.stringify(Json.encodeToDynamic(serializer, value)).)

swankjesse avatar Apr 05 '22 19:04 swankjesse

That's indeed true, but I'm afraid it can't be drop-in replacement, as there are several known limitations of dynamic decoding (https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/decode-from-dynamic.html): it can't support non-string Map keys and doesn't work properly with big Long numbers. There are also probably some implementation-defined bugs or inconsistencies that we don't know yet about.

However, for basic cases such as yours it may be recommendation to use this path. Either way, decodeFromDynamic is now missing in our guide entirely, so we at least need to add it there.

sandwwraith avatar Apr 06 '22 13:04 sandwwraith

@sandwwraith hey there! Do you mind adding the js tag?
Makes it easier for me (and maybe others) to find this JS-related discussion.

lppedd avatar Aug 06 '23 16:08 lppedd