time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3210B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # Read JSON
     22 
     23 > Read a file as [JSON][json].
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var readJSON = require( '@stdlib/fs/read-json' );
     31 ```
     32 
     33 <a name="read-json"></a>
     34 
     35 #### readJSON( file\[, options], clbk )
     36 
     37 Asynchronously reads a file as [JSON][json].
     38 
     39 ```javascript
     40 var join = require( 'path' ).join;
     41 
     42 readJSON( join( __dirname, 'package.json' ), onJSON );
     43 
     44 function onJSON( error, data ) {
     45     if ( error ) {
     46         throw error;
     47     }
     48     console.dir( data );
     49 }
     50 ```
     51 
     52 The function accepts the following `options`:
     53 
     54 -   **encoding**: file encoding.
     55 -   **flag**: file status flag.
     56 -   **reviver**: [JSON][json] transformation `function`.
     57 
     58 The `options` parameter may also be a `string` specifying the file `encoding`.
     59 
     60 ```javascript
     61 var join = require( 'path' ).join;
     62 
     63 readJSON( join( __dirname, 'package.json' ), 'utf8', onJSON );
     64 
     65 function onJSON( error, data ) {
     66     if ( error ) {
     67         throw error;
     68     }
     69     console.dir( data );
     70 }
     71 ```
     72 
     73 #### readJSON.sync( file\[, options] )
     74 
     75 Synchronously reads a file as [JSON][json].
     76 
     77 ```javascript
     78 var join = require( 'path' ).join;
     79 var instanceOf = require( '@stdlib/assert/instance-of' );
     80 
     81 var out = readJSON.sync( join( __dirname, 'package.json' ) );
     82 if ( instanceOf( out, Error ) ) {
     83     throw out;
     84 }
     85 console.dir( out );
     86 ```
     87 
     88 The function accepts the same `options` as [`readJSON()`](#read-json) above.
     89 
     90 </section>
     91 
     92 <!-- /.usage -->
     93 
     94 <section class="notes">
     95 
     96 ## Notes
     97 
     98 -   If the `encoding` option is set to `utf8` and the file has a UTF-8 [byte order mark][bom] (BOM), the byte order mark is **removed** before attempting to parse as [JSON][json].
     99 
    100 </section>
    101 
    102 <!-- /.notes -->
    103 
    104 <section class="examples">
    105 
    106 ## Examples
    107 
    108 <!-- eslint no-undef: "error" -->
    109 
    110 ```javascript
    111 var join = require( 'path' ).join;
    112 var readJSON = require( '@stdlib/fs/read-json' );
    113 
    114 var file = join( __dirname, 'package.json' );
    115 
    116 // Synchronously read file contents...
    117 var data = readJSON.sync( file, 'utf8' );
    118 // returns <Object>
    119 
    120 data = readJSON.sync( 'beepboop', {
    121     'encoding': 'utf8'
    122 });
    123 // returns <Error>
    124 
    125 // Asynchronously read file contents...
    126 readJSON( file, onJSON );
    127 readJSON( 'beepboop', onJSON );
    128 
    129 function onJSON( error, data ) {
    130     if ( error ) {
    131         if ( error.code === 'ENOENT' ) {
    132             console.error( 'JSON file does not exist.' );
    133         } else {
    134             throw error;
    135         }
    136     } else {
    137         console.log( 'Package description: %s', data.description );
    138     }
    139 }
    140 ```
    141 
    142 </section>
    143 
    144 <!-- /.examples -->
    145 
    146 <section class="links">
    147 
    148 [json]: http://www.json.org/
    149 
    150 [bom]: https://en.wikipedia.org/wiki/Byte_order_mark
    151 
    152 </section>
    153 
    154 <!-- /.links -->