-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date parsing #986
Comments
Ok, I found a workaround for the issue. On a client: var request = require('superagent')
request.parse['application/json'] = text => JSON.parse(text, JSON.date_parser) On a server: var request = require('superagent')
request.parse['application/json'] = function parseJSON(res, fn){
res.text = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ res.text += chunk;});
res.on('end', function(){
try {
var body = res.text && JSON.parse(res.text, JSON.date_parser);
} catch (e) {
var err = e;
// issue #675: return the raw response if the response parsing fails
err.rawResponse = res.text || null;
// issue #876: return the http status code if the response parsing fails
err.statusCode = res.statusCode;
} finally {
fn(err, body);
}
});
}; JSON.date_parser: if (!JSON.date_parser)
{
var ISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
JSON.date_parser = function(key, value)
{
if (typeof value === 'string' && ISO.test(value))
{
return new Date(value)
}
return value
}
} |
There is no date datatype in JSON, so I think superagent's behavior is correct. Your solution is fine. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As far as I can see currently you only parse dates as strings:
https://github.com/visionmedia/superagent/blob/b91a26fb4d8acf28534ac2cdd0bb3ce4a6f17498/lib/node/parsers/json.js#L8
This code can't handle Dates properly.
What if I add a second parameter to JSON.parse() to handle Dates properly?
https://github.com/halt-hammerzeit/react-isomorphic-render/blob/249723de77323071517a07a3774cdcf4f14d6413/source/page-server/html.js#L123
It could be an option say
parseDates: true
The text was updated successfully, but these errors were encountered: