Skip to content

Commit

Permalink
Merge pull request #13 from gfiorentino/master
Browse files Browse the repository at this point in the history
merge master
  • Loading branch information
gfiorentino committed Mar 14, 2021
2 parents daa70d0 + 2f4cd06 commit f481ea4
Show file tree
Hide file tree
Showing 40 changed files with 20,093 additions and 2,201 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2.1
orbs:
node: circleci/node@3.0.0
workflows:
node-tests:
jobs:
- node/test
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules/*
node_modules/
dist
**/private
citylots.json
.nyc_output/

4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
spec/
.vscode/
dev/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "14"
cache:
directories:
- ./node_modules

install:
- npm install

script:
- npm run coverage

after_success: npm run coveralls
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"program": "${workspaceRoot}\\node_modules\\jasmine-ts\\lib\\index",
"args": ["--config=./spec/support/jasmine.json", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"name": "Express attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 gfiorentino

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
136 changes: 128 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,132 @@
# PIFFERO
```
_________________________ .
=(____o_o_o_o_|_o_o_o_o____| |
'
```
# PIFFERO [![Build Status](https://travis-ci.com/gfiorentino/piffero.svg?branch=master)](https://travis-ci.com/github/gfiorentino/piffero) [![Coverage Status](https://coveralls.io/repos/github/gfiorentino/piffero/badge.svg?branch=master)](https://coveralls.io/github/gfiorentino/piffero?branch=master) [![NPM version](https://img.shields.io/npm/v/piffero.svg)](https://www.npmjs.com/package/piffero)
The ultimate JSON SAX parser

the ultimate JSON SAX parser
Piffero is an open source SAX parser who work directely on the streams to get parts of big JSON files.

Piffero can load big files larger than memory used and return the required content in a stream.

```
______________________________ . ♪
=(____|_o_o_o_o_|_o_o_o_o_|*____| | ♫
'
```
# Description

No piffero is done yet
The version 1.0.0 only support the full qualified JSONpath

example: `$.father.son.array[:index].attribute`

The sintax that will be implemented in the next future and the examples from [Stefan Goessner's post](http://goessner.net/articles/JsonPath/)

JSONPath | Description |Implemented
-------------------|----------------------------------------------------------------------|------------
`$` | The root object/element | ✅
`@` | The current object/element |
`.` | Child member operator | ✅
`..` | Recursive descendant operator; JSONPath borrows this syntax from E4X |
`*` | Wildcard matching all objects/elements regardless their names |
`[]` | Subscript operator | ✅
`[,]` | Union operator for alternate names or array indices as a set |
`[start:end:step]` | Array slice operator borrowed from ES4 / Python |
`?()` | Applies a filter (script) expression via static evaluation |
`()` | Script expression via static evaluation |

# Get Started

*Step 1*: install Piffero

```bash
npm i piffero
```

*Step 2*: use Piffero

Piffero is easy to use, you only need to call one of the methods:

`findByPath(inputStream: Stream, jsonpath: string): Stream`

or

`findAsString(callback: (result: any, err?: any) => void, inputStream: Stream, jsonpath: string): void`

## Examples

For example, if you have this json:

#### employees.json
```js
{
"employees":[
{
"name":"John",
"surname":"Doe",
"phoneNumbers":[
{
"type":"iPhone",
"number":"0123-4567-8888",
"test":false
},
{
"type":"home",
"number":"0123-4567-8910",
"test":true
}
]
},
{
"name":"Joe",
"surname":"Black",
"phoneNumbers":[
]
}
]
}
```

and you want extract the second employees "Joe Black", you need to call `findByPath` with the json path `$.employees[1]`, eg:

```js
const piffero = require('piffero');
const fs = require('fs');

// create read stream of json
const inputStream = fs.createReadStream('./employees.json');
// pass the stream to Piffero with the json path
const resultStream = piffero.Piffero.findByPath(inputStream, "$.employees[1]");

// trasform the stream to string and print into console
const chunks = [];
resultStream.on('data', chunk => chunks.push(chunk));
resultStream.on('end', () => console.log(Buffer.concat(chunks).toString('utf8')));
```
#### console result
```js
[{"name":"Joe","surname":"Black","phoneNumbers":[]}]
```

Otherwise, if you want the second phone number of the first employees, you need to call `findByPath` with the json path `$.employees[0].phoneNumbers[1]`, eg:

```js
const piffero = require('piffero');
const fs = require('fs');

// create read stream of json
const inputStream = fs.createReadStream('./employees.json');
// pass the stream to Piffero with the json path
const resultStream = piffero.Piffero.findPath(inputStream, '$.employees[0].phoneNumbers[1]');

// trasform the stream to string and print into console
const chunks = [];
resultStream.on('data', chunk => chunks.push(chunk));
resultStream.on('end', () => console.log(Buffer.concat(chunks).toString('utf8')));
```
#### console result
```js
[{"type":"home","number":"0123-4567-8910","test":true}]
```
## Other tools
* Piffero is built on [Clarinet](https://github.com/dscape/clarinet)
* You can try [Oboe](https://github.com/jimhigson/oboe.js)
* You can also try [JSONPath Online Evaluator](https://jsonpath.com/)

enjoy piffero!!
53 changes: 53 additions & 0 deletions benchmarking/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* RUN TEST: node index.js
*/

var fs = require("fs");
var performance = require('perf_hooks').performance;
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;


// JsonPath streaming libs
var oboe = require('./oboe-node.js');
var Piffero = require('../dist/index.js').Piffero;

// config
var JSON_FILE = '../spec/jsonFiles/large.json';
var JSON_PATH = '[2].tags[2]';

// run test ...

suite.add('oboe', {
defer: true,
fn: function (deferred) {

oboe(fs.createReadStream(JSON_FILE)).node(JSON_PATH, function (result) {
this.abort();
deferred.resolve();
});

}
})
.add('Piffero', {
defer: true,
fn: function (deferred) {

Piffero.findAsString(function (result) {
deferred.resolve();
},
fs.createReadStream(JSON_FILE),
'$' + JSON_PATH
);

}
})
.on('complete', function () {
console.log('###########################################');
for (var i = 0; i < this.length; i++) {
console.log(this[i].name + " " + this[i].hz + " ops/sec ("+ this[i].stats.sample.length +" runs sampled)");
}
console.log('Fastest is ' + this.filter('fastest').map('name'));
console.log('Slowest is ' + this.filter('slowest').map('name'));
})
.run()

0 comments on commit f481ea4

Please sign in to comment.