99
1010This arduino library is based on [ libyaml] ( https://github.com/yaml/libyaml ) .
1111
12- It provides several ways to convert YAML<=>JSON using libyaml, cJSON or ArduinoJson objects.
1312
14- Supported platforms (some untested) :
13+ ### Supported platforms:
1514
1615- ESP32
1716- RP2040
1817- ESP8266
1918- SAMD
2019
20+ ### Features:
2121
22+ - YAML➔JSON and JSON➔YAML conversion
23+ - ArduinoJson serializers/deserializers
24+ - cJSON serializers/deserializers
2225
23- ### Usage
26+
27+ ----------------------------
28+
29+
30+ ## Usage
2431
2532``` cpp
2633#include < ArduinoYaml.h>
3441
3542```
3643
44+ ----------------------------
3745
3846
47+ ## Pure libyaml implementation
48+
49+ YAML is a superset of JSON, so native conversion from/to JSON is possible without any additional JSON library.
50+
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 );
3958
40- #### pure libyaml
59+ ```
4160
42- YAML is a superset of JSON, so native conversion from JSON is possible without any additional JSON library.
4361
62+ **Convert YAML to JSON**
4463```cpp
45- #include < ArduinoYaml.h>
4664
47- // JSON stream to YAML stream
48- size_t serializeYml ( Stream &json_src_stream, Stream &yml_dest_stream );
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 );
69+
70+ ```
71+
72+
73+
74+ ** Convert JSON to YAML**
75+ ``` cpp
76+
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 );
81+
82+ ```
83+
84+ ----------------------------
85+
86+ ## Bindings
87+
88+ ArduinoJson and cJSON bindings operate differently depending on the platform.
89+
4990
91+ | | ArduinoJson support | cJSON support |
92+ |-----------------|---------------------|------------------------------|
93+ | ESP32 | detected (*) | implicit (built-in esp-idf) |
94+ | ESP8266 | implicit | implicit (bundled) |
95+ | RP2040 | implicit | implicit (bundled) |
96+ | SAMD | implicit | implicit (bundled) |
97+
98+
99+ (*) On ESP32 platform, the detection depends on `__has_include(<ArduinoJson.h>)` macro.
100+ So all ArduinoJson functions will be disabled unless `#include <ArduinoJson.h>` is found **before** `#include <ArduinoYaml.h>`.
101+
102+ On ESP8266/RP2040/SAMD platforms it is assumed that ArduinoJson is already available as a dependency.
103+
104+
105+ In order to save flash space and/or memory, the defaults bindings can be disabled independently by setting one or all of the
106+ following macros before including ArduinoYaml:
107+
108+ ```cpp
109+ #define YAML_DISABLE_ARDUINOJSON // disable all ArduinoJson functions
110+ #define YAML_DISABLE_CJSON // disable all cJSON functions
50111```
51112
113+ Note to self: this should probably be the other way around e.g. explicitely enabled by user.
114+
115+ Note to readers: should ArduinoJson and/or cJSON be implicitely loaded?
116+ [ Feedback is welcome!] ( https://github.com/tobozo/YAMLDuino/issues )
117+
52118
53- #### ArduinoJson
119+ ----------------------------
120+
121+ ## ArduinoJson bindings
122+
123+ The support is implicitely enabled on most platforms.
54124
55125
56126``` cpp
57- #include <ArduinoJson.h> // include this first or functions will be disabled
58- #include <ArduinoYaml.h>
127+ #include < ArduinoJson.h> // ESP32 plaforms must include this before ArduinoYaml or functions will be disabled
128+ #include < ArduinoYaml.h>
129+ ```
130+
131+ Enabling support will expose the following functions:
59132
133+ ``` cpp
60134 // ArduinoJSON object to YAML string
61135 size_t serializeYml ( JsonVariant src_obj, String &dest_string );
62136 // ArduinoJSON object to YAML stream
@@ -72,17 +146,21 @@ YAML is a superset of JSON, so native conversion from JSON is possible without a
72146
73147```
74148
149+ ----------------------------
75150
151+ ## cJSON bindinds
76152
77- #### cJSON
153+ The support is implicitely enabled on most platforms and will use the bundled cJSON version.
154+ ESP32 will use the built-in version.
78155
79- ⚠️ cJSON has a memory leak with floats, the leak happens only once though, and may be
80- avoided by quoting the float, which won't affect yaml output.
81156
157+ ⚠️ both versions of cJSON have a memory leak with floats, the leak happens only once though, and
158+ may be avoided by quoting the float, which won't affect yaml output.
159+
160+
161+ Enabling support will expose the following functions:
82162
83163```cpp
84- // #include <cJSON.h> // no need to include, cJSON is built-in with esp32 and also bundled with ArduinoYaml
85- #include < ArduinoYaml.h>
86164
87165 // cJSON object to YAML string
88166 size_t serializeYml( cJSON* src_obj, String &dest_string );
@@ -95,19 +173,120 @@ avoided by quoting the float, which won't affect yaml output.
95173
96174```
97175
176+ ----------------------------
177+
178+ ## String/Stream helper
179+
180+ Although ` const char* ` is an acceptable source type for conversion, using ` Stream ` is recommended as it is more memory efficient.
181+
182+ The ` StringStream ` class is provided with this library as a helper.
183+
184+ ``` cpp
98185
186+ String my_json = " {\" blah\" :true}" ;
99187
188+ StringStream json_input_stream (my_json);
189+
190+ String my_output;
191+ StringStream output_stream(my_output);
192+
193+ ```
194+
195+ The `StringStream` bundled class is based on Arduino `String` and can easily be replaced by any class inheriting from `Stream`.
196+
197+ ```cpp
198+ class StringStream : public Stream
199+ {
200+ public:
201+ StringStream(String &s) : str(s), pos(0) {}
202+ virtual ~StringStream() {};
203+ virtual int available() { return str.length() - pos; }
204+ virtual int read() { return pos<str.length() ? str[pos++] : -1; }
205+ virtual int peek() { return pos<str.length() ? str[pos] : -1; }
206+ virtual size_t write(uint8_t c) { str += (char)c; return 1; }
207+ virtual void flush() {}
208+ private:
209+ String &str;
210+ unsigned int pos;
211+ };
212+ ```
213+
214+ See [ ArduinoStreamUtils] ( https://github.com/bblanchon/ArduinoStreamUtils ) for other types of streams (i.e. buffered).
215+
216+
217+ ----------------------------
218+
219+
220+ ## Output decorators
221+
222+
223+ JSON and YAML indentation levels can be customized:
224+
225+
226+ ``` 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 );
229+
230+ ```
231+
232+
233+ Set custom JSON indentation and folding depth:
234+
235+ ```cpp
236+ // two spaces per indentation level, unfold up to 8 nesting levels
237+ YAMLParser::setJSONIndent(" ", 8 ); // lame fact: folds on objects, not on arrays
238+
239+ ```
240+
241+
242+ Set custom YAML indentation (minimum=2, max=16):
243+
244+ ``` cpp
245+ // annoy your friends with 3 spaces indentation, totally valid in YAML
246+ YAML::setYAMLIndent ( 3 );
247+
248+ ```
249+
250+
251+ ----------------------------
252+
253+
254+ ## Debug
255+
256+
257+ The debug level can be changed at runtime:
258+
259+
260+ ```cpp
261+ void YAML::setLogLevel( LogLevel_t level );
262+ ```
263+
264+ Set library debug level:
265+
266+ ``` cpp
267+ //
268+ // Accepted values:
269+ // LogLevelNone : No logging
270+ // LogLevelError : Errors
271+ // LogLevelWarning : Errors+Warnings
272+ // LogLevelInfo : Errors+Warnings+Info
273+ // LogLevelDebug : Errors+Warnings+Info+Debug
274+ // LogLevelVerbose : Errors+Warnings+Info+Debug+Verbose
275+ YAMLParser::setLogLevel ( YAML::LogLevelDebug );
276+ ```
100277
278+ ----------------------------
101279
102- ### Credits and special thanks to:
280+ ## Credits and special thanks to:
103281
104282 - [@yaml](https://github.com/yaml)
105283 - [@DaveGamble](https://github.com/DaveGamble)
106284 - [@bblanchon](https://github.com/bblanchon)
107285 - [@vikman90](https://github.com/vikman90/yaml2json)
108286
109- ### Additional resources:
287+ ## Additional resources:
110288
111289 - ArduinoJson : https://github.com/bblanchon/ArduinoJson
290+ - ArduinoStreamUtils : https://github.com/bblanchon/ArduinoStreamUtils
112291 - cJSON : https://github.com/DaveGamble/cJSON
113292 - libyaml : https://github.com/yaml/libyaml
0 commit comments