Skip to content

Commit b07da3a

Browse files
committed
fixes mem leaks + implements multiline
1 parent e20e98a commit b07da3a

File tree

8 files changed

+244
-110
lines changed

8 files changed

+244
-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: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ 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
1115
just_a_string: "I am a string"
1216
array_of_strings:
1317
# some unindented comments
@@ -39,6 +43,7 @@ const char* json_example_str = R"_JSON_STRING_(
3943
"first": true,
4044
"fourth": false,
4145
"blah": {
46+
"multiline_string": "omg I'm multiline!\nwhelp \"I'm quoted\" ? ! () { } \"\\t\\r\\n\"\nslash\\ed",
4247
"just_a_string": "I am a string",
4348
"array_of_strings": ["oops", "meh" ],
4449
"same_array_of_strings": [ "oops", "meh" ],
@@ -74,36 +79,83 @@ int test_number = 1;
7479

7580
#if defined HAS_ARDUINOJSON
7681

82+
83+
void test_deserializeYml_JsonObject_YamlStream()
84+
{
85+
#if !defined ARDUINO_ARCH_RP2040
86+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_obj, yaml_stream):", test_number++ );
87+
String yaml_str = String( yaml_example_str );
88+
StringStream yaml_stream( yaml_str );
89+
DynamicJsonDocument json_doc(2048);
90+
JsonObject json_obj = json_doc.as<JsonObject>();
91+
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
92+
if( err ) {
93+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
94+
return;
95+
}
96+
String str_json_out = ""; // JSON output string
97+
//StringStream json_stream_out( str_json_out ); // Stream to str_yaml_out
98+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
99+
Serial.println( str_json_out );
100+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
101+
#endif
102+
}
103+
104+
105+
void test_deserializeYml_JsonObject_YamlString()
106+
{
107+
#if !defined ARDUINO_ARCH_RP2040
108+
YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_obj, yaml_example_str):", test_number++ );
109+
DynamicJsonDocument json_doc(2048);
110+
JsonObject json_obj = json_doc.as<JsonObject>();
111+
auto err = deserializeYml( json_obj, yaml_example_str ); // deserialize yaml string to JsonObject
112+
if( err ) {
113+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
114+
return;
115+
}
116+
String str_json_out = ""; // JSON output string
117+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
118+
Serial.println( str_json_out );
119+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
120+
#endif
121+
}
122+
123+
124+
77125
void test_deserializeYml_JsonDocument_YamlStream()
78126
{
79127
#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);
128+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonDocument -> deserializeYml(json_doc, yaml_stream):", test_number++ );
129+
DynamicJsonDocument json_doc(2048);
82130
String yaml_str = String( yaml_example_str );
83131
StringStream yaml_stream( yaml_str );
84132
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
85133
if( err ) {
86134
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
87135
return;
88136
}
89-
const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject
137+
String str_json_out = ""; // JSON output string
138+
const size_t bytes_out = serializeJsonPretty( json_doc, str_json_out ); // print deserialized JsonObject
139+
Serial.println( str_json_out );
90140
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
91141
#endif
92142
}
93143

94144

95145
void test_deserializeYml_JsonDocument_YamlString()
96146
{
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);
147+
YAML_LOG_n( "[TEST #%d] YAML string to JsonDocument -> deserializeYml(json_doc, yaml_example_str):", test_number++ );
99148
String yaml_str( yaml_example_str );
149+
DynamicJsonDocument json_doc(2048);
100150
auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument
101151
if( err ) {
102152
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
103153
return;
104154
}
105155
JsonObject json_obj = json_doc.as<JsonObject>();
106-
const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject
156+
String str_json_out = ""; // JSON output string
157+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
158+
Serial.println( str_json_out );
107159
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
108160
}
109161

@@ -115,8 +167,7 @@ int test_number = 1;
115167
String str_yaml_out = ""; // YAML output string
116168
String json_str = String( json_example_str );
117169
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);
170+
DynamicJsonDocument doc(2048); // create and populate a JsonObject
120171
auto err = deserializeJson( doc, json_str.c_str() );
121172
if( err ) {
122173
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
@@ -135,9 +186,7 @@ int test_number = 1;
135186
YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, str_yaml_out):", test_number++ );
136187
String str_yaml_out = ""; // YAML output string
137188
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);
189+
DynamicJsonDocument doc(2048); // create and populate a JsonObject
141190
auto err = deserializeJson( doc, json_str.c_str() );
142191
if( err ) {
143192
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
@@ -157,17 +206,14 @@ int test_number = 1;
157206

158207
void test_serializeYml_JsonStream_YamlStream()
159208
{
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-
}
209+
YAML_LOG_n( "[TEST #%d] JSON stream to %s to YAML stream -> serializeYml(stream_in, stream_out):", test_number++, STREAM_TO_STREAM );
210+
String str_yaml = "";
211+
String str_json = String( json_example_str );
212+
StringStream stream_in( str_json );
213+
StringStream stream_out( str_yaml );
214+
const size_t bytes_out = serializeYml( stream_in, stream_out );
215+
Serial.println( str_yaml );
216+
YAML_LOG_n("[JSON=>JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n", json_str_size, bytes_out);
171217
}
172218

173219
#endif
@@ -180,7 +226,7 @@ int test_number = 1;
180226
{
181227
YAML_LOG_n( "YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):" );
182228
// deserialize YAML string into cJSON object
183-
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
229+
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
184230
int ret = deserializeYml( objPtr, yaml_example_str );
185231
if (!ret) {
186232
Serial.println("deserializeYml failed");
@@ -205,7 +251,7 @@ int test_number = 1;
205251
YAML_LOG_n( "YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):" );
206252
String yaml_str = String( yaml_example_str );
207253
StringStream yaml_stream( yaml_str );
208-
cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
254+
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
209255
// deserialize YAML stream into cJSON object
210256
int ret = deserializeYml( objPtr, yaml_stream );
211257
if (!ret) {
@@ -264,22 +310,25 @@ void setup()
264310

265311
YAMLParser::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited)
266312

313+
314+
#if defined USE_STREAM_TO_STREAM // json->yaml stream->stream using intermediate (cJSON or ArduinoJSON) object
315+
#pragma message "Enabling ArduinoJson stream<=>stream tests"
316+
test_serializeYml_JsonStream_YamlStream();
317+
#endif
318+
319+
267320
#if defined HAS_ARDUINOJSON
268321
#pragma message "Enabling ArduinoJson tests"
269322
YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n");
270-
//test_deserializeYml_JsonObject_YamlStream();
271-
//test_deserializeYml_JsonObject_YamlString();
323+
test_deserializeYml_JsonObject_YamlStream();
324+
test_deserializeYml_JsonObject_YamlString();
272325
test_deserializeYml_JsonDocument_YamlStream();
273326
test_deserializeYml_JsonDocument_YamlString();
274327
test_serializeYml_JsonObject_YamlStream();
275328
test_serializeYml_JsonObject_YamlString();
276329
YAML_LOG_n("ArduinoJson tests complete");
277330
#endif
278331

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

284333
#if defined HAS_CJSON
285334
#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)