SocketCluster / socketcluster-client

JavaScript client for SocketCluster

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to decode JSON date string

hongnk opened this issue · 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)
}

How about adding EJSON support?