Skip to content

Commit bc40a8e

Browse files
authored
Merge pull request #22 from tobozo/1.4.2
backported ArduinoJson 6.x
2 parents de7cfb9 + b61db69 commit bc40a8e

File tree

5 files changed

+125
-55
lines changed

5 files changed

+125
-55
lines changed

examples/test/platformio.ini

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
[platformio]
22
description = YAMLDuino test sketch for platformio
3-
default_envs = esp32_2_0_5
3+
;default_envs = esp32_2_0_5
44
src_dir = src
55

66

77
[env]
88
framework = arduino
9+
platform = espressif32
910
lib_deps =
10-
bblanchon/ArduinoJson @ ^6
11+
bblanchon/ArduinoJson
1112
; tobozo/YAMLDuino @ ^1.2
1213
YAMLDuino
1314
lib_ldf_mode = deep
1415

16+
[env:arduinojson6x]
17+
lib_deps =
18+
bblanchon/ArduinoJson @ ^6
19+
YAMLDuino
20+
21+
[env:arduinojson7x]
22+
lib_deps =
23+
bblanchon/ArduinoJson @ ^7
24+
YAMLDuino
25+
26+
[env:esp32-arduinojson6x]
27+
extends = env:arduinojson6x
28+
board = esp32dev
29+
30+
[env:esp32-arduinojson7x]
31+
extends = env:arduinojson7x
32+
board = esp32dev
1533

16-
[env:esp32_2_0_5]
17-
board = esp32dev
18-
platform = espressif32
1934
; or alternate platform/package:
2035
;platform = https://github.com/tasmota/platform-espressif32
2136
;platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32/releases/download/2.0.5/esp32-2.0.5.zip

examples/test/src/test.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

22
#include <ArduinoJson.h>
33

4+
// very dirty but necessary for the CI test
5+
// don't do that in your project!!
46
#if ARDUINOJSON_VERSION_MAJOR<7
5-
#error "ArduinoJSON version is deprecated, please upgrade to 7.x"
7+
#define ARDUINOJSONDOC DynamicJsonDocument json_doc(2048)
8+
#else
9+
#define ARDUINOJSONDOC JsonDocument json_doc
610
#endif
711

