Skip to content

Commit 34d4ed0

Browse files
committed
updated examples
1 parent c9f85c1 commit 34d4ed0

File tree

1 file changed

+129
-93
lines changed

1 file changed

+129
-93
lines changed

examples/test/src/test.cpp

Lines changed: 129 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
#include <ArduinoJson.h> // optional
2-
//#include <cJSON.h> // implicit with esp32, otherwise optional
1+
#include <ArduinoJson.h>
2+
3+
// #define YAML_DISABLE_ARDUINOJSON
4+
// #define YAML_DISABLE_CJSON
5+
36
#include <YAMLDuino.h>
47

58

@@ -38,6 +41,7 @@ fourth: false
3841
prop2: baz
3942
prop4: wat
4043
integer: 12345
44+
# this float value gives a memleak to cJSON
4145
float: 12.3323
4246
inline_json_for_the_haters: { "hello":"json", "nested":[3,2,"1","moon"] }
4347
whatever:
@@ -78,6 +82,7 @@ const char* json_example_str = R"_JSON_STRING_(
7882
"whatever": { "nope": [ "n", "o", "p", "e" ] },
7983
"last": true
8084
}
85+
8186
)_JSON_STRING_";
8287

8388

@@ -87,68 +92,116 @@ int test_number = 1;
8792

8893

8994

90-
#if defined HAS_ARDUINOJSON
95+
void test_Yaml_String_Parser()
96+
{
97+
YAML_LOG_n( "[TEST #%d] parse JSON String to YAML Stream:", test_number++ );
98+
YAMLParser* parser = new YAMLParser();
99+
Stream* output_stream = &Serial;
100+
parser->setOutputStream( output_stream );
101+
parser->parse( json_example_str );
102+
delete parser;
103+
YAML_LOG_n("Tests complete");
104+
}
105+
106+
107+
void test_Yaml_Stream_Parser()
108+
{
109+
YAML_LOG_n( "[TEST #%d] parse JSON Stream to YAML Stream:", test_number++ );
110+
YAMLParser* parser = new YAMLParser();
111+
Stream* output_stream = &Serial;
112+
parser->setOutputStream( output_stream );
113+
String json_in = String( json_example_str );
114+
StringStream input_stream( json_in );
115+
parser->parse( input_stream );
116+
delete parser;
117+
YAML_LOG_n("Tests complete");
118+
}
119+
120+
121+
void test_Yaml_String_Loader()
122+
{
123+
YAML_LOG_n( "[TEST #%d] load JSON String and parse to YAML Stream:", test_number++ );
124+
YAMLParser* parser = new YAMLParser();
125+
Stream* output_stream = &Serial;
126+
parser->setOutputStream( output_stream );
127+
parser->load( json_example_str );
128+
// do something with parser->getDocument()
129+
parser->parse();
130+
delete parser;
131+
YAML_LOG_n("Tests complete");
132+
}
133+
134+
135+
void test_Yaml_Stream_Loader()
136+
{
137+
YAML_LOG_n( "[TEST #%d] load JSON Stream and parse to YAML Stream:", test_number++ );
138+
YAMLParser* parser = new YAMLParser();
139+
Stream* output_stream = &Serial;
140+
parser->setOutputStream( output_stream );
141+
String json_in = String( json_example_str );
142+
StringStream input_stream( json_in );
143+
parser->load( input_stream );
144+
// do something with parser->getDocument()
145+
parser->parse();
146+
delete parser;
147+
YAML_LOG_n("Tests complete");
148+
}
91149

92150

151+
#if defined HAS_ARDUINOJSON
152+
93153
void test_deserializeYml_JsonObject_YamlStream()
94154
{
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
155+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_obj, yaml_stream):", test_number++ );
156+
String yaml_str = String( yaml_example_str );
157+
StringStream yaml_stream( yaml_str );
158+
DynamicJsonDocument json_doc(2048);
159+
JsonObject json_obj = json_doc.as<JsonObject>();
160+
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
161+
if( err ) {
162+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
163+
return;
164+
}
165+
String str_json_out = ""; // JSON output string
166+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
167+
Serial.println( str_json_out );
168+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
112169
}
113170

