JSON encoder that ignores keys that can't be serialized

I’d like to send arbitrary data to the server for processing. Some of that data are DOM events. DOM events can’t be encoded as JSON because some of the keys point to non-JSON encodeable values. Is there any JSON encoder that looks at the arguments and ignores what it can’t serialize? This would allow me to have an API like: sendToServer([arg1, arg2, arg3]) in which some of the arguments may be JS events.

AFAIK there is not such a thing. And to be honest, if a serializer finds unserializable data it should barf at me, since just filtering it out silently might change the meaning of the remaining data.

If you only want to serialize a subset of your data, the way to go is to preprocess it and convert it that it only contains the stuff you want to serialize (or are able to).

1 Like

Yes, I can preprocess the maps so that they contain only serializable keys and values, but I was hoping such thing already existed. Basically I want to allow the user to setup event listeners that process the event on the server. Obviously I can’t send the whole DOM to the server, so I’m ok with losing a bit of information. So I guess I’ll write a function to prepreocess the JSON data myself…

Yeah you really do not just want to serialize up random events, they can be substantially larger than you may otherwise expect at times, instead always whitelist what to take. ^.^

1 Like

Yes, they’re large. Often the full event won’t be needed, especially for the kinds of things you’d do serverside (It’s only important that the event was triggered), and I can always tell the user to send individual properties as arguments. Actually filtering the event for the interesting properties might pose some complications due to some project-specific constraints, but it’s probably the roght way to go.

Could always specify a decoder to use to choose what to pick out of the event, like I do in bucklescript-tea?