GithubHelp home page GithubHelp logo

jsonload's Introduction

jsonload

read in a json file, handles line-delimited

usage

var jsonload = require('jsonload');

// async
jsonload('./foo', function(err, results) {
    console.log(results);
});

// sync
var foo = jsonload.sync('./foo');

errors

Because jsonload handles simple and line-delimited JSON files, there are a three possible errors that may be returned/thrown:

  • file not found
  • an invalid JSON file
  • an invalid JSON line(s)
// async
jsonload('./foo', function(err, results) {
    if (err.code == 'ENOENT')
        console.log('file not found');
    if (err.name == 'SyntaxError')
        console.log('invalid JSON file');
    if (err.length)
        console.log('invalid JSON lines', e);

    // on complex files, there may be lines which parsed correctly
    console.log(results);
});

// sync
try {
    var foo = jsonload.sync('./foo');
} catch (err) {
    if (err.code == 'ENOENT')
        console.log('file not found');
    if (err.name == 'SyntaxError')
        console.log('invalid JSON file');
    if (err.length)
        console.log('invalid JSON lines', e);
}

parser

By default, jsonload utilizes the built-in JSON parser. You can optionally specify a different parser. It must be an object with a .parse() function:

var EJSON = require('mongodb-extended-json');
var sync = jsonload.sync('./ejson-file', EJSON);

jsonload('./ejson-file', EJSON, function(err, ejson) {
    console.log(ejson[0]);
});

The parser is invoked line-by-line. To log each line before parsing (tap-style):

jsonload('./json-file', { parse: function(obj) {
    console.log(obj);
    return JSON.parse(obj);
} }, function() {});

jsonload's People

Contributors

weisjohn avatar

Stargazers

Daniel Waardal avatar

Watchers

 avatar James Cloos avatar

jsonload's Issues

optional parser argument

Currently, we utilize JSON.parse(), but if we could pass in a different parser, projects like mongoose-simple-fixtures could utilize it.

custom parser is not honored when using require() style

When invoked with a custom parser, we dishonor that request by using require(). Instead, we should attempt to read the file, then use the custom parser.

affected areas:

jsonload/index.js

Lines 58 to 60 in e71603b

// simple style
var err;
try { return cb(null, require(path)); } catch (e) { err = e; }
and

jsonload/index.js

Lines 74 to 76 in e71603b

// simple
var err;
try { return require(path); } catch (e) { err = e; }

exposed by weisjohn/mongoose-prime#4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.