Code Monkey home page Code Monkey logo

jsonsettings's Introduction

jsonsettings

An extremely simple node.js module to read/write settings from a JSON file

Usage

var jsonsettings = require("jsonsettings");
jsonsettings.default_settings_dir = "my_fav_settings";  // optional
var mysettings = new jsonsettings.SettingsFile({ ... options ... });

mysettings.data  // the JSON data

// When mysettings.data is changed, call mysettings.update() to write the changes back to the file
// (unless watchers are enabled - see below)

options:

  • filename: the name of the settings file (if not specified, you must manually call load(filename))
  • settings_dir: the directory in which filename exists (default: default_settings_dir)
  • use_watchers: implement watchers on the JSON object to detect changes and automatically update the JSON file (default: false)
  • onload: function () called after data is loaded
  • onerror: function (err) called on an error (instead of just throwing the error)
  • onupdate: function () called after data file is updated without error

Watchers

If use_watchers is set to true, changes to settings values will automatically be written back to the JSON file.

Some notes on watchers:

  • As soon as any property is modified, the JSON file will be automatically updated IFF the property already had a value.
  • If the property did not already have a value (or if you are unsure), you must call parent.setProp(name, value). Example: Instead of mysettings.data.bob = 2 use mysettings.data.setProp("bob", 2) or mysettings.data.setProp("bob"); mysettings.data.bob = 2;
  • To get the "raw" value of any property, call parent.getProp(name). Example: Instead of mysettings.data.foo.bar use mysettings.data.foo.getProp("bar")
  • If you do NOT use watchers, you must call object.update() after each modification (where object is a SettingsFile object)

Examples

var jsonsettings = require("jsonsettings");

// Load settings from "mysettings/stuff.json" and log them to console when loaded
var mysettings = new jsonsettings.SettingsFile({
    filename: "stuff.json",
    settings_dir: "mysettings",
    onload: function () {
        console.log(mysettings.data);
    }
});

// Make some changes to the settings we loaded and write the changes back to the file
mysettings.data.newstuff = {hello: "world"};
mysettings.data.oldstuff.favorite = 3;
mysettings.update();

Another example:

var jsonsettings = require("jsonsettings");
jsonsettings.default_settings_dir = "/serversettings";

var server_config = new jsonsettings.SettingsFile({
    use_watchers: true,
    onerror: function (err) {
        console.log(err);
    }
});

// Later on...
server_config.load("mystuff.json");
server_config.data.bob = "jim";
// As long as "bob" had a previous value, the changes have automatically been written back to the file.
// To make sure, we could have done this instead:
server_config.data.setProp("bob", "jim");
// or this:
server_config.data.setProp("bob");
server_config.data.bob = "jim";

License

jsonsettings is licensed under the MIT license

jsonsettings's People

Contributors

jhartz avatar

Stargazers

sha avatar

Watchers

 avatar James Cloos avatar sha 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.