@@ -16,13 +16,18 @@ This arduino library is based on [libyaml](https://github.com/yaml/libyaml).
16
16
- RP2040
17
17
- ESP8266
18
18
- SAMD
19
+ - TeensyDuino
19
20
20
21
### Features:
21
22
22
23
- YAML➔JSON and JSON➔YAML conversion
24
+ - Accepts * valid* JSON or YAML as the input.
25
+ - Standalone serializers/deserializers
23
26
- ArduinoJson serializers/deserializers
24
27
- cJSON serializers/deserializers
25
-
28
+ - Node accessors
29
+ - l10n style gettext()
30
+ - i18n loader
26
31
27
32
----------------------------
28
33
@@ -31,14 +36,12 @@ This arduino library is based on [libyaml](https://github.com/yaml/libyaml).
31
36
32
37
``` cpp
33
38
#include < ArduinoYaml.h>
34
-
35
39
```
36
40
37
41
or
38
42
39
43
``` cpp
40
44
#include < YAMLDuino.h>
41
-
42
45
```
43
46
44
47
----------------------------
49
52
YAML is a superset of JSON, so native conversion from/to JSON is possible without any additional JSON library.
50
53
51
54
``` cpp
52
- // JSON <=> YAML stream to stream conversion (both ways!).
53
- // Accepts valid JSON or YAML as the input.
54
55
// 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 );
56
+ // OUTPUT_YAML (default)
57
+ // OUTPUT_JSON
58
+ // OUTPUT_JSON_PRETTY
59
+ // JSON/YAML document to YAML/JSON string
60
+ size_t serializeYml ( yaml_document_t* src_doc, String &dest_string, OutputFormat_t format=OUTPUT_YAML );
61
+ // JSON/YAML object to YAML/JSON stream
62
+ size_t serializeYml( yaml_document_t* src_doc, Stream &dest_stream, OutputFormat_t format=OUTPUT_YAML );
63
+
64
+ // YAML stream to YAML document
65
+ int deserializeYml( YAMLNode& dest_obj, const char* src_yaml_str );
66
+ // YAML string to YAML document
67
+ int deserializeYml( YAMLNode& dest_obj, Stream &src_stream );
59
68
60
69
```
61
70
62
71
63
72
**Convert YAML to JSON**
64
73
```cpp
65
74
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
-
75
+ YAMLNode yamlnode = YAMLNode::loadString( yaml_str );
76
+ serializeYml( yamlnode.getDocument(), Serial, OUTPUT_JSON_PRETTY ); // pretty JSON
77
+ // serializeYml( yamlnode.getDocument(), Serial, OUTPUT_JSON ); // ugly JSON
70
78
```
71
79
72
80
73
81
74
82
** Convert JSON to YAML**
75
83
``` cpp
76
84
String json_str = " {\" hello\" : \" world\" , \" boolean\" : true, \" float\" :1.2345}" ;
77
- StringStream json_stream ( json_str );
78
-
79
- serializeYml( json_stream, Serial, YAMLParser::OUTPUT_YAML );
80
-
85
+ YAMLNode yamlnode = YAMLNode::loadString( yaml_str );
86
+ serializeYml ( yamlnode.getDocument(), Serial, OUTPUT_YAML );
81
87
```
82
88
83
89
----------------------------
@@ -123,9 +129,9 @@ See the [motivational post](https://github.com/bblanchon/ArduinoJson/issues/1808
123
129
124
130
ArduinoJson support is implicitely enabled on most platforms except for ESP32 where dependencies can be detected.
125
131
132
+ ***** ESP32 plaforms must include ArduinoJson.h before ArduinoYaml.h or bindings will be disabled!******
126
133
127
134
``` cpp
128
- // ESP32 plaforms must include ArduinoJson before ArduinoYaml or functions will be disabled
129
135
#include < ArduinoJson.h>
130
136
#include < ArduinoYaml.h>
131
137
```
@@ -172,6 +178,8 @@ size_t serializeYml( cJSON* src_obj, Stream &dest_stream );
172
178
int deserializeYml( cJSON* dest_obj, const char* src_yaml_str );
173
179
// YAML stream to cJSON object
174
180
int deserializeYml( cJSON* dest_obj, Stream &src_stream );
181
+ // YAML document to cJSON object
182
+ int deserializeYml( cJSON** dest_obj, yaml_document_t* src_document );
175
183
176
184
```
177
185
@@ -235,7 +243,7 @@ Set custom JSON indentation and folding depth:
235
243
236
244
```cpp
237
245
// this set two spaces per indentation level, unfolds up to 8 nesting levels
238
- YAMLParser ::setJSONIndent(" ", 8 ); // lame fact: folds on objects, not on arrays
246
+ YAML ::setJSONIndent(" ", 8 ); // lame fact: folds on objects, not on arrays
239
247
240
248
```
241
249
@@ -251,43 +259,106 @@ YAML::setYAMLIndent( 3 );
251
259
252
260
----------------------------
253
261
262
+ ## YAML gettext Module
263
+
264
+ The gettext module is a member of YAMLNode object.
265
+
266
+ ```cpp
267
+ class YAMLNode
268
+ {
269
+ // (...)
270
+ public:
271
+ const char* gettext( const char* path, char delimiter=':' );
272
+ // YAMLNode objects also bring few interesting methods to scope:
273
+ const char* scalar();
274
+ size_t size();
275
+ bool isScalar();
276
+ bool isSequence();
277
+ bool isMap();
278
+ bool isNull();
279
+ // (...)
280
+ }
281
+ ```
282
+
283
+ #### Usage (persistent)
284
+
285
+ Load from string:
286
+ ``` cpp
287
+ YAMLNode yamlnode = YAMLNode::loadString( yaml_or_json_string );
288
+ ```
289
+
290
+ Load from stream:
291
+ ``` cpp
292
+ YAMLNode yamlnode = YAMLNode::loadStream( yaml_or_json_stream );
293
+ ```
294
+
295
+
296
+ Access a value:
297
+ ``` cpp
298
+ const char * text = yamlnode.gettext( " path:to:property:name" );
299
+ ```
300
+
301
+ #### Usage (non persistent)
302
+
303
+ YAMLNode supports chaining:
304
+
305
+ ``` cpp
306
+ // load yaml and extract value from 'stuff'
307
+ YAMLNode::loadString ("blah:\n stuff:\n true\n").gettext("blah: stuff ");
308
+ // load json and extract value from 'stuff'
309
+ YAMLNode::loadString("{\" blah\" :{\" stuff\" :\" true\" }}").gettext("blah: stuff ");
310
+ ```
311
+
312
+
313
+ ## I18N/L10N with gettext Module
314
+
315
+ Note: i18n Support is disabled with WIO Terminal (platform needs a proper `fs::FS` filesystem implementation).
316
+ WIO Terminal can still use the native `YAMLNode::gettext()` though.
254
317
255
- ## I18N and L10N
256
318
257
- Note: Support is disabled with WIO Terminal (needs a proper fs::FS implementation).
319
+ #### Usage
258
320
259
- * Load the module with `#include <i18n/i18n.hpp>`.
260
- * Assign a filesystem with ` i18n.setFS() `.
261
- * Load a locale with `i18n.setLocale()`.
321
+ * Include ArduinoJson and a `fs::FS` filesystem first
322
+ * Create an i18n instance and assign the filesystem `i18n_t i18n( &LittleFS ); `.
323
+ * Load `en-GB` locale with `i18n.setLocale("en-GB" )`.
262
324
* Use `i18n.gettext()` to access localized strings.
263
325
264
326
327
+ #### Example
328
+
329
+ YAML Sample `/lang/en-GB.yml` stored in LittleFS:
330
+
331
+ ```yml
332
+ en-GB:
333
+ hello: world
334
+ blah:
335
+ my_array:
336
+ - first
337
+ - second
338
+ - third
339
+
340
+ ```
341
+
342
+ Load the language file and access translations:
343
+
344
+
265
345
``` cpp
266
346
267
- #include <LittleFS.h>
268
- #include <ArduinoJson.h>
269
- #define YAML_DISABLE_CJSON // not needed here
270
- #include <YAMLDuino.h>
271
- #include <i18n/i18n.hpp>
347
+ #include < LittleFS.h> // Mandatory filestem (can be SPIFFS, SD, SD_MMC, LittleFS)
348
+ #include < YAMLDuino.h> // Load the library
272
349
273
- // Sample example `/lang/en-GB.yml` stored in LittleFS:
274
- //
275
- // en-GB:
276
- // hello: world
277
- // blah:
278
- // my_array:
279
- // - first
280
- // - second
281
- // - third
282
350
351
+ i18n_t i18n ( &LittleFS ); // Create an i18n instance attached to filesystem
283
352
284
353
void setup()
285
354
{
286
355
Serial.begin(115200);
287
356
LittleFS.begin();
288
357
289
- i18n.setFS( &LittleFS ); // assign LittleFS
290
- i18n.setLocale("en-GB"); // will load "/lang/en-GB.yml" language file
358
+ // i18n.setFS( &SD ); // change filesystem to SD
359
+ i18n.setLocale("en-GB"); // This will look for "en-GB.yml" language file in "/lang/" folder and set "en-GB" as locale
360
+ // i18n.setLocale("/lang/en-GB.yml"); // This will load "/lang/en-GB.yml" language file and set "en-GB" as locale
361
+ // i18n.setLocale("en-GB", "/non-locale/file.yml"); // This will set "en-GB" as locale and load arbitrary "/non-locale/file.yml" language file
291
362
292
363
Serial.println( i18n.gettext("hello" ) ); // prints "world"
293
364
Serial.println( i18n.gettext("blah:my_array:2" ) ); // prints "third"
@@ -327,7 +398,7 @@ Set library debug level:
327
398
// LogLevelInfo : Errors+Warnings+Info
328
399
// LogLevelDebug : Errors+Warnings+Info+Debug
329
400
// LogLevelVerbose : Errors+Warnings+Info+Debug+Verbose
330
- YAMLParser ::setLogLevel( YAML::LogLevelDebug );
401
+ YAML ::setLogLevel ( YAML::LogLevelDebug );
331
402
```
332
403
333
404
----------------------------
@@ -355,6 +426,7 @@ project. Thanks in advance!
355
426
- [@DaveGamble](https://github.com/DaveGamble)
356
427
- [@bblanchon](https://github.com/bblanchon)
357
428
- [@vikman90](https://github.com/vikman90/yaml2json)
429
+ - [@Visse](https://github.com/Visse/libyaml-cpp)
358
430
359
431
360
432
@@ -364,3 +436,4 @@ project. Thanks in advance!
364
436
- ArduinoStreamUtils : https://github.com/bblanchon/ArduinoStreamUtils
365
437
- cJSON : https://github.com/DaveGamble/cJSON
366
438
- libyaml : https://github.com/yaml/libyaml
439
+
0 commit comments