String copy policy behavior?
Created by: samvik
Describe the issue
First of all thanks for a grate library.
We are finding that it is a bit unintuitive and error prone when it comes to string handling. More specifically where const char* is not copied but char* is. Since we try to keep as much as possible const and this has nothing to do with lifetime we often end up having to const_cast pointers in order to force arduinojson to copy the string (easy to forget and leads to strange runtime errors). We have mitigated the issue a bit by implementing custom converters for our string classes (mainly etl::string and etl::string_view), but this has it's limitations see question 2 below.
So to my questions.
1. Any way to disable zerocopy entirely for a single document or for the entire library? Or is it possible to use another setter that will always copy?
2. The custom converters only seems to work on the value and not the keys, is this by design? See example below.
etl::string_view str = ...;
doc[str] = 1234; // Does not compile
doc["name"] = str; // Compiles and work
3. Is there a better way of handling/controlling the behavior? In other words have we misunderstood how to use arduinojson efficiently?
Thanks /Björn
Troubleshooter report
- The issue happens at run time
- The issue concerns serialization
- Output contains garbage
-
serializeJson()
produces garbage - Program doesn't call
deserializeJson()
- Program calls
serializeJson(const JsonDocument&, ...)
- Program uses
DynamicJsonDocument
- Program doesn't use
String
Environment
Here is the environment that I'm using':
- NXP iMX RT1061
- Core/runtime: FreeRTOS
- IDE: MCU Xpresso