|
| 1 | +#include <ArduinoJson.h> // optional |
| 2 | +//#include <cJSON.h> // implicit with esp32, otherwise optional |
| 3 | +#include <YAMLDuino.h> |
| 4 | + |
| 5 | + |
| 6 | +// sorry about the notation, but it looks nicer than chunk-splitting+quoting |
| 7 | +const char* yaml_example_str = R"_YAML_STRING_( |
| 8 | +first: "true" |
| 9 | +blah: |
| 10 | + just_a_string: "I am a string" |
| 11 | + array_of_strings: |
| 12 | +# some unindented comments |
| 13 | + - oops |
| 14 | + # more indented comments |
| 15 | + - meh |
| 16 | + same_array_of_strings: [ oops, meh ] |
| 17 | + array_mixed: [1,2,3, "soleil!"] |
| 18 | + array_of_anonymous_objects: |
| 19 | + - prop1: wizz |
| 20 | + prop2: pop |
| 21 | + prop3: snap |
| 22 | + - prop1: foo |
| 23 | + prop3: bar |
| 24 | + prop2: baz |
| 25 | + prop4: wat |
| 26 | + integer: 12345 |
| 27 | + float: 12.3323 |
| 28 | + inline_json_for_the_haters: { "hello":"json", "nested":[3,2,"1","moon"] } |
| 29 | +whatever: |
| 30 | + nope: ["n","o","p","e"] |
| 31 | +last: "true" |
| 32 | +
|
| 33 | +)_YAML_STRING_"; |
| 34 | + |
| 35 | + |
| 36 | +const char* json_example_str = R"_JSON_STRING_( |
| 37 | +{ |
| 38 | + "first": "true", |
| 39 | + "blah": { |
| 40 | + "just_a_string": "I am a string", |
| 41 | + "array_of_strings": ["oops", "meh" ], |
| 42 | + "same_array_of_strings": [ "oops", "meh" ], |
| 43 | + "array_mixed": [ 1, 2, 3, "soleil!" ], |
| 44 | + "array_of_anonymous_objects": [ |
| 45 | + { |
| 46 | + "prop1": "wizz", |
| 47 | + "prop2": "pop", |
| 48 | + "prop3": "snap" |
| 49 | + }, |
| 50 | + { |
| 51 | + "prop1": "foo", |
| 52 | + "prop3": "bar", |
| 53 | + "prop2": "baz", |
| 54 | + "prop4": "wat" |
| 55 | + } |
| 56 | + ], |
| 57 | + "integer": 12345, |
| 58 | + "float": 12.3323, |
| 59 | + "inline_json_for_the_haters": { "hello": "json", "nested": [ 3, 2, "1", "moon" ] } |
| 60 | + }, |
| 61 | + "whatever": { "nope": [ "n", "o", "p", "e" ] }, |
| 62 | + "last": "true" |
| 63 | +} |
| 64 | +)_JSON_STRING_"; |
| 65 | + |
| 66 | + |
| 67 | +void setup() |
| 68 | +{ |
| 69 | + Serial.begin(115200); |
| 70 | + delay(5000); |
| 71 | + Serial.print("Welcome to the YAML Test sketch\nRam free: "); |
| 72 | + Serial.print( HEAP_AVAILABLE() ); |
| 73 | + Serial.println(" bytes"); |
| 74 | + |
| 75 | + const size_t yaml_str_size = strlen(yaml_example_str); |
| 76 | + const size_t json_str_size = strlen(json_example_str); |
| 77 | + int test_number = 1; |
| 78 | + |
| 79 | + Serial.printf("[DEBUG] YAML (in):\n%s\n\n", yaml_example_str); |
| 80 | + |
| 81 | + YAMLParser::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited) |
| 82 | + |
| 83 | + |
| 84 | + #if defined HAS_ARDUINOJSON |
| 85 | + #pragma message "Enabling ArduinoJson tests" |
| 86 | + |
| 87 | + YAML_LOG_n("YAML=>JSON and JSON=>YAML using ArduinoJson\n\n"); |
| 88 | + |
| 89 | + #if !defined ARDUINO_ARCH_SAMD // samd with 32kb ram can oom after this, so only enable for specific test |
| 90 | + { |
| 91 | + YAML_LOG_n( "[TEST #%d] YAML stream to JsonObject -> deserializeYml(json_doc, yaml_stream):", test_number++ ); |
| 92 | + DynamicJsonDocument json_doc(yaml_str_size*2); |
| 93 | + String yaml_str = String( yaml_example_str ); |
| 94 | + StringStream yaml_stream( yaml_str ); |
| 95 | + auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument |
| 96 | + if( err ) { |
| 97 | + YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 98 | + return; |
| 99 | + } |
| 100 | + const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject |
| 101 | + YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out); |
| 102 | + } |
| 103 | + #endif |
| 104 | + |
| 105 | + |
| 106 | + { |
| 107 | + YAML_LOG_n( "[TEST #%d] YAML string to JsonObject -> deserializeYml(json_doc, yaml_example_str):", test_number++ ); |
| 108 | + DynamicJsonDocument json_doc(yaml_str_size*2); |
| 109 | + auto err = deserializeYml( json_doc, yaml_example_str ); // deserialize yaml string to JsonDocument |
| 110 | + if( err ) { |
| 111 | + YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 112 | + return; |
| 113 | + } |
| 114 | + JsonObject json_obj = json_doc.as<JsonObject>(); |
| 115 | + const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject |
| 116 | + YAML_LOG_n("[YAML=>JsonObject] yaml bytes in=%d, json bytes out=%d\n\n", yaml_str_size, bytes_out); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + { |
| 124 | + // Convert JsonObject to yaml |
| 125 | + YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, yaml_stream_out):", test_number++ ); |
| 126 | + String str_yaml_out = ""; // YAML output string |
| 127 | + StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out |
| 128 | + // create and populate a JsonObject |
| 129 | + DynamicJsonDocument doc(json_str_size*2); |
| 130 | + auto err = deserializeJson( doc, json_example_str ); |
| 131 | + if( err ) { |
| 132 | + YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() ); |
| 133 | + return; |
| 134 | + } |
| 135 | + JsonObject json_obj = doc.as<JsonObject>(); |
| 136 | + const size_t bytes_out = serializeYml( json_obj, yaml_stream_out ); |
| 137 | + Serial.println( str_yaml_out ); |
| 138 | + YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out ); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + { |
| 143 | + // Convert JsonObject to yaml |
| 144 | + YAML_LOG_n( "[TEST #%d] JsonObject to YAML stream -> serializeYml(json_obj, str_yaml_out):", test_number++ ); |
| 145 | + String str_yaml_out = ""; // YAML output string |
| 146 | + //StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out |
| 147 | + // create and populate a JsonObject |
| 148 | + DynamicJsonDocument doc(json_str_size*2); |
| 149 | + auto err = deserializeJson( doc, json_example_str ); |
| 150 | + if( err ) { |
| 151 | + YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() ); |
| 152 | + return; |
| 153 | + } |
| 154 | + JsonObject json_obj = doc.as<JsonObject>(); |
| 155 | + const size_t bytes_out = serializeYml( json_obj, str_yaml_out ); |
| 156 | + Serial.println( str_yaml_out ); |
| 157 | + YAML_LOG_n("[JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n\n", json_str_size, bytes_out ); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + #if defined USE_STREAM_TO_STREAM // stream to stream unavailable on esp8266 (not enough memory) |
| 162 | + #pragma message "Enabling ArduinoJson stream<=>stream tests" |
| 163 | + { |
| 164 | + YAML_LOG_n( "[TEST #%d] JSON stream to JsonObject to YAML stream -> serializeYml(stream_in, Serial):", test_number++ ); |
| 165 | + String str_json = String( json_example_str ); |
| 166 | + StringStream stream_in( str_json ); |
| 167 | + const size_t bytes_out = serializeYml( stream_in, Serial ); |
| 168 | + YAML_LOG_n("[JSON=>JsonObject=>YAML] json bytes in=%d, yaml bytes out=%d\n", json_str_size, bytes_out); |
| 169 | + } |
| 170 | + #endif |
| 171 | + |
| 172 | + |
| 173 | + YAML_LOG_n("ArduinoJson tests complete"); |
| 174 | + |
| 175 | + #endif |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + #if __has_include(<cJSON.h>) |
| 181 | + #pragma message "Enabling cJSON tests" |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + YAML_LOG_n("\n\nYAML=>JSON and JSON=>YAML using cJSON:\n"); |
| 186 | + |
| 187 | + |
| 188 | + { |
| 189 | + YAML_LOG_n( "YAML string to cJSON Object -> deserializeYml(cJSON_obj*, yaml_example_str):" ); |
| 190 | + // deserialize YAML string into cJSON object |
| 191 | + cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object |
| 192 | + int ret = deserializeYml( objPtr, yaml_example_str ); |
| 193 | + if (!ret) { |
| 194 | + Serial.println("deserializeYml failed"); |
| 195 | + return; |
| 196 | + } |
| 197 | + YAML_LOG_n("Printing json"); |
| 198 | + char* json = cJSON_Print( objPtr ); |
| 199 | + if( !json ) { |
| 200 | + YAML_LOG_e("emtpy output, aborting"); |
| 201 | + return; |
| 202 | + } |
| 203 | + String str_json_out = String( json ); |
| 204 | + free(json); |
| 205 | + Serial.print( str_json_out ); |
| 206 | + cJSON_Delete( objPtr ); |
| 207 | + YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str_size, str_json_out.length() ); |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + { |
| 213 | + YAML_LOG_n( "YAML stream to cJSON Object -> deserializeYml(cJSON_obj*, yaml_stream):" ); |
| 214 | + String yaml_str = String( yaml_example_str ); |
| 215 | + StringStream yaml_stream( yaml_str ); |
| 216 | + cJSON* objPtr = (cJSON*)malloc( sizeof(cJSON) ); // allocate minimal memory to empty object |
| 217 | + // deserialize YAML stream into cJSON object |
| 218 | + int ret = deserializeYml( objPtr, yaml_stream ); |
| 219 | + if (!ret) { |
| 220 | + Serial.println("deserializeYml failed"); |
| 221 | + return; |
| 222 | + } |
| 223 | + YAML_LOG_n("Printing json"); |
| 224 | + char* json = cJSON_Print( objPtr ); |
| 225 | + if( !json ) { |
| 226 | + YAML_LOG_e("emtpy output, aborting"); |
| 227 | + return; |
| 228 | + } |
| 229 | + size_t bytes_out = strlen(json); |
| 230 | + Serial.print( json ); |
| 231 | + free(json); |
| 232 | + cJSON_Delete( objPtr ); |
| 233 | + YAML_LOG_n("[YAML=>cJsonObject] yaml bytes in=%d, json bytes out=%d\n", yaml_str.length(), bytes_out ); |
| 234 | + } |
| 235 | + |
| 236 | + |
| 237 | + { |
| 238 | + YAML_LOG_n( "cJSON Object to YAML stream -> serializeYml( objPtr, Serial ):" ); |
| 239 | + cJSON* objPtr = cJSON_Parse( json_example_str ); |
| 240 | + size_t bytes_out = serializeYml( objPtr, Serial ); |
| 241 | + cJSON_Delete( objPtr ); |
| 242 | + YAML_LOG_n("[YAML=>cJsonObject=>YAML] yaml bytes in=%d, json bytes out=%d\n", json_str_size, bytes_out); |
| 243 | + } |
| 244 | + |
| 245 | + |
| 246 | + { |
| 247 | + YAML_LOG_n( "cJSON Object to YAML string -> serializeYml( objPtr, yaml_dest_str ):" ); |
| 248 | + cJSON* objPtr = cJSON_Parse( json_example_str ); |
| 249 | + String yaml_dest_str; |
| 250 | + size_t bytes_out = serializeYml( objPtr, yaml_dest_str ); |
| 251 | + Serial.println( yaml_dest_str ); |
| 252 | + cJSON_Delete( objPtr ); |
| 253 | + YAML_LOG_n("[YAML=>cJsonObject=>YAML] yaml bytes in=%d, json bytes out=%d\n", json_str_size, bytes_out ); |
| 254 | + } |
| 255 | + |
| 256 | + |
| 257 | + YAML_LOG_n("cJSON tests complete"); |
| 258 | + |
| 259 | + |
| 260 | + #endif |
| 261 | + |
| 262 | + |
| 263 | +} |
| 264 | + |
| 265 | + |
| 266 | +void loop() |
| 267 | +{ |
| 268 | + |
| 269 | +} |
0 commit comments