I'm writing a service in nodejs
that replaces an existing system written in .NET
. The service provides a JSON API, one of the calls returns a date. The Microsoft date format for JSON was/is where 1599890827000
is the milliseconds offset:
/Date(1599890827000)/
The problem I am having is that JSON.stringify
(used in res.send
and res.json
in express
) does not escape forward slashes but the existing Microsoft library (System.Web.Script.Serialization.JavaScriptSerializer) expects forward slashes to be escaped.
For example, the client expects JSON like this:
{
"Expires": "\/Date(1599890827000)\/"
}
But JSON.stringify
produces the following:
{
"Expires": "/Date(1599890827000)/"
}
The second result is perfectly valid but the Microsoft library doesn't like it and fails to parse.
Is there any way I can force Express/Node/JSON to escape forward slashes in JSON.stringify
or handle this case?
I could use a regex replacement after running stringify
but because of an object caching system we use in the project it would be very hacky to have to convert to JSON before sending to the client instead of letting.
Note: I cannot change the client, only the api service.