DonutEspresso / big-json

A stream based implementation of JSON.parse and JSON.stringify for big POJOs
MIT License
114 stars 9 forks source link

Offer an API to parse from a file #16

Closed JasinYip closed 4 years ago

JasinYip commented 4 years ago

Could we offer an API which could let the users could parse a file without doing the stream jobs.

For example, users could use the API like this:

const json = require('big-json')
const object = await json.parse('/path/to/file.json');

To implement this, we could wrap fs.createReadStream and json.createParseStream() like this:

module.exports.parse = function parse (filePath) {
  return new Promise((resolve, reject) => {
    const parseStream = json.createParseStream()
    parseStream.on('data', resolve)
    parseStream.on('error', reject)
    fs.createReadStream(filePath).pipe(parseStream)
  })
}

You might notice that I thought we could reuse the existed API parse for this new feature.

Could I send a pull request for this?

DonutEspresso commented 4 years ago

Are you basically looking for a promise based interface for the parse and stringify methods?

JasinYip commented 4 years ago

You are right, maybe I should use the callback interface with utils.promisify👍