Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Option to decode JSON date string #120

Open
hongnk opened this issue Jul 17, 2018 · 1 comment
Open

Option to decode JSON date string #120

hongnk opened this issue Jul 17, 2018 · 1 comment

Comments

@hongnk
Copy link

hongnk commented Jul 17, 2018

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
Copy link

How about adding EJSON support?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants