I'm aware (from this) I can explicitly convert a value to an integer before passing it to JSON.stringify
. But this approach is not useful when you want to serialize a form data. I have a generic code:
let myform='#my_form_id`;
$(myform).submit(function(event) {
event.preventDefault();
const formData = new FormData($(myform)[0]);
const json = JSON.stringify(Object.fromEntries(formData));
ws.send(json); // send over websocket
});
And I really don't want to manually convert each field to the appropriate type before serialization. It's against all the rules of a "well-written" code.
So, how can I serialize the form fields in JSON keeping their native data types?