114171

115172
void test_deserializeYml_JsonObject_YamlString()
116173
{
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
174+
YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_obj, yaml_example_str):", test_number++ );
175+
DynamicJsonDocument json_doc(2048);
176+
JsonObject json_obj = json_doc.as<JsonObject>();
177+
auto err = deserializeYml( json_obj, yaml_example_str ); // deserialize yaml string to JsonObject
178+
if( err ) {
179+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
180+
return;
181+
}
182+
String str_json_out = ""; // JSON output string
183+
const size_t bytes_out = serializeJsonPretty( json_obj, str_json_out ); // print deserialized JsonObject
184+
Serial.println( str_json_out );
185+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
131186
}
132187

133188

134189

135190
void test_deserializeYml_JsonDocument_YamlStream()
136191
{
137-
#if !defined ARDUINO_ARCH_SAMD // samd with 32kb ram can oom after this, so only enable for specific test
138-
YAML_LOG_n( "[TEST #%d] YAML stream to JsonDocument -> deserializeYml(json_doc, yaml_stream):", test_number++ );
139-
DynamicJsonDocument json_doc(2048);
140-
String yaml_str = String( yaml_example_str );
141-
StringStream yaml_stream( yaml_str );
142-
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
143-
if( err ) {
144-
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
145-
return;
146-
}
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 );
150-
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
151-
#endif
192+
YAML_LOG_n( "[TEST #%d] YAML stream to JsonDocument -> deserializeYml(json_doc, yaml_stream):", test_number++ );
193+
DynamicJsonDocument json_doc(2048);
194+
String yaml_str = String( yaml_example_str );
195+
StringStream yaml_stream( yaml_str );
196+
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
197+
if( err ) {
198+
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
199+
return;
200+
}
201+
String str_json_out = ""; // JSON output string
202+
const size_t bytes_out = serializeJsonPretty( json_doc, str_json_out ); // print deserialized JsonObject
203+
Serial.println( str_json_out );
204+
YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out);
152205
}
153206

154207

@@ -212,33 +265,17 @@ int test_number = 1;
212265

213266

214267

215-
#if defined USE_STREAM_TO_STREAM // stream to stream unavailable on low memory devices
216-
217-
void test_serializeYml_JsonStream_YamlStream()
218-
{
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);
227-
}
228-
229-
#endif
230-
231268

232269

233270
#if defined HAS_CJSON
234271

272+
235273
void test_deserializeYml_cJson_String()
236274
{
237-
YAML_LOG_n( "YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):" );
238-
// deserialize YAML string into cJSON object
239-
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
240-
int ret = deserializeYml( objPtr, yaml_example_str );
241-
if (!ret) {
275+
YAML_LOG_n( "[TEST #%d] YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):", test_number++ );
276+
cJSON* objPtr;
277+
int ret = deserializeYml( &objPtr, yaml_example_str ); // deserialize YAML string into cJSON object
278+
if (ret<0) {
242279
Serial.println("deserializeYml failed");
243280
return;
244281
}
@@ -248,23 +285,22 @@ int test_number = 1;
248285
YAML_LOG_e("emtpy output, aborting");
249286
return;
250287
}
251-
String str_json_out = String( json );
252-
free(json);
253-
Serial.print( str_json_out );
288+
size_t bytes_out = strlen( json );
289+
Serial.println( json );
290+
cJSON_free( json );
254291
cJSON_Delete( objPtr );
255-
YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str_size, str_json_out.length() );
292+
YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str_size, bytes_out );
256293
}
257294

258295

