Skip to content

Instantly share code, notes, and snippets.

@mikeal
Created April 27, 2012 00:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeal/2504336 to your computer and use it in GitHub Desktop.
Save mikeal/2504336 to your computer and use it in GitHub Desktop.
Date parsing JSON
JSON._dateReviver = function (k,v) {
if (v.length !== 24 || typeof v !== 'string') return v
try {return new Date(v)}
catch(e) {return v}
}
JSON.parseWithDates = function (obj) {
return JSON.parse(obj, JSON._dateReviver);
}
@visnup
Copy link

visnup commented Apr 27, 2012

ooh

@polotek
Copy link

polotek commented Apr 27, 2012

var _parse = JSON.parse;
function dateReviver(k, v) {
  try {return new Date(v)} 
  catch(e) {return v}
}

JSON.parse = function(obj, reviver) {
   return _parse(obj, function(k, v) {
      if(typeof reviver === 'function') {
        v = reviver(k, v);
      }
      if (typeof v === 'string' && v.length === 24) {
        v = dateReviver(k, v);
      }
      return v;
   });
}

@polotek
Copy link

polotek commented Apr 27, 2012

the above is off the cuff and totally untested.

@mikeal
Copy link
Author

mikeal commented Apr 27, 2012

i wouldn't override the global one, other people are going to assume certain properties are strings when they aren't.

@mikeal
Copy link
Author

mikeal commented Apr 27, 2012

also, this is always going to be slower than parsing without a reviver, so i wouldn't override it globally.

@polotek
Copy link

polotek commented Apr 27, 2012

Sure, it's a judgment call. But if most of the json you're parsing has dates and you use dates a lot and you want them to be date objects, this is way better than always wondering which one you should use.

@visnup
Copy link

visnup commented Apr 27, 2012

yeah, I wouldn't either. I'd just use it on json I'd suspect has dates in it so that I wouldn't have to individually Date.parse them afterwards.

@visnup
Copy link

visnup commented Apr 27, 2012

@polotek, good point though.

@mikeal
Copy link
Author

mikeal commented Apr 27, 2012

what i was trying to say was, if you override it globally and then use someone else's library which internally uses JSON.parse they will get results they are not expecting, which would be very bad. if you want it to be this way in your code just always use this JSON function but don't override the global.

@polotek
Copy link

polotek commented Apr 27, 2012

How's this?

JSON._dateReviver = function (k,v) {
  if (v.length !== 24 || typeof v !== 'string') return v
  try {return new Date(v)} 
  catch(e) {return v}
}

JSON.parseWithDates = function (obj) {
  return JSON.parse(obj, JSON._dateReviver);
}

@mikeal
Copy link
Author

mikeal commented Apr 27, 2012

stealing.

@max-mapper
Copy link

i'd use JSON.dateyParse or JSON.timeyParse or something fun like that. think rachel ray: yummo! sammies!

@yocontra
Copy link

JSON.dateParsely fits standard naming convention

@visnup
Copy link

visnup commented Apr 27, 2012 via email

@mark-hahn
Copy link

How about dateify? I've always thought stringify was weird so why not keep up the tradition?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment