Skip to content

Commit 3bbad14

Browse files
authored
Merge pull request #7 from tobozo/1.2.6
fixes mem leaks + implements multiline
2 parents 90f7296 + cf61b8a commit 3bbad14

File tree

8 files changed

+254
-110
lines changed

8 files changed

+254
-110
lines changed

assets/json.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"JSON":[
3+
{"J": "Java" },
4+
{"S": "Script" },
5+
{"O": "Object" },
6+
{"N": "Notation" }
7+
]
8+
}

assets/sleazy-logo-with-title.png

48.6 KB
Loading

assets/sleazy-logo.png

36.9 KB
Loading

assets/yaml.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
YAML:
2+
- Y: YAML
3+
- A: Ain't
4+
- M: Markup
5+
- L: Language

examples/test/src/test.cpp

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const char* yaml_example_str = R"_YAML_STRING_(
88
first: true
99
fourth: false
1010
blah:
11+
multiline_string: |
12+
omg I'm multiline!
13+
whelp "I'm quoted" ? ! () { } "\t\r\n"
14+
slash\ed
15+
array_of_indexed_multiline_strings:
16+
- multiline_string1: |
17+
one !
18+
two "?"
19+
three ...
20+
- multiline_string2: |
21+
four @'"]
22+
five [[(()
23+
six +-_%^&
1124
just_a_string: "I am a string"
1225
array_of_strings:
1326
# some unindented comments
@@ -39,6 +52,8 @@ const char* json_example_str = R"_JSON_STRING_(
3952
"first": true,
4053
"fourth": false,
4154
"blah": {
55+
"multiline_string": "omg I'm multiline!\nwhelp \"I'm quoted\" ? ! () { } \"\\t\\r\\n\"\nslash\\ed",
56+
"array_of_indexed_multiline_strings": [{"multiline_string1": "one !\ntwo \"?\"\nthree ..."},{"multiline_string2": "four @'\"]\nfive [[(()\nsix +-_%^&"}],
4257
"just_a_string": "I am a string",
4358
"array_of_strings": ["oops", "meh" ],
4459
"same_array_of_strings": [ "oops", "meh" ],
@@ -74,36 +89,83 @@ int test_number = 1;
7489

7590
#if defined HAS_ARDUINOJSON
7691

92+
93+
void test_deserializeYml_JsonObject_YamlStream()
94+
{
95+
#if !defined ARDUINO_ARCH_RP2040
96+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_obj, yaml_stream):", test_number++ );
97+
String yaml_str = String( yaml_example_str );
98+
StringStream yaml_stream( yaml_str );
99+
DynamicJsonDocument json_doc(2048);
100+
JsonObject json_obj = json_doc.as<JsonObject>();
101+
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
102+
if( err ) {
103+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
104+
return;
105+
}
106+
String str_json_out = ""; // JSON output string
107+
//StringStream json_stream_out( str_json_out ); // Stream to str_yaml_out
108+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
109+
Serial.println( str_json_out );
110+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
111+
#endif
112+
}
113+
114+
115+
void test_deserializeYml_JsonObject_YamlString()
116+
{
117+
#if !defined ARDUINO_ARCH_RP2040
118+
YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_obj, yaml_example_str):", test_number++ );
119+
DynamicJsonDocument json_doc(2048);
120+
JsonObject json_obj = json_doc.as<JsonObject>();
121+
auto err = deserializeYml( json_obj, yaml_example_str ); // deserialize yaml string to JsonObject
122+
if( err ) {
123+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
124+
return;
125+
}
126+
String str_json_out = ""; // JSON output string
127+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
128+
Serial.println( str_json_out );
129+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
130+
#endif
131+
}
132+
133+
134+
77135
void test_deserializeYml_JsonDocument_YamlStream()
78136
{
79137
#if !defined ARDUINO_ARCH_SAMD // samd with 32kb ram can oom after this, so only enable for specific test
80-
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_doc, yaml_stream):", test_number++ );
81-
DynamicJsonDocument json_doc(yaml_str_size*2);
138+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonDocument -> deserializeYml(json_doc, yaml_stream):", test_number++ );
139+
DynamicJsonDocument json_doc(2048);
82140
String yaml_str = String( yaml_example_str );
83141
StringStream yaml_stream( yaml_str );
84142
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
85143
if( err ) {
86144
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
87145
return;
88146
}
89-
const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject
147+
String str_json_out = ""; // JSON output string
148+
const size_t bytes_out = serializeJsonPretty( json_doc, str_json_out ); // print deserialized JsonObject
149+
Serial.println( str_json_out );
90150
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
91151
#endif
92152
}
93153

94154

95155
void test_deserializeYml_JsonDocument_YamlString()
96156
{
97-
YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_doc, yaml_example_str):", test_number++ );
98-
DynamicJsonDocument json_doc(yaml_str_size*2);
157+
YAML_LOG_n( "[TEST #%d] YAML string to JsonDocument -> deserializeYml(json_doc, yaml_example_str):", test_number++ );
99158
String yaml_str( yaml_example_str );
159+
DynamicJsonDocument json_doc(2048);
100160
auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument
101161
if( err ) {
102162
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
103163
return;
104164
}
105165
JsonObject json_obj = json_doc.as<JsonObject>();
106-
const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject
166+
String str_json_out = ""; // JSON output string
167+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
168+
Serial.println( str_json_out );
107169
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
108170
}
109171