259296
void test_deserializeYml_cJson_Stream()
260297
{
261-
YAML_LOG_n( "YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):" );
298+
YAML_LOG_n( "[TEST #%d] YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):", test_number++ );
262299
String yaml_str = String( yaml_example_str );
263300
StringStream yaml_stream( yaml_str );
264-
cJSON* objPtr = cJSON_Parse("{}"); // (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object
265-
// deserialize YAML stream into cJSON object
266-
int ret = deserializeYml( objPtr, yaml_stream );
267-
if (!ret) {
301+
cJSON* objPtr;
302+
int ret = deserializeYml( &objPtr, yaml_stream ); // deserialize YAML stream into cJSON object
303+
if (ret<0) {
268304
Serial.println("deserializeYml failed");
269305
return;
270306
}
@@ -274,17 +310,17 @@ int test_number = 1;
274310
YAML_LOG_e("emtpy output, aborting");
275311
return;
276312
}
277-
size_t bytes_out = strlen(json);
278-
Serial.print( json );
279-
free(json);
313+
size_t bytes_out = strlen( json );
314+
Serial.println( json );
315+
cJSON_free( json );
280316
cJSON_Delete( objPtr );
281317
YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str.length(), bytes_out );
282318
}
283319

284320

285321
void test_serializeYml_cJson_Stream()
286322
{
287-
YAML_LOG_n( "cJSON Object to YAML stream -> serializeYml( objPtr, Serial ):" );
323+
YAML_LOG_n( "[TEST #%d] cJSON Object to YAML stream -> serializeYml( objPtr, Serial ):", test_number++ );
288324
cJSON* objPtr = cJSON_Parse( json_example_str );
289325
size_t bytes_out = serializeYml( objPtr, Serial );
290326
cJSON_Delete( objPtr );
@@ -294,7 +330,7 @@ int test_number = 1;
294330

295331
void test_serializeYml_cJson_String()
296332
{
297-
YAML_LOG_n( "cJSON Object to YAML string -> serializeYml( objPtr, yaml_dest_str ):" );
333+
YAML_LOG_n( "[TEST #%d] cJSON Object to YAML string -> serializeYml( objPtr, yaml_dest_str ):", test_number++ );
298334
cJSON* objPtr = cJSON_Parse( json_example_str );
299335
String yaml_dest_str;
300336
size_t bytes_out = serializeYml( objPtr, yaml_dest_str );
@@ -316,22 +352,22 @@ void setup()
316352
Serial.print( HEAP_AVAILABLE() );
317353
Serial.println(" bytes");
318354

319-
Serial.printf("[DEBUG] YAML (in):\n%s\n\n", yaml_example_str);
320-
321355
YAMLParser::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited)
322356

357+
YAML_LOG_n("\n\nJSON=>YAML using libyaml:\n");
323358

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
359+
test_Yaml_String_Parser();
360+
test_Yaml_Stream_Parser();
361+
test_Yaml_String_Loader();
362+
test_Yaml_Stream_Loader();
328363

364+
YAML_LOG_n("YAMLParser libyaml tests complete");
329365

330366
#if defined HAS_ARDUINOJSON
331367
#pragma message "Enabling ArduinoJson tests"
332368
YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n");
333-
test_deserializeYml_JsonObject_YamlStream();
334369
test_deserializeYml_JsonObject_YamlString();
370+
test_deserializeYml_JsonObject_YamlStream();
335371
test_deserializeYml_JsonDocument_YamlStream();
336372
test_deserializeYml_JsonDocument_YamlString();
337373
test_serializeYml_JsonObject_YamlStream();
@@ -343,10 +379,10 @@ void setup()
343379
#if defined HAS_CJSON
344380
#pragma message "Enabling cJSON tests"
345381
YAML_LOG_n("\n\nYAML=>JSON and JSON=>YAML using cJSON:\n");
346-
test_deserializeYml_cJson_String();
347-
test_deserializeYml_cJson_Stream();
348382
test_serializeYml_cJson_Stream();
349383
test_serializeYml_cJson_String();
384+
test_deserializeYml_cJson_Stream();
385+
test_deserializeYml_cJson_String();
350386
YAML_LOG_n("cJSON tests complete");
351387
#endif
352388
}

0 commit comments

Comments
 (0)