socketcluster-client icon indicating copy to clipboard operation
socketcluster-client copied to clipboard

Option to decode JSON date string

Open hongnk opened this issue 6 years ago • 1 comments

When sending Date objects, they are serialized into ISO string format, and JSON parse doesn't convert them back.

Propose to add option to JSON decode function, for example

const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;

function dateReviver(key, value) {
    if (typeof value === "string" && dateFormat.test(value)) {
        return new Date(value);
    }
    
    return value;
}

module.exports.decode = function(str) {
     return JSON.parse(str, dateReviver)
}

hongnk avatar Jul 17 '18 18:07 hongnk

How about adding EJSON support?

Pagebakers avatar Aug 28 '19 13:08 Pagebakers