Code Monkey home page Code Monkey logo

node-cjson's Introduction

CJSON (Commented Javascript Object Notation) is a comments enabled json config loader.

JSON has a good spec, is implemented in every language, has easy to read syntax and is much more powerfull then ini files.

JSON is perfect for writing config files, except of one problem - there is no comments, but sometimes config files get large and need to be commented.

Well, you could just evaluate json file as a javascript using one-liner, right?

The purpose of this module is to avoid dirty javascript configs and to enable clean, consistent, secure, portable and JSON valid notation.

CJSON supports javascript style comments: singleline "//" and multiline "/**/". It takes care about comments inside of strings.

Example of such shiny config file:

/*
 * This is my app configuration file.
 * 
 */
{
	"host": "localhost",
	// app is listening on this port
	"port": 8888
}

API

load the module

var cjson = require('cjson');

cjson.load(path, [options]);

Load config file from given path, array of paths or directory. Second parameter is optional and can be a boolean or object.

  • path
    • {string} absolute path to the file
    • {string} absolute path of the folder containing the files
    • {array} list of files
  • path {string} absolute path to the file
  • options {boolean|Object} optional options

options defaults: { merge: false, replace: null extension: '.json' }

If you pass true as second param, its the same like {merge: true} and will merge all configs together. replace allows you to do some string replacements, see cjson.replace.

if the extension if set, only include the files with this extension. {"extension":""} to get all the files in the folder

Examples:

// just one config 
var conf = cjson.load('/path/to/your/config.json');

// array of configs 
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json']);

//output
{
	config1: {key1: 'value1'}
	config2: {key2: 'value2'}
}


// use optional merge parameter
// array of configs 
var conf = cjson.load(['/path/to/your/config1.json', '/path/to/your/config2.json'], true);

// output
{
	key1: 'value1',
	key2: 'value2'
}


// load all config files from a directory
var conf = cjson.load('/path/to/your/configs');

// overwriting dev config with production
var paths = ['/path/to/conf.json'];
if (process.env.NODE_ENV ==='production') {
	paths.push('/path/to/conf-prod.json');
}
var conf = cjson.load(paths, true);

cjson.extend([deep], target, object1, [objectN])

Merge the contents of two or more objects together into the first object.

  • deep If true, the merge becomes recursive.
  • target The object to extend. It will receive the new properties.
  • object1 An object containing additional properties to merge in.
  • objectN Additional objects containing properties to merge in.

Example:

var object = $.extend({}, object1, object2);

cjson.decomment(str)

Remove javascript style comments, singleline - '//' and multiline - '/**/'. It takes care about comments inside of strings and escaping.

cjson.parse(str, [reviver])

Like JSON.parse, but it takes care about comments. Optional reviver argument is for JSON.parse method and will be called for every key and value at every level of the final result

cjson.replace(str, obj)

Replace all strings {{key}} contained in {key: 'value'}, where key can be any property of passed obj.

Example: var str = '{"path": "{{root}}/src"}'; // json file contents cjson.replace(str, {root: '/usr'}); // '{"path": "/usr/src"}'

Installation

npm install cjson

node-cjson's People

Contributors

kof avatar papandreou avatar

Stargazers

xavier dutoit avatar

Watchers

xavier dutoit avatar James Cloos avatar  avatar

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.