SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
290 stars 92 forks source link

Option to decode JSON date string #120

Open hongnk opened 5 years ago

hongnk commented 5 years ago

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)
}
Pagebakers commented 4 years ago

How about adding EJSON support?