The macro ARDUINOJSON_ENABLE_STD_STRING controls the support of the type std::string in the library.

If ARDUINOJSON_ENABLE_STD_STRING is defined 1, then std::string is supported.

Default value

On C++17-capable compilers, ArduinoJson can detect the presence of the <string> header. In that case, the default value of ARDUINOJSON_ENABLE_STD_STRING is:

  • 1 if <string> is available,
  • 0 otherwise.

On older compilers, ArduinoJson tries to guess whether std::string is available or not, based on the presence of the ARDUINO macro. In that case, the default value of ARDUINOJSON_ENABLE_STD_STRING is:

  • 0 if ARDUINO is defined,
  • 1 otherwise.

In other words, ArduinoJson assumes that std::string is available as soon as you work outside of Arduino.

Examples

If you need to force the support of std::string, add the following line at the top of your program:

#define ARDUINOJSON_ENABLE_STD_STRING 1
#include <ArduinoJson.h>

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

#define ARDUINOJSON_ENABLE_STD_STRING 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 class?

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

  1. As your JSON input:

     // WARNING: ArduinoJson duplicates the std::string in the JsonDocument
     std::string input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
     deserializeJson(doc, input);
    
  2. As your JSON output:

     std::string output;
     serializeJson(doc, output);
    
  3. As a key to extract a value from a JsonDocument:

     long time = doc[std::string("time")];
    
  4. As a new key in JsonDocument:

     // WARNING: ArduinoJson duplicates the std::string in the JsonDocument
     doc[std::string("time")] = time;
    
  5. To extract a value from a JsonVariant:

     std::string sensor = doc["sensor"];
    
  6. To set the value of a JsonVariant:

     // WARNING: ArduinoJson duplicates the std::string in the JsonDocument
     doc["sensor"] = sensor;
    
  7. You can also concatenate strings

     // WARNING: ArduinoJson duplicates the std::strings in the JsonDocument
     doc[std::string("sen") + "sor"] = std::string("gp") + "s";
    
  8. You can compare the content of a JsonVariant with a std::string

     if (doc["sensor"] == sensor) {
         // ...
     }
    

The one place where it doesn’t work ⚠️

Unfortunately, the following doesn’t work:

std::string sensor(doc["sensor"]); // <-  error: call of overloaded 'basic_string(...)' is ambiguous

This line is ambiguous because the compiler cannot tell which constructor of std::string to call. Is it the one taking a const char*, an int, the copy constructor, or the move constructor?

In C++11, we get a similar problem with the brace-initializer (issue #1498):

std::string sensor{doc["sensor"]}; // <-  error: no matching function for call to 'variantAs(...)'

To solve this ambiguity, the simplest is to use the assignment operator:

std::string sensor = doc["sensor"];
// or
auto sensor = doc["sensor"].as<std::string>();

Alternatively, you can explicitly case the JsonVariant to a const char*:

std::string sensor(doc["sensor"].as<const char*>());

See also

 X online