JSON Handler - Simple Wrapper for Handling .json Files
example
const Handler = require('json-handler'); let handler = new Handler(`path-to-json-file`); console.log(dealer.toString()); dealer.append('foo', 'Yeah'); //foo now shall be 'Yeah'. dealer.set('foo', 'Nooo'); //foo now shall be 'Nooo'. dealer.remove('foo'); //foo now shall be undefined. console.log(dealer.toString());
What's this
A wrapper to hadle json files. actually just some syntactic sugar. But I love it.
API
- constructor/ new
To create an instance of the Handler. Receive the path to the json file. Will throw exceptions because no catch in the main logic. Note: if an empty file is passed, the constructor will create a null object{}
and save to the file for later actions. - toString()
Get the stringfied value of the JSON object. - append(key, value)
Create a key/value pair and append to the object, then save automatically. If the key exists, action aborts. - get(key, value)
Get the value from the provided key. - set(key, value)
Set the value to the key. Will overwrite existing value. Save automatically. - remove(key)
Remove the given key from the object. - save()
Internal function. Don't need to call it, usually.