scratch – Blame information for rev 127
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
75 | office | 1 | yaml.js |
2 | ======= |
||
3 | |||
4 | ![Build status](https://travis-ci.org/jeremyfa/yaml.js.svg?branch=develop) |
||
5 | |||
6 | Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools. |
||
7 | |||
8 | Mainly inspired from [Symfony Yaml Component](https://github.com/symfony/Yaml). |
||
9 | |||
10 | How to use |
||
11 | ---------- |
||
12 | |||
13 | Import yaml.js in your html page: |
||
14 | |||
15 | ``` html |
||
16 | <script type="text/javascript" src="yaml.js"></script> |
||
17 | ``` |
||
18 | |||
19 | Parse yaml string: |
||
20 | |||
21 | ``` js |
||
22 | nativeObject = YAML.parse(yamlString); |
||
23 | ``` |
||
24 | |||
25 | Dump native object into yaml string: |
||
26 | |||
27 | ``` js |
||
28 | yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]); |
||
29 | ``` |
||
30 | |||
31 | Load yaml file: |
||
32 | |||
33 | ``` js |
||
34 | nativeObject = YAML.load('file.yml'); |
||
35 | ``` |
||
36 | |||
37 | Load yaml file: |
||
38 | |||
39 | ``` js |
||
40 | YAML.load('file.yml', function(result) |
||
41 | { |
||
42 | nativeObject = result; |
||
43 | }); |
||
44 | ``` |
||
45 | |||
46 | Use with node.js |
||
47 | ---------------- |
||
48 | |||
49 | Install module: |
||
50 | |||
51 | ``` bash |
||
52 | npm install yamljs |
||
53 | ``` |
||
54 | |||
55 | Use it: |
||
56 | |||
57 | ``` js |
||
58 | YAML = require('yamljs'); |
||
59 | |||
60 | // parse YAML string |
||
61 | nativeObject = YAML.parse(yamlString); |
||
62 | |||
63 | // Generate YAML |
||
64 | yamlString = YAML.stringify(nativeObject, 4); |
||
65 | |||
66 | // Load yaml file using YAML.load |
||
67 | nativeObject = YAML.load('myfile.yml'); |
||
68 | ``` |
||
69 | |||
70 | Command line tools |
||
71 | ------------------ |
||
72 | |||
73 | You can enable the command line tools by installing yamljs as a global module: |
||
74 | |||
75 | ``` bash |
||
76 | npm install -g yamljs |
||
77 | ``` |
||
78 | |||
79 | Then, two cli commands should become available: **yaml2json** and **json2yaml**. They let you convert YAML to JSON and JSON to YAML very easily. |
||
80 | |||
81 | **yaml2json** |
||
82 | |||
83 | ``` |
||
84 | usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input |
||
85 | |||
86 | Positional arguments: |
||
87 | input YAML file or directory containing YAML files. |
||
88 | |||
89 | Optional arguments: |
||
90 | -h, --help Show this help message and exit. |
||
91 | -v, --version Show program's version number and exit. |
||
92 | -p, --pretty Output pretty (indented) JSON. |
||
93 | -i INDENTATION, --indentation INDENTATION |
||
94 | Number of space characters used to indent code (use |
||
95 | with --pretty, default: 2). |
||
96 | -s, --save Save output inside JSON file(s) with the same name. |
||
97 | -r, --recursive If the input is a directory, also find YAML files in |
||
98 | sub-directories recursively. |
||
99 | -w, --watch Watch for changes. |
||
100 | ``` |
||
101 | |||
102 | **json2yaml** |
||
103 | |||
104 | ``` |
||
105 | usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input |
||
106 | |||
107 | Positional arguments: |
||
108 | input JSON file or directory containing JSON files. |
||
109 | |||
110 | Optional arguments: |
||
111 | -h, --help Show this help message and exit. |
||
112 | -v, --version Show program's version number and exit. |
||
113 | -d DEPTH, --depth DEPTH |
||
114 | Set minimum level of depth before generating inline |
||
115 | YAML (default: 2). |
||
116 | -i INDENTATION, --indentation INDENTATION |
||
117 | Number of space characters used to indent code |
||
118 | (default: 2). |
||
119 | -s, --save Save output inside YML file(s) with the same name. |
||
120 | -r, --recursive If the input is a directory, also find JSON files in |
||
121 | sub-directories recursively. |
||
122 | -w, --watch Watch for changes. |
||
123 | ``` |
||
124 | |||
125 | **examples** |
||
126 | |||
127 | ``` bash |
||
128 | # Convert YAML to JSON and output resulting JSON on the console |
||
129 | yaml2json myfile.yml |
||
130 | |||
131 | # Store output inside a JSON file |
||
125 | office | 132 | yaml2json myfile.yml > output.json |
75 | office | 133 | |
134 | # Output "pretty" (indented) JSON |
||
135 | yaml2json myfile.yml --pretty |
||
136 | |||
137 | # Save the output inside a file called myfile.json |
||
138 | yaml2json myfile.yml --pretty --save |
||
139 | |||
140 | # Watch a full directory and convert any YAML file into its JSON equivalent |
||
141 | yaml2json mydirectory --pretty --save --recursive |
||
142 | |||
143 | # Convert JSON to YAML and store output inside a JSON file |
||
125 | office | 144 | json2yaml myfile.json > output.yml |
75 | office | 145 | |
146 | # Output YAML that will be inlined only after 8 levels of indentation |
||
147 | json2yaml myfile.json --depth 8 |
||
148 | |||
149 | # Save the output inside a file called myfile.json with 4 spaces for each indentation |
||
150 | json2yaml myfile.json --indentation 4 |
||
151 | |||
152 | # Watch a full directory and convert any JSON file into its YAML equivalent |
||
153 | json2yaml mydirectory --pretty --save --recursive |
||
154 |