https://github.com/lkwr/ejson
Implementation of Extended JSON (EJSON) in Deno.
https://github.com/lkwr/ejson
deno deno-module denoland ejson json
Last synced: 4 months ago
JSON representation
Implementation of Extended JSON (EJSON) in Deno.
- Host: GitHub
- URL: https://github.com/lkwr/ejson
- Owner: lkwr
- License: mit
- Created: 2022-04-15T17:42:38.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-15T18:27:53.000Z (about 4 years ago)
- Last Synced: 2025-08-20T08:59:01.887Z (9 months ago)
- Topics: deno, deno-module, denoland, ejson, json
- Language: TypeScript
- Homepage: https://deno.land/x/ejson
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### NOTICE: This project is currently experimental and may not work properly!
# EJSON
#### Implementation of Extended JSON (EJSON) in Deno. You can use it for example in MongoDB DataAPI.





## Key Features
- Made for [Deno](https://deno.land)
- works with [Deno Deploy](https://deno.com/deploy)
- Pure Typescript
- Lightweight
- Extendable
- Zero _third party_ dependencies (only deno_std)
## How To Use
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 🤠)
```ts
import * as EJSON from 'https://deno.land/x/ejson/mod.ts';
import * as Base64 from 'https://deno.land/std@0.135.0/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 ] } ]
```
## TODO
- add built-in types
## Known issues
--
## Contributing
Feel free to open merge requests!
## License
MIT
---
> Homepage [luke.id](https://luke.id)  · GitHub
> [@lkwr](https://github.com/lkwr)  · Twitter
> [@wlkrlk](https://twitter.com/wlkrlk)