The macro ARDUINOJSON_ENABLE_STRING_VIEW controls the support of the type std::string_view in the library.

If ARDUINOJSON_ENABLE_STRING_VIEW is defined to 1, then std::string_view is supported.

This features was added in ArduinoJson 6.18.1.

Default value

ArduinoJson can detect the presence of the <string_view> header. The default value of ARDUINOJSON_ENABLE_STRING_VIEW is:

Examples

If for some reason, you need to force the support of std::string_view, add the following line at the top of your program:

#define ARDUINOJSON_ENABLE_STRING_VIEW 1
#include <ArduinoJson.h>

If you need to disable the support for std::string_view, add the following line at the top of your program:

#define ARDUINOJSON_ENABLE_STRING_VIEW 0
#include <ArduinoJson.h>

Only 0 and 1 are valid. Any other value (like false or true) will produce a compilation error.

Where can I use the std::string_view class?

Once enabled, you can use a std::string_view in many places.

  1. As your JSON input:

     // WARNING: ArduinoJson duplicates the std::string in the JsonDocument
     deserializeJson(doc, std::string_view("{\"sensor\":\"gps\"}", 16));
    
  2. As a value:

     doc["sensor"] = std::string_view("gps", 3);
    
  3. As a key to extract a member from a JsonDocument:

     const char* sensor = doc[std::string_view("sensor", 6)];
    
  4. As a key to insert a member in a JsonDocument:

     doc[std::string_view("time", 4)] = time;
    
  5. You can compare the content of a JsonVariant with a std::string_view

     if (doc["sensor"] == std::string_view("gps", 3)) {
         // ...
     }
    

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)