4949YAML is a superset of JSON, so native conversion from/to JSON is possible without any additional JSON library.
5050
5151``` 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 );
5859
5960```
6061
6162
6263**Convert YAML to JSON**
6364```cpp
65+ String yaml_str = "hello: world\nboolean: true\nfloat: 1.2345";
66+ StringStream yaml_stream( yaml_str );
6467
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 );
6969
7070```
7171
7272
7373
7474** Convert JSON to YAML**
7575``` cpp
76+ String json_str = " {\" hello\" : \" world\" , \" boolean\" : true, \" float\" :1.2345}" ;
77+ StringStream json_stream ( json_str );
7678
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 );
8180
8281```
8382
@@ -102,7 +101,7 @@ So all ArduinoJson functions will be disabled unless `#include <ArduinoJson.h>`
102101On ESP8266/RP2040/SAMD platforms it is assumed that ArduinoJson is already available as a dependency.
103102
104103
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
106105following macros before including ArduinoYaml:
107106
108107```cpp
@@ -120,56 +119,59 @@ Note to readers: should ArduinoJson and/or cJSON be implicitely loaded?
120119
121120## ArduinoJson bindings
122121
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.
124125
125126
126127``` 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>
129131```
130132
131133Enabling support will expose the following functions:
132134
133135``` 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) ;
146148
147149```
148150
149151----------------------------
150152
151153## cJSON bindinds
152154
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.
155157
156158
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
158160may be avoided by quoting the float, which won't affect yaml output.
159161
160162
161163Enabling support will expose the following functions:
162164
163165```cpp
164166
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 );
173175
174176```
175177
@@ -183,12 +185,11 @@ The `StringStream` class is provided with this library as a helper.
183185
184186``` cpp
185187
186- String my_json = " {\" blah\" :true}" ;
188+ String my_json = " {\" blah\" :true}" ;
189+ StringStream json_input_stream (my_json);
187190
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);
192193
193194```
194195
@@ -224,16 +225,16 @@ JSON and YAML indentation levels can be customized:
224225
225226
226227``` 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 );
229230
230231```
231232
232233
233234Set custom JSON indentation and folding depth:
234235
235236```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
237238YAMLParser::setJSONIndent(" ", 8 ); // lame fact: folds on objects, not on arrays
238239
239240```
@@ -258,7 +259,7 @@ The debug level can be changed at runtime:
258259
259260
260261```cpp
261- void YAML::setLogLevel( LogLevel_t level );
262+ void YAML::setLogLevel( LogLevel_t level );
262263```
263264
264265Set library debug level:
@@ -275,6 +276,23 @@ Set library debug level:
275276YAMLParser::setLogLevel ( YAML::LogLevelDebug );
276277```
277278
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+
278296----------------------------
279297
280298## Credits and special thanks to:
@@ -284,6 +302,8 @@ YAMLParser::setLogLevel( YAML::LogLevelDebug );
284302 - [@bblanchon](https://github.com/bblanchon)
285303 - [@vikman90](https://github.com/vikman90/yaml2json)
286304
305+
306+
287307## Additional resources:
288308
289309 - ArduinoJson : https://github.com/bblanchon/ArduinoJson
0 commit comments