UNPKG

2.79 kBMarkdownView Raw
1# JSON Future
2
3<h1 align="center">
4 <img src="https://i.imgur.com/WSDllwa.png" alt="json-future">
5 <br>
6 <br>
7</h1>
8
9![Last version](https://img.shields.io/github/tag/Kikobeats/json-future.svg?style=flat-square)
10[![Build Status](http://img.shields.io/travis/Kikobeats/json-future/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/json-future)
11[![NPM Status](http://img.shields.io/npm/dm/json-future.svg?style=flat-square)](https://www.npmjs.org/package/json-future)
12
13> Modern JSON interface. [propositions for ECMAScript 7](https://github.com/mohsen1/async-json).
14
15## Why
16
17* High level methods for manipulate JSON files.
18* Backward compatibility with JSON Object in Node/Browser.
19* Async support (callback/promise) based on [ECMAScript proposal](https://github.com/mohsen1/async-json).
20
21JSON Future is based into a set of cool libraries to handle JSON, but some of this libraries uses promises or callback style. This library adds an extra layer to call these libraries uniformly.
22
23## Install
24
25```bash
26npm install json-future --save
27```
28
29## Usage
30
31```js
32const jsonFuture = require('json-future')
33```
34
35Don't be afraid to replace for the default `JSON` object. The library is specially designed for be compatible and don't break your code:
36
37```js
38JSON = require('json-future')
39```
40
41Also you can do this action using the `register` helper:
42
43```js
44require('json-future/register')
45```
46
47## API
48
49In `async` methods, if you don't provide a callback for node style, then the method return a `Promise`.
50
51### .stringify(input, [replacer], [space])
52### .stringifyAsync(input, [replacer], [space], [cb])
53
54Creates the `string` version of the input.
55
56### .parse(input, [reviver], [filename])
57### .parseAsync(input, [reviver], [filename], [cb])
58
59Creates the `object` version of the input.
60
61### .load(filepath)
62### .loadAsync(filepath, [cb])
63
64Returns the parsed JSON.
65
66### .save(filepath, data, [options])
67### .saveAsync(filepath, data, [options], [cb])
68
69Stringify and write JSON to a file atomically.
70
71#### options
72
73##### indent
74
75Type: `string`, `number`
76Default: `\t`
77
78Indentation as a string or number of spaces.
79Pass in `null` for no formatting.
80
81##### sortKeys
82
83Type: `boolean`, `function`
84Default: `false`
85
86Sort the keys recursively.
87Optionally pass in a [`compare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function.
88
89##### replacer
90
91Type: `function`
92
93Passed into [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter).
94
95##### mode
96
97Type: `number`
98Default `438` *(0666 in octal)*
99
100[Mode](https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation) used when writing the file.
101
102## License
103
104MIT © [Kiko Beats](http://kikobeats.com)