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.
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.
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.
std::string
class?
Once enabled, you can use a std::string
in many places.
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);
As your JSON output:
std::string output;
serializeJson(doc, output);
As a key to extract a value from a JsonDocument
:
long time = doc[std::string("time")];
As a new key in JsonDocument
:
// WARNING: ArduinoJson duplicates the std::string in the JsonDocument
doc[std::string("time")] = time;
To extract a value from a JsonVariant
:
std::string sensor = doc["sensor"];
To set the value of a JsonVariant
:
// WARNING: ArduinoJson duplicates the std::string in the JsonDocument
doc["sensor"] = sensor;
You can also concatenate strings
// WARNING: ArduinoJson duplicates the std::strings in the JsonDocument
doc[std::string("sen") + "sor"] = std::string("gp") + "s";
You can compare the content of a JsonVariant
with a std::string
if (doc["sensor"] == sensor) {
// ...
}
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4