812
//#define YAML_DISABLE_CJSON // not needed here
@@ -207,7 +211,7 @@ void test_Json_gettext_stream()
207211
{
208212
String yaml_str = String( yaml_sample_str );
209213
StringStream yaml_stream( yaml_str );
210-
JsonDocument json_doc;
214+
ARDUINOJSONDOC;
211215
JsonObject json_obj = json_doc.to<JsonObject>();
212216
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
213217
if( err ) {
@@ -223,7 +227,7 @@ void test_Json_gettext_stream()
223227

224228
void test_deserializeYml_JsonObject_YamlString()
225229
{
226-
JsonDocument json_doc;
230+
ARDUINOJSONDOC;
227231
JsonObject json_obj = json_doc.to<JsonObject>();
228232
auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject
229233
if( err ) {
@@ -238,7 +242,7 @@ void test_Json_gettext_stream()
238242

239243
void test_deserializeYml_JsonDocument_YamlStream()
240244
{
241-
JsonDocument json_doc;
245+
ARDUINOJSONDOC;
242246
String yaml_str = String( yaml_sample_str );
243247
StringStream yaml_stream( yaml_str );
244248
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
@@ -256,7 +260,7 @@ void test_Json_gettext_stream()
256260
void test_deserializeYml_JsonDocument_YamlString()
257261
{
258262
String yaml_str( yaml_sample_str );
259-
JsonDocument json_doc;
263+
ARDUINOJSONDOC;
260264
auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument
261265
if( err ) {
262266
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
@@ -275,13 +279,13 @@ void test_Json_gettext_stream()
275279
String str_yaml_out = ""; // YAML output string
276280
String json_str = String( json_sample_str );
277281
StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
278-
JsonDocument doc; // create and populate a JsonObject
279-
auto err = deserializeJson( doc, json_str.c_str() );
282+
ARDUINOJSONDOC; // create and populate a JsonObject
283+
auto err = deserializeJson( json_doc, json_str.c_str() );
280284
if( err ) {
281285
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
282286
return;
283287
}
284-
JsonObject json_obj = doc.as<JsonObject>();
288+
JsonObject json_obj = json_doc.as<JsonObject>();
285289
const size_t bytes_out = serializeYml( json_obj, yaml_stream_out );
286290
Serial.println( str_yaml_out );
287291
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()
293297
// Convert JsonObject to yaml
294298
String str_yaml_out = ""; // YAML output string
295299
String json_str = String( json_sample_str );
296-
JsonDocument doc; // create and populate a JsonObject
297-
auto err = deserializeJson( doc, json_str.c_str() );
300+
ARDUINOJSONDOC; // create and populate a JsonObject
301+
auto err = deserializeJson( json_doc, json_str.c_str() );
298302
if( err ) {
299303
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
300304
return;
301305
}
302-
JsonObject json_obj = doc.as<JsonObject>();
306+
JsonObject json_obj = json_doc.as<JsonObject>();
303307
const size_t bytes_out = serializeYml( json_obj, str_yaml_out );
304308
Serial.println( str_yaml_out );
305309
YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out );

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=YAMLDuino
2-
version=1.4.1
2+
version=1.4.2
33
author=tobozo <[email protected]>
44
maintainer=tobozo <[email protected]>
55
sentence=A simple and efficient YAML library for embedded C++

src/ArduinoYaml.cpp

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,46 +1042,92 @@ namespace YAML
10421042
break;
10431043
case YAML_SEQUENCE_NODE:
10441044
{
1045-
jsonNode[(char*)nodename].to<JsonArray>();
1046-
JsonArray nodeArray = jsonNode[(char*)nodename];//.to<JsonArray>();
1045+
#if ARDUINOJSON_VERSION_MAJOR<7
1046+
1047+
JsonArray tmpArray = jsonNode.createNestedArray((char*)nodename);
1048+
yaml_node_item_t * item_i;
1049+
yaml_node_t *itemNode;
1050+
String _nodeItemName;
1051+
JsonObject tmpObj;
1052+
for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) {
1053+
itemNode = yaml_document_get_node(document, *item_i);
1054+
if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects
1055+
tmpObj = tmpArray.createNestedObject(); // insert empty nested object
1056+
_nodeItemName = ROOT_NODE + String( nodename ) + String( tmpArray.size() ); // generate a temporary nodename
1057+
tmpObj.createNestedObject((char*)_nodeItemName.c_str());
1058+
deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name
1059+
jsonNode[nodename][tmpArray.size()-1] = tmpObj[_nodeItemName.c_str()]; // remove temporary name and make object anonymous
1060+
} else { // array of sequences or values
1061+
_nodeItemName = "" + String( nodename );
1062+
deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 );
1063+
}
1064+
}
10471065

1048-
yaml_node_item_t * item_i;
1049-
yaml_node_t *itemNode;
1050-
String _nodeItemName;
1051-
JsonDocument copyDoc;
1052-
JsonObject tmpObj;
1053-
for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) {
1054-
itemNode = yaml_document_get_node(document, *item_i);
1055-
if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects
1056-
tmpObj = nodeArray.add<JsonObject>(); // insert empty nested object
1057-
_nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename
1058-
tmpObj[(char*)_nodeItemName.c_str()].to<JsonObject>();
1059-
deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name
1060-
copyDoc.set(tmpObj[_nodeItemName]); // make object anonymous, remove temporary nodename
1061-
nodeArray[nodeArray.size()-1].set(copyDoc); // replace array item by reparented node
1062-
} else { // array of sequences or values
1063-
_nodeItemName = "" + String( nodename );
1064-
deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 );
1066+
#else
1067+
1068+
jsonNode[(char*)nodename].to<JsonArray>();
1069+
JsonArray nodeArray = jsonNode[(char*)nodename];//.to<JsonArray>();
1070+
1071+
yaml_node_item_t * item_i;
1072+
yaml_node_t *itemNode;
1073+
String _nodeItemName;
1074+
JsonDocument copyDoc;
1075+
JsonObject tmpObj;
1076+
for (item_i = yamlNode->data.sequence.items.start; item_i < yamlNode->data.sequence.items.top; ++item_i) {
1077+
itemNode = yaml_document_get_node(document, *item_i);
1078+
if( itemNode->type == YAML_MAPPING_NODE ) { // array of anonymous objects
1079+
tmpObj = nodeArray.add<JsonObject>(); // insert empty nested object
1080+
_nodeItemName = ROOT_NODE + String( nodename ) + String( nodeArray.size() ); // generate a temporary nodename
1081+
tmpObj[(char*)_nodeItemName.c_str()].to<JsonObject>();
1082+
deserializeYml_JsonObject( document, itemNode, tmpObj, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 ); // go recursive using temporary node name
1083+
copyDoc.set(tmpObj[_nodeItemName]); // make object anonymous, remove temporary nodename
1084+
nodeArray[nodeArray.size()-1].set(copyDoc); // replace array item by reparented node
1085+
} else { // array of sequences or values
1086+
_nodeItemName = "" + String( nodename );
1087+
deserializeYml_JsonObject( document, itemNode, jsonNode, YAMLNode::Type::Sequence, _nodeItemName.c_str(), depth+1 );
1088+
}
10651089
}
1066-
}
1090+
1091+
#endif
10671092
}
10681093
break;
10691094
case YAML_MAPPING_NODE:
10701095
{
1071-
JsonObject tmpNode = isRootNode ? jsonNode : jsonNode[(char*)nodename].to<JsonObject>();
1072-
yaml_node_pair_t* pair_i;
1073-
yaml_node_t* key;
1074-
yaml_node_t* value;
1075-
for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) {
1076-
key = yaml_document_get_node(document, pair_i->key);
1077-
value = yaml_document_get_node(document, pair_i->value);
1078-
if (key->type != YAML_SCALAR_NODE) {
1079-
YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) );
1080-
continue;
1096+
#if ARDUINOJSON_VERSION_MAJOR<7
1097+
1098+
JsonObject tmpNode = isRootNode ? jsonNode : jsonNode.createNestedObject((char*)nodename);
1099+
yaml_node_pair_t* pair_i;
1100+
yaml_node_t* key;
1101+
yaml_node_t* value;
1102+
for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) {
1103+
key = yaml_document_get_node(document, pair_i->key);
1104+
value = yaml_document_get_node(document, pair_i->value);
1105+
if (key->type != YAML_SCALAR_NODE) {
1106+
YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) );
1107+
continue;
1108+
}
1109+
tmpNode.createNestedObject( SCALAR_s(key) );
1110+
deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 );
10811111
}
1082-
tmpNode[SCALAR_s(key)].add<JsonObject>();
1083-
deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 );
1084-
}
1112+
1113+
#else
1114+
1115+
JsonObject tmpNode = isRootNode ? jsonNode : jsonNode[(char*)nodename].to<JsonObject>();
1116+
yaml_node_pair_t* pair_i;
1117+
yaml_node_t* key;
1118+
yaml_node_t* value;
1119+
for (pair_i = yamlNode->data.mapping.pairs.start; pair_i < yamlNode->data.mapping.pairs.top; ++pair_i) {
1120+
key = yaml_document_get_node(document, pair_i->key);
1121+
value = yaml_document_get_node(document, pair_i->value);
1122+
if (key->type != YAML_SCALAR_NODE) {
1123+
YAML_LOG_e("Mapping key is not scalar (line %lu, val=%s).", key->start_mark.line, SCALAR_c(value) );
1124+
continue;
1125+
}
1126+
tmpNode[SCALAR_s(key)].add<JsonObject>();
1127+
deserializeYml_JsonObject( document, value, tmpNode, YAMLNode::Type::Map, SCALAR_c(key), depth+1 );
1128+
}
1129+
1130+
#endif
10851131
}
10861132
break;
10871133
case YAML_NO_NODE: YAML_LOG_e("YAML_NO_NODE");

