Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backported ArduinoJson 6.x #22

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions examples/test/platformio.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 15 additions & 11 deletions examples/test/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

#include <ArduinoJson.h>

// 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
Expand Down Expand Up @@ -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<JsonObject>();
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
if( err ) {
Expand All @@ -223,7 +227,7 @@ void test_Json_gettext_stream()

void test_deserializeYml_JsonObject_YamlString()
{
JsonDocument json_doc;
ARDUINOJSONDOC;
JsonObject json_obj = json_doc.to<JsonObject>();
auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject
if( err ) {
Expand All @@ -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
Expand All @@ -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() );
Expand All @@ -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>();
JsonObject json_obj = json_doc.as<JsonObject>();
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 );
Expand All @@ -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>();
JsonObject json_obj = json_doc.as<JsonObject>();
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 );
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=YAMLDuino
version=1.4.1
version=1.4.2
author=tobozo <[email protected]>
maintainer=tobozo <[email protected]>
sentence=A simple and efficient YAML library for embedded C++
Expand Down
112 changes: 79 additions & 33 deletions src/ArduinoYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,46 +1042,92 @@ namespace YAML
break;
case YAML_SEQUENCE_NODE:
{
jsonNode[(char*)nodename].to<JsonArray>();
JsonArray nodeArray = jsonNode[(char*)nodename];//.to<JsonArray>();
#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<JsonObject>(); // insert empty nested object
_nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename
tmpObj[(char*)_nodeItemName.c_str()].to<JsonObject>();
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>();
JsonArray nodeArray = jsonNode[(char*)nodename];//.to<JsonArray>();

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<JsonObject>(); // insert empty nested object
_nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename
tmpObj[(char*)_nodeItemName.c_str()].to<JsonObject>();
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<JsonObject>();
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<JsonObject>();
deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 );
}

#else

JsonObject tmpNode = isRootNode ? jsonNode : jsonNode[(char*)nodename].to<JsonObject>();
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<JsonObject>();
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");
Expand Down
15 changes: 10 additions & 5 deletions src/ArduinoYaml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ extern "C"

#if defined HAS_ARDUINOJSON
#include <ArduinoJson.h>
#if ARDUINOJSON_VERSION_MAJOR<7
#error "ArduinoJSON version is deprecated, please upgrade to 7.x"
#endif
#endif

#if defined HAS_CJSON
Expand Down Expand Up @@ -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<JsonObject>(); }
#if ARDUINOJSON_VERSION_MAJOR<7
void setJsonDocument( const size_t capacity ) { _doc = new DynamicJsonDocument(capacity); _root = _doc->to<JsonObject>(); }
#else
void setJsonDocument( const size_t capacity ) { _doc = new JsonDocument; _root = _doc->to<JsonObject>(); }
#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;

};
Expand Down
Loading