@@ -115,8 +177,7 @@ int test_number = 1;
115177
String str_yaml_out = ""; // YAML output string
116178
String json_str = String( json_example_str );
117179
StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
118-
// create and populate a JsonObject
119-
DynamicJsonDocument doc(json_str_size*2);
180+
DynamicJsonDocument doc(2048); // create and populate a JsonObject
120181
auto err = deserializeJson( doc, json_str.c_str() );
121182
if( err ) {
122183
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
@@ -135,9 +196,7 @@ int test_number = 1;
135196
YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, str_yaml_out):", test_number++ );
136197
String str_yaml_out = ""; // YAML output string
137198
String json_str = String( json_example_str );
138-
//StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
139-
// create and populate a JsonObject
140-
DynamicJsonDocument doc(json_str_size*2);
199+
DynamicJsonDocument doc(2048); // create and populate a JsonObject
141200
auto err = deserializeJson( doc, json_str.c_str() );
142201
if( err ) {
143202
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
@@ -157,17 +216,14 @@ int test_number = 1;
157216

158217
void test_serializeYml_JsonStream_YamlStream()
159218
{
160-
#pragma message "Enabling ArduinoJson stream<=>stream tests"
161-
{
162-
YAML_LOG_n( "[TEST #%d] JSON stream to JsonObject to YAML stream -> serializeYml(stream_in, stream_out):", test_number++ );
163-
String str_yaml = "";
164-
String str_json = String( json_example_str );
165-
StringStream stream_in( str_json );
166-
StringStream stream_out( str_yaml );
167-
const size_t bytes_out = serializeYml( stream_in, stream_out );
168-
Serial.println( str_yaml );
169-
YAML_LOG_n("[JSON=>JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n", json_str_size, bytes_out);
170-
}
219+
YAML_LOG_n( "[TEST #%d] JSON stream to %s to YAML stream -> serializeYml(stream_in, stream_out):", test_number++, STREAM_TO_STREAM );
220+
String str_yaml = "";
221+
String str_json = String( json_example_str );
222+
StringStream stream_in( str_json );
223+
StringStream stream_out( str_yaml );
224+
const size_t bytes_out = serializeYml( stream_in, stream_out );
225+
Serial.println( str_yaml );
226+
YAML_LOG_n("[JSON=>JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n", json_str_size, bytes_out);
171227
}
172228

173229
#endif
@@ -180,7 +236,7 @@ int test_number = 1;
180236
{
181237
YAML_LOG_n( "YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):" );
182238
// deserialize YAML string into cJSON object
183-
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
239+
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
184240
int ret = deserializeYml( objPtr, yaml_example_str );
185241
if (!ret) {
186242
Serial.println("deserializeYml failed");
@@ -205,7 +261,7 @@ int test_number = 1;
205261
YAML_LOG_n( "YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):" );
206262
String yaml_str = String( yaml_example_str );
207263
StringStream yaml_stream( yaml_str );
208-
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
264+
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
209265
// deserialize YAML stream into cJSON object
210266
int ret = deserializeYml( objPtr, yaml_stream );
211267
if (!ret) {
@@ -264,22 +320,25 @@ void setup()
264320

265321
YAMLParser::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited)
266322

323+
324+
#if defined USE_STREAM_TO_STREAM // json->yaml stream->stream using intermediate (cJSON or ArduinoJSON) object
325+
#pragma message "Enabling ArduinoJson stream<=>stream tests"
326+
test_serializeYml_JsonStream_YamlStream();
327+
#endif
328+
329+
267330
#if defined HAS_ARDUINOJSON
268331
#pragma message "Enabling ArduinoJson tests"
269332
YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n");
270-
//test_deserializeYml_JsonObject_YamlStream();
271-
//test_deserializeYml_JsonObject_YamlString();
333+
test_deserializeYml_JsonObject_YamlStream();
334+
test_deserializeYml_JsonObject_YamlString();
272335
test_deserializeYml_JsonDocument_YamlStream();
273336
test_deserializeYml_JsonDocument_YamlString();
274337
test_serializeYml_JsonObject_YamlStream();
275338
test_serializeYml_JsonObject_YamlString();
276339
YAML_LOG_n("ArduinoJson tests complete");
277340
#endif
278341

279-
#if defined USE_STREAM_TO_STREAM // stream to stream unavailable on esp8266 (not enough memory)
280-
test_serializeYml_JsonStream_YamlStream();
281-
#endif
282-
283342

284343
#if defined HAS_CJSON
285344
#pragma message "Enabling cJSON tests"

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.2.5
2+
version=1.2.6
33
author=tobozo <[email protected]>
44
maintainer=tobozo <[email protected]>
55
sentence=A simple and efficient YAML library for embedded C++

0 commit comments

Comments
 (0)