|
| 1 | +#pragma GCC diagnostic ignored "-Wunused-variable" |
| 2 | + |
| 3 | +#include <ArduinoJson.h> |
| 4 | +#include <YAMLDuino.h> |
| 5 | + |
| 6 | +const char* yaml_sample_str = R"_YAML_STRING_( |
| 7 | +first: true |
| 8 | +blah: |
| 9 | + nope: ["n","o","p","e"] |
| 10 | +integer: 12345 |
| 11 | +float: 12.3323 |
| 12 | +last: true |
| 13 | +
|
| 14 | +)_YAML_STRING_"; |
| 15 | + |
| 16 | +// exact JSON representation of yaml_sample_str |
| 17 | +const char* json_sample_str = R"_JSON_STRING_( |
| 18 | +{ |
| 19 | + "first": true, |
| 20 | + "blah": { "nope": [ "n", "o", "p", "e" ] }, |
| 21 | + "integer": 12345, |
| 22 | + "float": 12.3323, |
| 23 | + "last": true |
| 24 | +} |
| 25 | +
|
| 26 | +)_JSON_STRING_"; |
| 27 | + |
| 28 | + |
| 29 | +const size_t yaml_str_size = strlen(yaml_sample_str); |
| 30 | +const size_t json_str_size = strlen(json_sample_str); |
| 31 | +int test_number = 1; |
| 32 | + |
| 33 | + |
| 34 | +// uncomment/comment as needed |
| 35 | + |
| 36 | +#define TEST_YAML_Stream_To_ArduinoJsonObject |
| 37 | +#define TEST_YAML_String_To_ArduinoJsonObject |
| 38 | +#define TEST_YAML_Stream_To_ArduinoJsonDocument |
| 39 | +#define TEST_YAML_String_To_ArduinoJsonDocument |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +void test_deserializeYml_JsonObject_YamlStream() |
| 44 | +{ |
| 45 | + #if defined TEST_YAML_Stream_To_ArduinoJsonObject |
| 46 | + String yaml_str = String( yaml_sample_str ); |
| 47 | + StringStream yaml_stream( yaml_str ); |
| 48 | + StaticJsonDocument<128> json_doc; |
| 49 | + JsonObject json_obj = json_doc.to<JsonObject>(); |
| 50 | + auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject |
| 51 | + if( err ) { |
| 52 | + Serial.printf("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 53 | + return; |
| 54 | + } |
| 55 | + const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject |
| 56 | + #endif |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +void test_deserializeYml_JsonObject_YamlString() |
| 61 | +{ |
| 62 | + #if defined TEST_YAML_String_To_ArduinoJsonObject |
| 63 | + StaticJsonDocument<128> json_doc; |
| 64 | + JsonObject json_obj = json_doc.to<JsonObject>(); |
| 65 | + auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject |
| 66 | + if( err ) { |
| 67 | + Serial.printf("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 68 | + return; |
| 69 | + } |
| 70 | + const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject |
| 71 | + #endif |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +void test_deserializeYml_JsonDocument_YamlStream() |
| 76 | +{ |
| 77 | + #if defined TEST_YAML_Stream_To_ArduinoJsonDocument |
| 78 | + StaticJsonDocument<128> json_doc; |
| 79 | + String yaml_str = String( yaml_sample_str ); |
| 80 | + StringStream yaml_stream( yaml_str ); |
| 81 | + auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument |
| 82 | + if( err ) { |
| 83 | + Serial.printf("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 84 | + return; |
| 85 | + } |
| 86 | + const size_t bytes_out = serializeJsonPretty( json_doc, Serial ); // print deserialized JsonObject |
| 87 | + #endif |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +void test_deserializeYml_JsonDocument_YamlString() |
| 92 | +{ |
| 93 | + #if defined TEST_YAML_String_To_ArduinoJsonDocument |
| 94 | + String yaml_str( yaml_sample_str ); |
| 95 | + StaticJsonDocument<128> json_doc; |
| 96 | + auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument |
| 97 | + if( err ) { |
| 98 | + Serial.printf("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() ); |
| 99 | + return; |
| 100 | + } |
| 101 | + JsonObject json_obj = json_doc.as<JsonObject>(); |
| 102 | + const size_t bytes_out = serializeJsonPretty( json_obj, Serial ); // print deserialized JsonObject |
| 103 | + #endif |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +void setup() |
| 109 | +{ |
| 110 | + Serial.begin(115200); |
| 111 | + delay(5000); |
| 112 | + Serial.printf("Welcome to the YAML Test sketch\nRam free: %d bytes", HEAP_AVAILABLE() ); |
| 113 | + |
| 114 | + // YAML::setLogLevel( YAML::LogLevelDebug ); // override sketch debug level (otherwise inherited) |
| 115 | + // YAML::setJSONIndent(" ", 8 ); // JSON -> two spaces per indent level, unfold objets up to 8 nesting levels |
| 116 | + // YAML::setYAMLIndent( 3 ); // annoy your friends with 3 spaces indentation |
| 117 | + |
| 118 | + test_deserializeYml_JsonDocument_YamlStream(); |
| 119 | + test_deserializeYml_JsonDocument_YamlString(); |
| 120 | + test_deserializeYml_JsonObject_YamlString(); |
| 121 | + test_deserializeYml_JsonObject_YamlStream(); |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +void loop() |
| 127 | +{ |
| 128 | + |
| 129 | +} |
| 130 | + |
0 commit comments