Skip to content

Commit

Permalink
Migrated to ArduinoJSON 7.x, fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Jun 16, 2024
1 parent 88e300b commit 0dec79b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 134 deletions.
50 changes: 37 additions & 13 deletions examples/ReadWriteConfigFile/ReadWriteConfigFile.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#include <ArduinoJson.h>
#include <YAMLDuino.h>
#include <M5Stack.h>

#if defined ARDUINO_M5Stack_Core_ESP32
#include <M5Stack.h>
#define FS_t fs::FS
#define File_t fs::File
#define delay_fn vTaskDelay
#elif defined CORE_TEENSY
#include <FS.h>
#include <SD.h>
#define FS_t FS
#define File_t File
#define delay_fn delay
#else
#error "please implement"
#endif


const char* yaml_example_str = R"_YAML_STRING_(
Expand Down Expand Up @@ -31,9 +45,9 @@ DynamicJsonDocument json_doc(2048);
JsonObject myConfig; // json accessor


bool writeTestYaml( fs::FS &fs, const char* path )
bool writeTestYaml( FS_t &fs, const char* path )
{
fs::File file = fs.open( path, FILE_WRITE );
File_t file = fs.open( path, FILE_WRITE );
if( !file ) {
Serial.println("Can't open file for writing");
return false;
Expand All @@ -47,7 +61,7 @@ bool writeTestYaml( fs::FS &fs, const char* path )

bool loadYamlConfig()
{
fs::File file = SD.open( config_file );
File_t file = SD.open( config_file );
if( !file ) {
Serial.println("Can't open test file for writing :-(");
return false;
Expand All @@ -68,7 +82,7 @@ bool loadYamlConfig()

bool saveYamlConfig()
{
fs::File file = SD.open( config_file, FILE_WRITE);
File_t file = SD.open( config_file, FILE_WRITE);
if( !file ) {
Serial.println("Can't open file for writing");
return false;
Expand All @@ -93,14 +107,22 @@ bool toggleYamlProperty()

void setup()
{
M5.begin();
#if defined ARDUINO_M5Stack_Core_ESP32
M5.begin();
if( M5.BtnA.isPressed() ) {
SD.remove( config_file );
Serial.println("Deleted config file");
while( M5.BtnA.isPressed() ) { M5.update(); } // wait for release
ESP.restart();
}
#elif defined CORE_TEENSY
Serial.begin(115200);
SD.begin( BUILTIN_SDCARD );
#else
#error "please implement"
#endif


if( M5.BtnA.isPressed() ) {
SD.remove( config_file );
Serial.println("Deleted config file");
while( M5.BtnA.isPressed() ) { M5.update(); } // wait for release
ESP.restart();
}

_load_config:
config_loaded = loadYamlConfig();
Expand All @@ -109,7 +131,7 @@ void setup()
Serial.printf("Ceating config file %s\n", config_file );
if( !writeTestYaml( SD, config_file ) ) {
Serial.println("Could not create config file, aborting");
while(1) vTaskDelay(1);
while(1) delay_fn(1);
}
// write succeeded, reload config
goto _load_config;
Expand All @@ -122,12 +144,14 @@ void setup()

void loop()
{
#if defined ARDUINO_M5Stack_Core_ESP32
M5.update();

if( M5.BtnB.wasPressed() ) {
if( !toggleYamlProperty() ) {
Serial.println("Failed to save property");
}
}
#endif
}

16 changes: 12 additions & 4 deletions examples/deserializeYml/deserializeYml.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
#include <ArduinoJson.h>
#include <YAMLDuino.h>

#if ARDUINOJSON_VERSION_MAJOR<7
#error "ArduinoJSON version is deprecated, please upgrade to 7.x"
#endif

const char* yaml_sample_str = R"_YAML_STRING_(
first: true
blah:
nope: ["n","o","p","e"]
integer: 12345
float: 12.3323
qinteger: "12345"
qfloat: "12.3323"
last: true
)_YAML_STRING_";
Expand All @@ -20,6 +26,8 @@ const char* json_sample_str = R"_JSON_STRING_(
"blah": { "nope": [ "n", "o", "p", "e" ] },
"integer": 12345,
"float": 12.3323,
"qinteger": "12345"
"qfloat": "12.3323"
"last": true
}
Expand All @@ -45,7 +53,7 @@ void test_deserializeYml_JsonObject_YamlStream()
#if defined TEST_YAML_Stream_To_ArduinoJsonObject
String yaml_str = String( yaml_sample_str );
StringStream yaml_stream( yaml_str );
StaticJsonDocument<128> json_doc;
JsonDocument json_doc;
JsonObject json_obj = json_doc.to<JsonObject>();
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
if( err ) {
Expand All @@ -60,7 +68,7 @@ void test_deserializeYml_JsonObject_YamlStream()
void test_deserializeYml_JsonObject_YamlString()
{
#if defined TEST_YAML_String_To_ArduinoJsonObject
StaticJsonDocument<128> json_doc;
JsonDocument json_doc;
JsonObject json_obj = json_doc.to<JsonObject>();
auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject
if( err ) {
Expand All @@ -75,7 +83,7 @@ void test_deserializeYml_JsonObject_YamlString()
void test_deserializeYml_JsonDocument_YamlStream()
{
#if defined TEST_YAML_Stream_To_ArduinoJsonDocument
StaticJsonDocument<128> json_doc;
JsonDocument json_doc;
String yaml_str = String( yaml_sample_str );
StringStream yaml_stream( yaml_str );
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
Expand All @@ -92,7 +100,7 @@ void test_deserializeYml_JsonDocument_YamlString()
{
#if defined TEST_YAML_String_To_ArduinoJsonDocument
String yaml_str( yaml_sample_str );
StaticJsonDocument<128> json_doc;
JsonDocument json_doc;
auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument
if( err ) {
Serial.printf("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
Expand Down
39 changes: 0 additions & 39 deletions examples/test/include/README

This file was deleted.

46 changes: 0 additions & 46 deletions examples/test/lib/README

This file was deleted.

26 changes: 17 additions & 9 deletions examples/test/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#include <ArduinoJson.h>

#if ARDUINOJSON_VERSION_MAJOR<7
#error "ArduinoJSON version is deprecated, please upgrade to 7.x"
#endif

//#define YAML_DISABLE_CJSON // not needed here
//#define YAML_DISABLE_ARDUINOJSON // not needed here

Expand Down Expand Up @@ -45,9 +49,11 @@ fourth: false
prop3: bar
prop2: baz
prop4: wat
integer: 12345
integer: 1234567890
quoted_integer: "1234567890"
# this float value gives a memleak to cJSON
float: 12.3323
quoted_float: "12.3323"
inline_json_for_the_haters: { "hello":"json", "nested":[3,2,"1","moon"] }
whatever:
nope: ["n","o","p","e"]
Expand Down Expand Up @@ -78,8 +84,10 @@ const char* json_sample_str = R"_JSON_STRING_(
"prop4": "wat"
}
],
"integer": 12345,
"integer": 1234567890,
"quoted_integer": "1234567890",
"float": 12.3323,
"quoted_float": "12.3323",
"inline_json_for_the_haters": { "hello": "json", "nested": [ 3, 2, "1", "moon" ] }
},
"whatever": { "nope": [ "n", "o", "p", "e" ] },
Expand Down Expand Up @@ -199,7 +207,7 @@ void test_Json_gettext_stream()
{
String yaml_str = String( yaml_sample_str );
StringStream yaml_stream( yaml_str );
DynamicJsonDocument json_doc(2048);
JsonDocument json_doc;
JsonObject json_obj = json_doc.to<JsonObject>();
auto err = deserializeYml( json_obj, yaml_stream ); // deserialize yaml stream to JsonObject
if( err ) {
Expand All @@ -215,7 +223,7 @@ void test_Json_gettext_stream()

void test_deserializeYml_JsonObject_YamlString()
{
DynamicJsonDocument json_doc(2048);
JsonDocument json_doc;
JsonObject json_obj = json_doc.to<JsonObject>();
auto err = deserializeYml( json_obj, yaml_sample_str ); // deserialize yaml string to JsonObject
if( err ) {
Expand All @@ -230,7 +238,7 @@ void test_Json_gettext_stream()

void test_deserializeYml_JsonDocument_YamlStream()
{
DynamicJsonDocument json_doc(2048);
JsonDocument json_doc;
String yaml_str = String( yaml_sample_str );
StringStream yaml_stream( yaml_str );
auto err = deserializeYml( json_doc, yaml_stream ); // deserialize yaml stream to JsonDocument
Expand All @@ -248,7 +256,7 @@ void test_Json_gettext_stream()
void test_deserializeYml_JsonDocument_YamlString()
{
String yaml_str( yaml_sample_str );
DynamicJsonDocument json_doc(2048);
JsonDocument json_doc;
auto err = deserializeYml( json_doc, yaml_str.c_str() ); // deserialize yaml string to JsonDocument
if( err ) {
YAML_LOG_n("Unable to deserialize demo YAML to JsonObject: %s", err.c_str() );
Expand All @@ -267,7 +275,7 @@ void test_Json_gettext_stream()
String str_yaml_out = ""; // YAML output string
String json_str = String( json_sample_str );
StringStream yaml_stream_out( str_yaml_out ); // Stream to str_yaml_out
DynamicJsonDocument doc(2048); // create and populate a JsonObject
JsonDocument doc; // create and populate a JsonObject
auto err = deserializeJson( doc, json_str.c_str() );
if( err ) {
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
Expand All @@ -285,7 +293,7 @@ void test_Json_gettext_stream()
// Convert JsonObject to yaml
String str_yaml_out = ""; // YAML output string
String json_str = String( json_sample_str );
DynamicJsonDocument doc(2048); // create and populate a JsonObject
JsonDocument doc; // create and populate a JsonObject
auto err = deserializeJson( doc, json_str.c_str() );
if( err ) {
YAML_LOG_n("Unable to deserialize demo JSON to JsonObject: %s", err.c_str() );
Expand Down Expand Up @@ -417,7 +425,7 @@ void setup()

test_fn( test_Yaml2JsonPretty, "serializeYml", "Yaml2JsonPretty", "serializeYml(yaml_document_t*, Stream&, OUTPUT_JSON_PRETTY)" );
test_fn( test_Yaml2Json, "serializeYml", "Yaml2Json", "serializeYml(yaml_document_t*, Stream&, OUTPUT_JSON)" );
test_fn( test_Json2Yaml, "serializeYml", "Json2Yam", "serializeYml(yaml_document_t*, Stream&, OUTPUT_YAML)" );
test_fn( test_Json2Yaml, "serializeYml", "Json2Yaml", "serializeYml(yaml_document_t*, Stream&, OUTPUT_YAML)" );


YAML_LOG_n("### YAMLParser libyaml tests complete\n");
Expand Down
11 changes: 0 additions & 11 deletions examples/test/test/README

This file was deleted.

Loading

0 comments on commit 0dec79b

Please sign in to comment.