- Made for Deno
- works with Deno Deploy
- Pure Typescript
- Lightweight
- Extendable
- Zero third party dependencies (only deno_std)
Currently there are no built-in types. But this is planned for the future but until then you can implement it yourself (or open a merge request 🤠 )
import * as EJSON from 'https://deno.land/x/ejson/mod.ts';
import * as Base64 from 'https://deno.land/[email protected]/encoding/base64.ts';
// Add date extention
EJSON.addEncoder(Date, (date) => {
return { $date: { $numberLong: date.getTime().toString() } };
});
EJSON.addDecoder('$date', (obj) => {
const num = Number.parseInt(obj.$numberLong);
return new Date(num);
});
// Add Uint8Array extention
EJSON.addEncoder(Uint8Array, (buffer) => {
return { $binary: { base64: Base64.encode(buffer) } };
});
EJSON.addDecoder('$binary', (obj) => {
return Base64.decode(obj.base64);
});
const obj = [new Date(), { myBin: new Uint8Array([0xff, 0x7f]) }];
console.log('obj', obj);
// obj [ 2022-04-15T18:06:07.820Z, { myBin: Uint8Array(2) [ 255, 127 ] } ]
const stringified = EJSON.stringify(obj);
console.log('stringified', stringified);
// stringified [{"$date":{"$numberLong":"1650045944276"}},{"myBin":{"$binary":{"base64":"/38="}}}]
const parsed = EJSON.parse(stringified);
console.log('parsed', parsed);
// parsed [ 2022-04-15T18:06:00.332Z, { myBin: Uint8Array(2) [ 255, 127 ] } ]
- add built-in types
--
Feel free to open merge requests!
MIT