json-native
Parse and stringify native javascript types
json-native allows you to store native types like Dates, RegExps and Functions in json and retrieve them back in their native form.
Install
from npm
npm install --save json-native
API
#stringify(subject)
This takes any valid javascript type and converts it to json format. For most types this will not be any different than JSON.stringify
. For Dates, RegExps and Functions however, this will provide a key to allow the value to be converted back to it's native form.
var jn = ; // same as normaljn; // => '{"k":"val"}' // special casejs; // => '{"d":"{"__type":"date","__value":"2015-03-06T18:25:11.682Z"}"}'
#parse(json)
This takes any valid json and converts it to it's native form. Contrary to JSON.parse
, this will also convert types like Dates, RegExps or Functions to their native form, so longs as the json was stringifed via #stringify
.
var jn = ; var date = ;var json = jn; date === jnd; // => true
License
MIT