From b61db696a745db5c287ca5b62f964cd0de24d85d Mon Sep 17 00:00:00 2001 From: tobozo Date: Tue, 18 Jun 2024 16:37:18 +0200 Subject: [PATCH] backported ArduinoJson 6.x --- examples/test/platformio.ini | 25 ++++++-- examples/test/src/test.cpp | 26 ++++---- library.properties | 2 +- src/ArduinoYaml.cpp | 112 ++++++++++++++++++++++++----------- src/ArduinoYaml.hpp | 15 +++-- 5 files changed, 125 insertions(+), 55 deletions(-) diff --git a/examples/test/platformio.ini b/examples/test/platformio.ini index c0327e7..3fc694f 100644 --- a/examples/test/platformio.ini +++ b/examples/test/platformio.ini @@ -1,21 +1,36 @@ [platformio] description = YAMLDuino test sketch for platformio -default_envs = esp32_2_0_5 +;default_envs = esp32_2_0_5 src_dir = src [env] framework = arduino +platform = espressif32 lib_deps = - bblanchon/ArduinoJson @ ^6 + bblanchon/ArduinoJson ; tobozo/YAMLDuino @ ^1.2 YAMLDuino lib_ldf_mode = deep +[env:arduinojson6x] +lib_deps = + bblanchon/ArduinoJson @ ^6 + YAMLDuino + +[env:arduinojson7x] +lib_deps = + bblanchon/ArduinoJson @ ^7 + YAMLDuino + +[env:esp32-arduinojson6x] +extends = env:arduinojson6x +board = esp32dev + +[env:esp32-arduinojson7x] +extends = env:arduinojson7x +board = esp32dev -[env:esp32_2_0_5] -board = esp32dev -platform = espressif32 ; or alternate platform/package: ;platform = https://github.com/tasmota/platform-espressif32 ;platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32/releases/download/2.0.5/esp32-2.0.5.zip diff --git a/examples/test/src/test.cpp b/examples/test/src/test.cpp index e81cb79..66b8267 100644 --- a/examples/test/src/test.cpp +++ b/examples/test/src/test.cpp @@ -1,8 +1,12 @@ #include +// very dirty but necessary for the CI test +// don't do that in your project!! #if ARDUINOJSON_VERSION_MAJOR<7 - #error "ArduinoJSON version is deprecated, please upgrade to 7.x" + #define ARDUINOJSONDOC DynamicJsonDocument json_doc(2048) +#else + #define ARDUINOJSONDOC JsonDocument json_doc #endif //#define YAML_DISABLE_CJSON // not needed here @@ -207,7 +211,7 @@ void test_Json_gettext_stream() { String yaml_str = String( yaml_sample_str ); StringStream yaml_stream( yaml_str ); - JsonDocument json_doc; + ARDUINOJSONDOC; JsonObject json_obj = json_doc.to(); auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject if( err ) { @@ -223,7 +227,7 @@ void test_Json_gettext_stream() void test_deserializeYml_JsonObject_YamlString() { - JsonDocument json_doc; + ARDUINOJSONDOC; JsonObject json_obj = json_doc.to(); auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject if( err ) { @@ -238,7 +242,7 @@ void test_Json_gettext_stream() void test_deserializeYml_JsonDocument_YamlStream() { - JsonDocument json_doc; + ARDUINOJSONDOC; String yaml_str = String( yaml_sample_str ); StringStream yaml_stream( yaml_str ); auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument @@ -256,7 +260,7 @@ void test_Json_gettext_stream() void test_deserializeYml_JsonDocument_YamlString() { String yaml_str( yaml_sample_str ); - JsonDocument json_doc; + ARDUINOJSONDOC; auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument if( err ) { YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); @@ -275,13 +279,13 @@ void test_Json_gettext_stream() String str_yaml_out = ""; // YAML output string String json_str = String( json_sample_str ); StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out - JsonDocument doc; // create and populate a JsonObject - auto err = deserializeJson( doc, json_str.c_str() ); + ARDUINOJSONDOC; // create and populate a JsonObject + auto err = deserializeJson( json_doc, json_str.c_str() ); if( err ) { YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() ); return; } - JsonObject json_obj = doc.as(); + JsonObject json_obj = json_doc.as(); const size_t bytes_out = serializeYml( json_obj, yaml_stream_out ); Serial.println( str_yaml_out ); YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out ); @@ -293,13 +297,13 @@ void test_Json_gettext_stream() // Convert JsonObject to yaml String str_yaml_out = ""; // YAML output string String json_str = String( json_sample_str ); - JsonDocument doc; // create and populate a JsonObject - auto err = deserializeJson( doc, json_str.c_str() ); + ARDUINOJSONDOC; // create and populate a JsonObject + auto err = deserializeJson( json_doc, json_str.c_str() ); if( err ) { YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() ); return; } - JsonObject json_obj = doc.as(); + JsonObject json_obj = json_doc.as(); const size_t bytes_out = serializeYml( json_obj, str_yaml_out ); Serial.println( str_yaml_out ); YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out ); diff --git a/library.properties b/library.properties index e5cd32e..8145549 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=YAMLDuino -version=1.4.1 +version=1.4.2 author=tobozo maintainer=tobozo sentence=A simple and efficient YAML library for embedded C++ diff --git a/src/ArduinoYaml.cpp b/src/ArduinoYaml.cpp index d510b90..7d6f1b7 100644 --- a/src/ArduinoYaml.cpp +++ b/src/ArduinoYaml.cpp @@ -1042,46 +1042,92 @@ namespace YAML break; case YAML_SEQUENCE_NODE: { - jsonNode[(char*)nodename].to(); - JsonArray nodeArray = jsonNode[(char*)nodename];//.to(); + #if ARDUINOJSON_VERSION_MAJOR<7 + + JsonArray tmpArray = jsonNode.createNestedArray((char*)nodename); + yaml_node_item_t * item_i; + yaml_node_t *itemNode; + String _nodeItemName; + JsonObject tmpObj; + for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) { + itemNode = yaml_document_get_node(document, *item_i); + if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects + tmpObj = tmpArray.createNestedObject(); // insert empty nested object + _nodeItemName = ROOT_NODE + String( nodename ) + String( tmpArray.size() ); // generate a temporary nodename + tmpObj.createNestedObject((char*)_nodeItemName.c_str()); + deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name + jsonNode[nodename][tmpArray.size()-1] = tmpObj[_nodeItemName.c_str()]; // remove temporary name and make object anonymous + } else { // array of sequences or values + _nodeItemName = "" + String( nodename ); + deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); + } + } - yaml_node_item_t * item_i; - yaml_node_t *itemNode; - String _nodeItemName; - JsonDocument copyDoc; - JsonObject tmpObj; - for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) { - itemNode = yaml_document_get_node(document, *item_i); - if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects - tmpObj = nodeArray.add(); // insert empty nested object - _nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename - tmpObj[(char*)_nodeItemName.c_str()].to(); - deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name - copyDoc.set(tmpObj[_nodeItemName]); // make object anonymous, remove temporary nodename - nodeArray[nodeArray.size()-1].set(copyDoc); // replace array item by reparented node - } else { // array of sequences or values - _nodeItemName = "" + String( nodename ); - deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); + #else + + jsonNode[(char*)nodename].to(); + JsonArray nodeArray = jsonNode[(char*)nodename];//.to(); + + yaml_node_item_t * item_i; + yaml_node_t *itemNode; + String _nodeItemName; + JsonDocument copyDoc; + JsonObject tmpObj; + for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) { + itemNode = yaml_document_get_node(document, *item_i); + if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects + tmpObj = nodeArray.add(); // insert empty nested object + _nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename + tmpObj[(char*)_nodeItemName.c_str()].to(); + deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name + copyDoc.set(tmpObj[_nodeItemName]); // make object anonymous, remove temporary nodename + nodeArray[nodeArray.size()-1].set(copyDoc); // replace array item by reparented node + } else { // array of sequences or values + _nodeItemName = "" + String( nodename ); + deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); + } } - } + + #endif } break; case YAML_MAPPING_NODE: { - JsonObject tmpNode = isRootNode ? jsonNode : jsonNode[(char*)nodename].to(); - yaml_node_pair_t* pair_i; - yaml_node_t* key; - yaml_node_t* value; - for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) { - key = yaml_document_get_node(document, pair_i->key); - value = yaml_document_get_node(document, pair_i->value); - if (key->type != YAML_SCALAR_NODE) { - YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) ); - continue; + #if ARDUINOJSON_VERSION_MAJOR<7 + + JsonObject tmpNode = isRootNode ? jsonNode : jsonNode.createNestedObject((char*)nodename); + yaml_node_pair_t* pair_i; + yaml_node_t* key; + yaml_node_t* value; + for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) { + key = yaml_document_get_node(document, pair_i->key); + value = yaml_document_get_node(document, pair_i->value); + if (key->type != YAML_SCALAR_NODE) { + YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) ); + continue; + } + tmpNode.createNestedObject( SCALAR_s(key) ); + deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 ); } - tmpNode[SCALAR_s(key)].add(); - deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 ); - } + + #else + + JsonObject tmpNode = isRootNode ? jsonNode : jsonNode[(char*)nodename].to(); + yaml_node_pair_t* pair_i; + yaml_node_t* key; + yaml_node_t* value; + for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) { + key = yaml_document_get_node(document, pair_i->key); + value = yaml_document_get_node(document, pair_i->value); + if (key->type != YAML_SCALAR_NODE) { + YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) ); + continue; + } + tmpNode[SCALAR_s(key)].add(); + deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 ); + } + + #endif } break; case YAML_NO_NODE: YAML_LOG_e("YAML_NO_NODE"); diff --git a/src/ArduinoYaml.hpp b/src/ArduinoYaml.hpp index 86900de..2888e26 100644 --- a/src/ArduinoYaml.hpp +++ b/src/ArduinoYaml.hpp @@ -85,9 +85,6 @@ extern "C" #if defined HAS_ARDUINOJSON #include - #if ARDUINOJSON_VERSION_MAJOR<7 - #error "ArduinoJSON version is deprecated, please upgrade to 7.x" - #endif #endif #if defined HAS_CJSON @@ -294,13 +291,21 @@ namespace YAML public: YAMLToArduinoJson() {}; ~YAMLToArduinoJson() { if( _doc) delete _doc; } - void setJsonDocument( const size_t capacity ) { _doc = new JsonDocument; _root = _doc->to(); } + #if ARDUINOJSON_VERSION_MAJOR<7 + void setJsonDocument( const size_t capacity ) { _doc = new DynamicJsonDocument(capacity); _root = _doc->to(); } + #else + void setJsonDocument( const size_t capacity ) { _doc = new JsonDocument; _root = _doc->to(); } + #endif JsonObject& getJsonObject() { return _root; } static DeserializationError toJsonObject( Stream &src, JsonObject& output ); static DeserializationError toJsonObject( const char* src, JsonObject& output ); private: - JsonDocument *_doc = nullptr; + #if ARDUINOJSON_VERSION_MAJOR<7 + DynamicJsonDocument *_doc = nullptr; + #else + JsonDocument *_doc = nullptr; + #endif JsonObject _root; };