Coder Social home page Coder Social logo

bjson's Introduction

ABANDONED 4/22/2016 Object.observe has been removed from future versions of V8. Therefore, this module is no longer being maintained.


Bind Json - "bjson"

Bind Json: Reactive way to read/write json files.

Note:
Bind Json, called as "bjson", don't have ANY RELATIONS with Binary Json, also called as "bjson".
These are two different projects with same abbreviation name. Sorry for this.

How it works

When you need to edit a json file, what you do?

Without bjson With bjson
Create json file if not exists
Read json file. Read json file (Will create if not exists)
Deserialize json file.
Edit parsed object. Edit object (Will reactively save in json file)
Serialize new object.
Write back into file.

Examples

Editing .json file without bjson

var fs = require('fs');
if(!fs.existsSync('settings.json')){
  fs.writeFileSync('settings.json', '{}');
}
var settingsJson = fs.readFileSync('settings.json');
var settings = JSON.parse(settingsJson);
settings.foo = 'bar';
var settingsJson = JSON.stringify(settings, null, 2);
fs.writeFileSync('settings.json', settingsJson);

Editing .json file with bjson

var bjson = require('bjson');
var settings = bjson('settings.json');
settings.foo = 'bar'; 

Getting started

Binding json

settings.json

{}

whatever.js

var bjson = require('bjson');
var settings = bjson('settings'); // will read or create settings.json
settings.prop = 'bar';

settings.json:

{
    "prop": "bar"
}

Watching changes with observe

You can watch changes with a instance of Object.observe passed as callback argument.

settings.json:

{
    "prop": "bar"
}

whatever.js

var bjson = require('bjson');
var settings = bjson('settings', function(observe){
    observe.on('change', function(changes){
        console.log('Path:', changes.path);
        console.log('Old Value:', changes.oldValue);
        console.log('New Value:', changes.value);
        console.log('-----');
    });
});

settings.prop = 'foo';
settings.otherprop = 'bar';

Log output:

Path: prop
Old Value: bar
New Value: foo
-----
Path: otherprop
Old Value: undefined
New Value: bar
-----

settings.json:

{
    "prop": "foo",
    "otherprop": "bar"
}

Observe events

var bjson = require('bjson');
var settings = bjson('settings', function(observe){
    observe.on('add', function(changes){});
    observe.on('update', function(changes){});
    observe.on('delete', function(changes){});
    observe.on('reconfigure', function(changes){});
    observe.on('change', function(changes){}); // fired when any of the above events are emitted
});

Observe events callback changes

path: full path to the property, including nesting
name: name of the path
type: name of the event
object: object
value: current value for the given path. same as object[name]
oldValue: previous value of the property

Example:

var bjson = require('bjson');
var settings = bjson('settings', function(observe){
    observe.on('change', function(changes){
        console.log(changes);
    });
});

settings.foo = 'bar'

//log:
// { path: 'foo',
//   name: 'foo',
//   type: 'add',
//   object: { foo: 'bar' },
//   value: 'bar',
//   oldValue: undefined }

bjson's People

Contributors

alisonmonteiro avatar renatorib avatar vartana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bjson's Issues

How to edit .bjson files

I am trying to edit a .bjson file from minecraft 3ds to see if I can mod it. What should I edit the .bjson with?

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.