49
49
YAML is a superset of JSON, so native conversion from/to JSON is possible without any additional JSON library.
50
50
51
51
``` cpp
52
- // JSON <=> YAML stream to stream conversion (both ways!), accepts valid JSON or YAML as the input
53
- // Available values for output format:
54
- // YAMLParser::OUTPUT_YAML
55
- // YAMLParser::OUTPUT_JSON
56
- // YAMLParser::OUTPUT_JSON_PRETTY
57
- size_t serializeYml ( Stream &source, Stream &destination, OutputFormat_t format );
52
+ // JSON <=> YAML stream to stream conversion (both ways!).
53
+ // Accepts valid JSON or YAML as the input.
54
+ // Available values for output format:
55
+ // YAMLParser::OUTPUT_YAML
56
+ // YAMLParser::OUTPUT_JSON
57
+ // YAMLParser::OUTPUT_JSON_PRETTY
58
+ size_t serializeYml ( Stream &source, Stream &destination, OutputFormat_t format );
58
59
59
60
```
60
61
61
62
62
63
**Convert YAML to JSON**
63
64
```cpp
65
+ String yaml_str = "hello: world\nboolean: true\nfloat: 1.2345";
66
+ StringStream yaml_stream( yaml_str );
64
67
65
- String yaml_str = "hello: world\nboolean: true\nfloat: 1.2345";
66
- StringStream yaml_stream( yaml_str );
67
-
68
- serializeYml( yaml_stream, Serial, YAMLParser::OUTPUT_JSON_PRETTY );
68
+ serializeYml( yaml_stream, Serial, YAMLParser::OUTPUT_JSON_PRETTY );
69
69
70
70
```
71
71
72
72
73
73
74
74
** Convert JSON to YAML**
75
75
``` cpp
76
+ String json_str = " {\" hello\" : \" world\" , \" boolean\" : true, \" float\" :1.2345}" ;
77
+ StringStream json_stream ( json_str );
76
78
77
- String json_str = " {\" hello\" : \" world\" , \" boolean\" : true, \" float\" :1.2345}" ;
78
- StringStream json_stream ( json_str );
79
-
80
- serializeYml( json_stream, Serial, YAMLParser::OUTPUT_YAML );
79
+ serializeYml( json_stream, Serial, YAMLParser::OUTPUT_YAML );
81
80
82
81
```
83
82
@@ -102,7 +101,7 @@ So all ArduinoJson functions will be disabled unless `#include <ArduinoJson.h>`
102
101
On ESP8266/RP2040/SAMD platforms it is assumed that ArduinoJson is already available as a dependency.
103
102
104
103
105
- In order to save flash space and/or memory, the defaults bindings can be disabled independently by setting one or all of the
104
+ In order to save flash space and/or memory, the default bindings can be disabled independently by setting one or all of the
106
105
following macros before including ArduinoYaml:
107
106
108
107
```cpp
@@ -120,56 +119,59 @@ Note to readers: should ArduinoJson and/or cJSON be implicitely loaded?
120
119
121
120
## ArduinoJson bindings
122
121
123
- The support is implicitely enabled on most platforms.
122
+ See the [ motivational post] ( https://github.com/bblanchon/ArduinoJson/issues/1808 ) for this implementation.
123
+
124
+ ArduinoJson support is implicitely enabled on most platforms except for ESP32 where dependencies can be detected.
124
125
125
126
126
127
``` cpp
127
- #include < ArduinoJson.h> // ESP32 plaforms must include this before ArduinoYaml or functions will be disabled
128
- #include < ArduinoYaml.h>
128
+ // ESP32 plaforms must include ArduinoJson before ArduinoYaml or functions will be disabled
129
+ #include < ArduinoJson.h>
130
+ #include < ArduinoYaml.h>
129
131
```
130
132
131
133
Enabling support will expose the following functions:
132
134
133
135
``` cpp
134
- // ArduinoJSON object to YAML string
135
- size_t serializeYml ( JsonVariant src_obj, String &dest_string );
136
- // ArduinoJSON object to YAML stream
137
- size_t serializeYml( JsonVariant src_obj, Stream &dest_stream );
138
- // Deserialize YAML string to ArduinoJSON object
139
- DeserializationError deserializeYml( JsonObject &dest_obj, const char* src_yaml_str );
140
- // Deserialize YAML stream to ArduinoJSON object
141
- DeserializationError deserializeYml( JsonObject &dest_obj, Stream &src_stream );
142
- // Deserialize YAML string to ArduinoJSON document
143
- DeserializationError deserializeYml( JsonDocument &dest_doc, Stream &src_stream );
144
- // Deserialize YAML string to ArduinoJSON document
145
- DeserializationError deserializeYml( JsonDocument &dest_doc, const char * src_yaml_str) ;
136
+ // ArduinoJSON object to YAML string
137
+ size_t serializeYml ( JsonVariant src_obj, String &dest_string );
138
+ // ArduinoJSON object to YAML stream
139
+ size_t serializeYml( JsonVariant src_obj, Stream &dest_stream );
140
+ // Deserialize YAML string to ArduinoJSON object
141
+ DeserializationError deserializeYml( JsonObject &dest_obj, const char* src_yaml_str );
142
+ // Deserialize YAML stream to ArduinoJSON object
143
+ DeserializationError deserializeYml( JsonObject &dest_obj, Stream &src_stream );
144
+ // Deserialize YAML string to ArduinoJSON document
145
+ DeserializationError deserializeYml( JsonDocument &dest_doc, Stream &src_stream );
146
+ // Deserialize YAML string to ArduinoJSON document
147
+ DeserializationError deserializeYml( JsonDocument &dest_doc, const char * src_yaml_str) ;
146
148
147
149
```
148
150
149
151
----------------------------
150
152
151
153
## cJSON bindinds
152
154
153
- The support is implicitely enabled on most platforms and will use the bundled cJSON version.
154
- ESP32 will use the built-in version.
155
+ cJSON support is implicitely enabled on most platforms, and will use the bundled cJSON version unless ESP32 platform is detected .
156
+ ESP32 will use the built-in cJSON version from esp-idf instead of the YAMLDuino bundled version.
155
157
156
158
157
- ⚠️ both versions of cJSON have a memory leak with floats, the leak happens only once though, and
159
+ ⚠️ Both versions of cJSON have a memory leak with floats, the leak happens only once though, and
158
160
may be avoided by quoting the float, which won't affect yaml output.
159
161
160
162
161
163
Enabling support will expose the following functions:
162
164
163
165
```cpp
164
166
165
- // cJSON object to YAML string
166
- size_t serializeYml( cJSON* src_obj, String &dest_string );
167
- // cJSON object to YAML stream
168
- size_t serializeYml( cJSON* src_obj, Stream &dest_stream );
169
- // YAML string to cJSON object
170
- int deserializeYml( cJSON* dest_obj, const char* src_yaml_str );
171
- // YAML stream to cJSON object
172
- int deserializeYml( cJSON* dest_obj, Stream &src_stream );
167
+ // cJSON object to YAML string
168
+ size_t serializeYml( cJSON* src_obj, String &dest_string );
169
+ // cJSON object to YAML stream
170
+ size_t serializeYml( cJSON* src_obj, Stream &dest_stream );
171
+ // YAML string to cJSON object
172
+ int deserializeYml( cJSON* dest_obj, const char* src_yaml_str );
173
+ // YAML stream to cJSON object
174
+ int deserializeYml( cJSON* dest_obj, Stream &src_stream );
173
175
174
176
```
175
177
@@ -183,12 +185,11 @@ The `StringStream` class is provided with this library as a helper.
183
185
184
186
``` cpp
185
187
186
- String my_json = " {\" blah\" :true}" ;
188
+ String my_json = " {\" blah\" :true}" ;
189
+ StringStream json_input_stream (my_json);
187
190
188
- StringStream json_input_stream (my_json);
189
-
190
- String my_output;
191
- StringStream output_stream(my_output);
191
+ String my_output;
192
+ StringStream output_stream(my_output);
192
193
193
194
```
194
195
@@ -224,16 +225,16 @@ JSON and YAML indentation levels can be customized:
224
225
225
226
226
227
``` cpp
227
- void YAML::setYAMLIndent ( int spaces_per_indent=2 ); // min=2, max=16
228
- void YAML::setJSONIndent( const char* spaces_or_tabs=JSON_SCALAR_TAB , int folding_depth=JSON_FOLDING_DEPTH );
228
+ void YAML::setYAMLIndent ( int spaces_per_indent=2 ); // min=2, max=16
229
+ void YAML::setJSONIndent( const char* spaces_or_tabs="\t" , int folding_depth=4 );
229
230
230
231
```
231
232
232
233
233
234
Set custom JSON indentation and folding depth:
234
235
235
236
```cpp
236
- // two spaces per indentation level, unfold up to 8 nesting levels
237
+ // this set two spaces per indentation level, unfolds up to 8 nesting levels
237
238
YAMLParser::setJSONIndent(" ", 8 ); // lame fact: folds on objects, not on arrays
238
239
239
240
```
@@ -258,7 +259,7 @@ The debug level can be changed at runtime:
258
259
259
260
260
261
```cpp
261
- void YAML::setLogLevel( LogLevel_t level );
262
+ void YAML::setLogLevel( LogLevel_t level );
262
263
```
263
264
264
265
Set library debug level:
@@ -275,6 +276,23 @@ Set library debug level:
275
276
YAMLParser::setLogLevel ( YAML::LogLevelDebug );
276
277
```
277
278
279
+ ----------------------------
280
+
281
+
282
+ ## Support the Project
283
+
284
+ There are a few things you can do to support the project:
285
+
286
+ - Star 🌟 this [repository](https://github.com/tobozo/YAMLDuino) and/or [follow me](https://github.com/tobozo/) on GitHub
287
+ - Share and upvote on sites like Twitter, Reddit, and Hacker News
288
+ - [Report](https://github.com/tobozo/YAMLDuino/issues) any bugs, glitches, or errors that you find
289
+
290
+
291
+ These things motivate me to to keep sharing what I build, and they provide
292
+ validation that my work is appreciated! They also help me improve the
293
+ project. Thanks in advance!
294
+
295
+
278
296
----------------------------
279
297
280
298
## Credits and special thanks to:
@@ -284,6 +302,8 @@ YAMLParser::setLogLevel( YAML::LogLevelDebug );
284
302
- [@bblanchon](https://github.com/bblanchon)
285
303
- [@vikman90](https://github.com/vikman90/yaml2json)
286
304
305
+
306
+
287
307
## Additional resources:
288
308
289
309
- ArduinoJson : https://github.com/bblanchon/ArduinoJson
0 commit comments