GT2/GT2-iOS/node_modules/@expo/json-file
Ren Ren Juan 30eef7d036 correct tree err 2018-02-12 17:26:06 +00:00
..
__tests__ correct tree err 2018-02-12 17:26:06 +00:00
.eslintrc.js correct tree err 2018-02-12 17:26:06 +00:00
.npmignore correct tree err 2018-02-12 17:26:06 +00:00
JsonFile.js correct tree err 2018-02-12 17:26:06 +00:00
JsonFileError.js correct tree err 2018-02-12 17:26:06 +00:00
LICENSE correct tree err 2018-02-12 17:26:06 +00:00
README.md correct tree err 2018-02-12 17:26:06 +00:00
circle.yml correct tree err 2018-02-12 17:26:06 +00:00
package.json correct tree err 2018-02-12 17:26:06 +00:00
test-json5.json correct tree err 2018-02-12 17:26:06 +00:00
test.json correct tree err 2018-02-12 17:26:06 +00:00
yarn.lock correct tree err 2018-02-12 17:26:06 +00:00

README.md

json-file

A module for reading, writing, and manipulating JSON files

Importing the package

import JsonFile from '@exponent/json-file';

Promise-based async API

Everything returns Promises. If you are using ES7 (or Babel), you can write code like this:

let config = await JsonFile.readAsync('config.json', {cantReadFileDefault: {}});

If you are using ES6, you can just use the return values the way you normally would use Promises.

JsonFile.readAsync('config.json', {cantReadFileDefault: {}}).then(config => {
   ...
});

Used as an object


var file = new JsonFile('config.json', {cantReadFileDefault: {}});
var somethingSaved = await file.getAsync('somethingSaved', null);

Used as functions


var pkg = await JsonFile.readAsync('package.json');
var main = await JsonFile.getAsync('package.json', 'main', 'index.js');
...

Options you can set, and their default values

Option Description Default Value
space How many spaces to use when pretty-printing, (0 for no pretty-printing) 2
default Catch-all default value for missing values, bad JSON, and files that can't be read undefined
jsonParseErrorDefault The default value for when a file is read but it doesn't contain valid JSON undefined
cantReadFileDefault The default value for when a file can't be read undefined
  • Note that if defaults are undefined, then an Error will be thrown instead of undefined being returned

Methods

.readAsync([options])

Returns the parse of the whole file as an object

.getAsync(key, [default-value], [options])

Returns a single value from a JSON file, using lodash's _.get to query the whole object.

See https://lodash.com/docs#get

.writeAsync(data, [options])

Writes out the given data to the file

.setAsync(key, val, [options])

Updates the file, inserting or updating the value for <key> with <val>

.mergeAsync(sources, [options])

Merges the values in <sources> into the object currently encoded in the file.

.deleteKeyAsync(key, [options])

Deletes a single key from the top level of the file.

Functions

The functions available all mirror the methods above but take file (filename as a string) as their first argument.