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 |
|
21 | JSON 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
|
26 | npm install json-future --save
|
27 | ```
|
28 |
|
29 | ## Usage
|
30 |
|
31 | ```js
|
32 | const jsonFuture = require('json-future')
|
33 | ```
|
34 |
|
35 | Don'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
|
38 | JSON = require('json-future')
|
39 | ```
|
40 |
|
41 | Also you can do this action using the `register` helper:
|
42 |
|
43 | ```js
|
44 | require('json-future/register')
|
45 | ```
|
46 |
|
47 | ## API
|
48 |
|
49 | In `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 |
|
54 | Creates the `string` version of the input.
|
55 |
|
56 | ### .parse(input, [reviver], [filename])
|
57 | ### .parseAsync(input, [reviver], [filename], [cb])
|
58 |
|
59 | Creates the `object` version of the input.
|
60 |
|
61 | ### .load(filepath)
|
62 | ### .loadAsync(filepath, [cb])
|
63 |
|
64 | Returns the parsed JSON.
|
65 |
|
66 | ### .save(filepath, data, [options])
|
67 | ### .saveAsync(filepath, data, [options], [cb])
|
68 |
|
69 | Stringify and write JSON to a file atomically.
|
70 |
|
71 | #### options
|
72 |
|
73 | ##### indent
|
74 |
|
75 | Type: `string`, `number`
|
76 | Default: `\t`
|
77 |
|
78 | Indentation as a string or number of spaces.
|
79 | Pass in `null` for no formatting.
|
80 |
|
81 | ##### sortKeys
|
82 |
|
83 | Type: `boolean`, `function`
|
84 | Default: `false`
|
85 |
|
86 | Sort the keys recursively.
|
87 | Optionally pass in a [`compare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function.
|
88 |
|
89 | ##### replacer
|
90 |
|
91 | Type: `function`
|
92 |
|
93 | Passed into [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter).
|
94 |
|
95 | ##### mode
|
96 |
|
97 | Type: `number`
|
98 | Default `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 |
|
104 | MIT © [Kiko Beats](http://kikobeats.com)
|