### Load and Use Localized Strings with gettext Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Example of setting up and using the i18n module for internationalization. Requires LittleFS or another compatible filesystem and the YAMLDuino library. Ensure the locale file is correctly placed. ```yml en-GB: hello: world blah: my_array: - first - second - third ``` ```cpp #include // Mandatory filestem (can be SPIFFS, SD, SD_MMC, LittleFS) #include // Load the library i18n_t i18n( &LittleFS ); // Create an i18n instance attached to filesystem void setup() { Serial.begin(115200); LittleFS.begin(); // i18n.setFS( &SD ); // change filesystem to SD i18n.setLocale("en-GB"); // This will look for "en-GB.yml" language file in "/lang/" folder and set "en-GB" as locale // i18n.setLocale("/lang/en-GB.yml"); // This will load "/lang/en-GB.yml" language file and set "en-GB" as locale // i18n.setLocale("en-GB", "/non-locale/file.yml"); // This will set "en-GB" as locale and load arbitrary "/non-locale/file.yml" language file Serial.println( i18n.gettext("hello" ) ); // prints "world" Serial.println( i18n.gettext("blah:my_array:2" ) ); // prints "third" } void loop() { delay(1000); } ``` -------------------------------- ### Include YAMLDuino Library Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Include the YAMLDuino library in your Arduino sketch. Use the appropriate header based on your project setup. ```cpp #include ``` ```cpp #include ``` -------------------------------- ### StringStream Helper Class Initialization Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Demonstrates initializing the StringStream helper class with a String for input and an empty String for output. ```cpp String my_json = "{\"blah\":true}"; StringStream json_input_stream(my_json); String my_output; StringStream output_stream(my_output); ``` -------------------------------- ### Load YAML and JSON Strings Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Demonstrates loading YAML and JSON strings and extracting text values using chaining. Ensure the YAMLNode class is available. ```cpp // load yaml and extract value from 'stuff' YAMLNode::loadString("blah:\n stuff:\n true\n").gettext("blah:stuff"); // load json and extract value from 'stuff' YAMLNode::loadString("{\"blah\":{\"stuff\":\"true\"}}").gettext("blah:stuff"); ``` -------------------------------- ### Include ArduinoJson and ArduinoYaml Headers Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Include the necessary headers for ArduinoJson and ArduinoYaml. Ensure ArduinoJson.h is included before ArduinoYaml.h on ESP32 platforms. ```cpp #include #include ``` -------------------------------- ### StringStream Helper Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md A helper class for treating Arduino Strings as memory-efficient streams. ```APIDOC ## String/Stream Helper ### Description Provides a `StringStream` class that inherits from `Stream` to allow `String` objects to be used with stream-based functions, offering memory efficiency over `const char*`. ### Class - `StringStream` - Constructor: `StringStream(String &s)` - Inherits from `Stream`. - Provides `available()`, `read()`, `peek()`, `write()`, and `flush()` methods. ``` -------------------------------- ### Convert YAML to Pretty JSON Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Load a YAML string into a YAMLNode and then serialize it to a pretty-printed JSON string output to the Serial stream. ```cpp String yaml_str = "hello: world\nboolean: true\nfloat: 1.2345"; YAMLNode yamlnode = YAMLNode::loadString( yaml_str ); serializeYml( yamlnode.getDocument(), Serial, OUTPUT_JSON_PRETTY ); // pretty JSON // serializeYml( yamlnode.getDocument(), Serial, OUTPUT_JSON ); // ugly JSON ``` -------------------------------- ### YAMLNode Load from String Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Loads YAML or JSON data into a YAMLNode object from a string. ```cpp YAMLNode yamlnode = YAMLNode::loadString( yaml_or_json_string ); ``` -------------------------------- ### Output Decorators Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Functions to customize JSON and YAML indentation levels. ```APIDOC ## Output Decorators ### Description Allows customization of indentation for JSON and YAML output. ### Functions - `void YAML::setYAMLIndent( int spaces_per_indent=2 );` - Sets the indentation level for YAML output. Accepts values between 2 and 16. - `void YAML::setJSONIndent( const char* spaces_or_tabs="\t", int folding_depth=4 );` - Sets the indentation (spaces or tabs) and folding depth for JSON output. ``` -------------------------------- ### Convert JSON to YAML Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Load a JSON string into a YAMLNode and then serialize it to a YAML string output to the Serial stream. ```cpp String json_str = "{\"hello\": \"world\", \"boolean\": true, \"float\":1.2345}"; YAMLNode yamlnode = YAMLNode::loadString( yaml_str ); serializeYml( yamlnode.getDocument(), Serial, OUTPUT_YAML ); ``` -------------------------------- ### Set Custom YAML Indentation Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Sets the indentation for YAML output to 3 spaces per level. The minimum is 2 and the maximum is 16 spaces. ```cpp YAML::setYAMLIndent( 3 ); ``` -------------------------------- ### Set Custom JSON Indentation and Folding Depth Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Configures JSON output with two spaces per indentation level and unfolds up to 8 nesting levels. Note that this setting folds on objects but not on arrays. ```cpp YAML::setJSONIndent(" ", 8 ); ``` -------------------------------- ### ArduinoJson Bindings Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Functions to serialize ArduinoJson objects to YAML and deserialize YAML to ArduinoJson objects. ```APIDOC ## ArduinoJson Bindings ### Description Provides functions to convert between ArduinoJson objects and YAML format, supporting both string and stream inputs/outputs. ### Functions - `size_t serializeYml( JsonVariant src_obj, String &dest_string );` - Serializes an ArduinoJson object to a YAML string. - `size_t serializeYml( JsonVariant src_obj, Stream &dest_stream );` - Serializes an ArduinoJson object to a YAML stream. - `DeserializationError deserializeYml( JsonObject &dest_obj, const char* src_yaml_str );` - Deserializes a YAML string into an ArduinoJson JsonObject. - `DeserializationError deserializeYml( JsonObject &dest_obj, Stream &src_stream );` - Deserializes a YAML stream into an ArduinoJson JsonObject. - `DeserializationError deserializeYml( JsonDocument &dest_doc, Stream &src_stream );` - Deserializes a YAML stream into an ArduinoJson JsonDocument. - `DeserializationError deserializeYml( JsonDocument &dest_doc, const char *src_yaml_str);` - Deserializes a YAML string into an ArduinoJson JsonDocument. ``` -------------------------------- ### ArduinoJson to YAML Serialization Functions Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md These functions serialize ArduinoJson objects into YAML format, either as a string or to a stream. They are part of the ArduinoJson bindings. ```cpp size_t serializeYml( JsonVariant src_obj, String &dest_string ); size_t serializeYml( JsonVariant src_obj, Stream &dest_stream ); ``` -------------------------------- ### YAMLNode Load from Stream Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Loads YAML or JSON data into a YAMLNode object from a stream. ```cpp YAMLNode yamlnode = YAMLNode::loadStream( yaml_or_json_stream ); ``` -------------------------------- ### Set Library Debug Log Level Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Allows changing the library's debug logging verbosity at runtime. Choose a level from None to Verbose based on the required detail. ```cpp // // Accepted values: // LogLevelNone : No logging // LogLevelError : Errors // LogLevelWarning : Errors+Warnings // LogLevelInfo : Errors+Warnings+Info // LogLevelDebug : Errors+Warnings+Info+Debug // LogLevelVerbose : Errors+Warnings+Info+Debug+Verbose YAML::setLogLevel( YAML::LogLevelDebug ); ``` -------------------------------- ### cJSON Bindings Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Functions to serialize cJSON objects to YAML and deserialize YAML to cJSON objects. ```APIDOC ## cJSON Bindings ### Description Provides functions to convert between cJSON objects and YAML format, supporting both string and stream inputs/outputs. Note potential memory leaks with floats in cJSON. ### Functions - `size_t serializeYml( cJSON* src_obj, String &dest_string );` - Serializes a cJSON object to a YAML string. - `size_t serializeYml( cJSON* src_obj, Stream &dest_stream );` - Serializes a cJSON object to a YAML stream. - `int deserializeYml( cJSON* dest_obj, const char* src_yaml_str );` - Deserializes a YAML string into a cJSON object. - `int deserializeYml( cJSON* dest_obj, Stream &src_stream );` - Deserializes a YAML stream into a cJSON object. - `int deserializeYml( cJSON** dest_obj, yaml_document_t* src_document );` - Deserializes a YAML document structure into a cJSON object. ``` -------------------------------- ### YAML to ArduinoJson Deserialization Functions Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md These functions deserialize YAML data from a string or stream into ArduinoJson objects or documents. They are part of the ArduinoJson bindings. ```cpp DeserializationError deserializeYml( JsonObject &dest_obj, const char* src_yaml_str ); DeserializationError deserializeYml( JsonObject &dest_obj, Stream &src_stream ); DeserializationError deserializeYml( JsonDocument &dest_doc, Stream &src_stream ); DeserializationError deserializeYml( JsonDocument &dest_doc, const char *src_yaml_str) ; ``` -------------------------------- ### YAMLNode gettext Module Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Methods for accessing YAML data using path-based queries. ```APIDOC ## YAML gettext Module ### Description Provides methods within the `YAMLNode` class to access and query YAML data structures. ### Class: `YAMLNode` #### Methods - `const char* gettext( const char* path, char delimiter=':' );` - Retrieves a value from the YAML structure using a delimited path. - `const char* scalar();` - Returns the scalar value of the node. - `size_t size();` - Returns the size of the node (for sequences or maps). - `bool isScalar();` - Checks if the node is a scalar. - `bool isSequence();` - Checks if the node is a sequence. - `bool isMap();` - Checks if the node is a map. - `bool isNull();` - Checks if the node is null. #### Static Methods - `YAMLNode YAMLNode::loadString( const char* yaml_or_json_string );` - Loads a YAML or JSON string into a `YAMLNode` object. - `YAMLNode YAMLNode::loadStream( Stream &yaml_or_json_stream );` - Loads a YAML or JSON stream into a `YAMLNode` object. ``` -------------------------------- ### cJSON to YAML Serialization Functions Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md These functions serialize cJSON objects into YAML format, either as a string or to a stream. They are part of the cJSON bindings. ```cpp size_t serializeYml( cJSON* src_obj, String &dest_string ); size_t serializeYml( cJSON* src_obj, Stream &dest_stream ); ``` -------------------------------- ### YAML to cJSON Deserialization Functions Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md These functions deserialize YAML data from a string or stream into cJSON objects. They are part of the cJSON bindings. Note the potential memory leak with floats. ```cpp int deserializeYml( cJSON* dest_obj, const char* src_yaml_str ); int deserializeYml( cJSON* dest_obj, Stream &src_stream ); int deserializeYml( cJSON** dest_obj, yaml_document_t* src_document ); ``` -------------------------------- ### YAMLNode Access Value with gettext Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md Retrieves a text value from a YAMLNode object using a delimited path string. The default delimiter is a colon ':'. ```cpp const char* text = yamlnode.gettext( "path:to:property:name" ); ``` -------------------------------- ### StringStream Class Definition Source: https://github.com/tobozo/yamlduino/blob/main/ReadMe.md The definition of the StringStream class, which inherits from Stream and provides basic stream functionality using Arduino Strings. ```cpp class StringStream : public Stream { public: StringStream(String &s) : str(s), pos(0) {} virtual ~StringStream() {}; virtual int available() { return str.length() - pos; } virtual int read() { return pos