Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dinoboff/firebase-json

Parser for Firebase rules JSON files
https://github.com/dinoboff/firebase-json

Last synced: about 1 month ago
JSON representation

Parser for Firebase rules JSON files

Awesome Lists containing this project

README

        

# firebase-json

Parser for Firebase rules JSON files.

It supersedes [RFC 7159] with those additions:

- support multi line ("/* [...] */") and single line ("// [...]") comments;
- support multi line strings (without escaping the line feed);
- disallow duplicate keys in objects;
- and allow trailing commas in objects and array.

## Example

To parse Firebase rules:

```js
const json = require('firebase-json');
const fs = require('js');
const rules = json.parse(fs.readFileSync('./rules.json', 'utf8'));

console.log(rules);
```

## Motivation

This is primarily used to load rules for [targaryen] test.

The alternatives are:

- To strip the comment with [strip-json-comments] and to not use multi line
string.
- To write the rules in [json5] and compile them before testing them in
[targaryen] or upload them (json5 rules using multi line string won't be
compatible with Firebase format).
- To write the rules in Firebase's [bolt] and compile the rules before testing
them with [targaryen]; for deployment, [firebase-tools] support bolt rules.

Those solutions might limit or disrupt your current work flow or deployment.
Parsing the rule might also allow us later to map a rule syntax or evaluation
error to a specific place in the JSON file similarly to Firebase simulator.

## Installation

```shell
npm install firebase-json
```

## API

- `parse(json: rules): any`

Parse the firebase-json encoded string.

- `load(filePath: string, options: void|string|object): Promise`

Read the file and parse its firebase-json encoded content.

- `loadSync(filePath: string, options: void|string|object): any`

Read the file synchronously and parse its firebase-json encoded content.

- `ast(json: rules): {type: string, expression: object, loc: object}`

Parse the firebase-json encoded string into an to intermediary AST. It uses
the ESTree AST schema and returns an "ExpressionStatement" node.

## Tests

```shell
git clone [email protected]:dinoboff/firebase-json.git
cd firebase-json
npm i
npm test
```

## Contributors

Firebase-json is maintained by [Damien Lebrun].

The parser is generated by [David Majda]'s [Pegjs]. The firebase-json grammar
definition is based on Pegjs JSON grammar example.

## License

MIT License

Copyright (c) 2017 Damien Lebrun

[RFC 7159]: http://tools.ietf.org/html/rfc7159
[targaryen]: https://github.com/goldibex/targaryen
[strip-json-comments]: https://github.com/sindresorhus/strip-json-comments
[json5]: http://json5.org/
[bolt]: https://github.com/firebase/bolt
[firebase-tools]: https://github.com/firebase/firebase-tools
[Pegjs]: https://pegjs.org/
[Damien Lebrun]: https://github.com/dinoboff
[David Majda]: https://majda.cz/