src/ArduinoYaml.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ extern "C"
8585

8686
#if defined HAS_ARDUINOJSON
8787
#include <ArduinoJson.h>
88-
#if ARDUINOJSON_VERSION_MAJOR<7
89-
#error "ArduinoJSON version is deprecated, please upgrade to 7.x"
90-
#endif
9188
#endif
9289

9390
#if defined HAS_CJSON
@@ -294,13 +291,21 @@ namespace YAML
294291
public:
295292
YAMLToArduinoJson() {};
296293
~YAMLToArduinoJson() { if( _doc) delete _doc; }
297-
void setJsonDocument( const size_t capacity ) { _doc = new JsonDocument; _root = _doc->to<JsonObject>(); }
294+
#if ARDUINOJSON_VERSION_MAJOR<7
295+
void setJsonDocument( const size_t capacity ) { _doc = new DynamicJsonDocument(capacity); _root = _doc->to<JsonObject>(); }
296+
#else
297+
void setJsonDocument( const size_t capacity ) { _doc = new JsonDocument; _root = _doc->to<JsonObject>(); }
298+
#endif
298299
JsonObject& getJsonObject() { return _root; }
299300
static DeserializationError toJsonObject( Stream &src, JsonObject& output );
300301
static DeserializationError toJsonObject( const char* src, JsonObject& output );
301302

302303
private:
303-
JsonDocument *_doc = nullptr;
304+
#if ARDUINOJSON_VERSION_MAJOR<7
305+
DynamicJsonDocument *_doc = nullptr;
306+
#else
307+
JsonDocument *_doc = nullptr;
308+
#endif
304309
JsonObject _root;
305310

306311
};

0 commit comments

Comments
 (0)