@@ -36,22 +36,27 @@ extern "C" {
3636 #include " libyaml/yaml.h" // https://github.com/yaml/libyaml
3737}
3838
39-
40- #define USE_STREAM_TO_STREAM
41-
4239#if defined ARDUINO_ARCH_SAMD || defined ARDUINO_ARCH_RP2040 || defined ESP8266
40+ // __has_include() macro only sees inside the sketch folder, so assume ArduinoJson as a default dependancy
4341 #include < Arduino.h>
44- #include < ArduinoJson.h>
4542 #include < assert.h>
46- #undef USE_STREAM_TO_STREAM
43+ #include < ArduinoJson.h>
44+ // also disable the "unstable" stream-to-stream function
4745 #define HAS_ARDUINOJSON
4846#endif
4947
5048#if !defined HAS_ARDUINOJSON && __has_include(<Arduino.h>)
49+ // esp32 __has_include() macro works outside the sketch folder, so it's possible to guess
5150 #define HAS_ARDUINOJSON
5251#endif
5352
53+ #if defined ESP32
54+ // platform specific feature is unstable but recoverable with ESP32 devices family
55+ #define USE_STREAM_TO_STREAM
56+ #endif
57+
5458
59+ // provide a default String::Stream reader/writer for internals
5560class StringStream : public Stream
5661{
5762public:
@@ -67,7 +72,7 @@ class StringStream : public Stream
6772};
6873
6974
70-
75+ // the base class
7176class YAMLParser
7277{
7378public:
@@ -100,6 +105,8 @@ class YAMLParser
100105
101106#if defined HAS_ARDUINOJSON
102107
108+ // ArduinoJson friendly functions and derivated class
109+
103110 #include < ArduinoJson.h>
104111
105112 // deconstructors
@@ -184,6 +191,8 @@ class YAMLParser
184191
185192#if __has_include(<cJSON.h>)
186193
194+ // cJSON friendly functions and derivated class
195+
187196 #include < cJSON.h> // built-in with esp32
188197
189198 // deconstructors
@@ -232,5 +241,18 @@ class YAMLParser
232241
233242#endif
234243
235- // this macro does not like to be defined early (especially before ArduinoJson.h is included)
236- #define indent (indent_size ) std::string(indent_size*2 , ' ' ).c_str()
244+ #if defined ARDUINO_ARCH_SAMD
245+ // using slow copy instead of a macro, because std::string is incomplete with samd core
246+ static String _indent_str;
247+ static const char * indent ( size_t size )
248+ {
249+ _indent_str = " " ;
250+ for ( size_t i=0 ;i<size;i++ ) _indent_str += " " ;
251+ return _indent_str.c_str ();
252+ }
253+ #else
254+ // this macro does not like to be defined early (especially before ArduinoJson.h is included)
255+ #define indent (indent_size ) (std::string(indent_size*2 , ' ' )).c_str()
256+ #endif
257+
258+
0 commit comments