bchr02 / filejson Goto Github PK
View Code? Open in Web Editor NEWSeamlessly syncs a JavaScript value to a JSON file in the background whenever that value changes.
License: MIT License
Seamlessly syncs a JavaScript value to a JSON file in the background whenever that value changes.
License: MIT License
test case:
var Filejson = require("../filejson/app.js");
var file1 = new Filejson();
file1.load("file1.json", proceed); // file1.json contains {"abc": "123"}
function proceed(error, file) {
if(error) {
console.error(error);
return;
}
console.log(file.contents); // outputs {"abc": "123"}
delete file.contents["msg"];
console.log(file.contents); // outputs {}, however file1.json still contains {"abc": "123"}
}
per the JSON spec, null values should be stored as the literal name null
Hello,
Since I noticed you have posted in other similar modules I thought I would CC you on this announcement.
filejson is a new module that allows you to store a value simply by doing file.contents = value
and have that and any future changes automatically saved to a JSON file on your drive. This will allow the variable to persist across app restarts. The important difference between this module and others is that you don't have to call a function every time you want to alter your objects value. So, instead of having to wrap every update in a function/callback and creating a pyramid (aka callback hell):
var jsonfile = require('jsonfile');
var file = '/tmp/data.json';
var obj = {};
jsonfile.writeFile(file, obj, function (err) {
if(err) {
console.error(err);
return;
}
obj.msg = "Hello World!";
jsonfile.writeFile(file, obj, function (err) {
if(err) {
console.error(err);
return;
}
obj.user = {"name": "Bob"};
jsonfile.writeFile(file, obj, function (err) {
if(err) {
console.error(err);
return;
}
});
});
});
With jsonfile you only need to wrap once and that's it:
var Filejson = require("filejson");
var f = new Filejson();
f.load("file.json", function(error, file) {
if(error) {
console.error(error);
return;
}
file.contents = {};
file.contents.msg = "Hello World!";
file.contents.user = {"name": "Bill"};
});
cc: @pinn3, @tankerkiller125, @hobberwickey, @YounessBenmouloud, @Lazarus118, @Sahas, @ajsingh007, @dudleyjosh, @SCBuergel, @kamikazechaser, @Pizylon, @Egoscio, @maxime1992, @bajtos, @piranna, @sampathsris
To do:
Things to consider:
_Pull requests are welcomed! :-) and if you have any additional ideas please post here. Thank you._
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.