Skip to content

String copy policy behavior? #1807

@samvik

Description

@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

  1. The issue happens at run time
  2. The issue concerns serialization
  3. Output contains garbage
  4. serializeJson() produces garbage
  5. Program doesn't call deserializeJson()
  6. Program calls serializeJson(const JsonDocument&, ...)
  7. Program uses DynamicJsonDocument
  8. Program doesn't use String

Environment
Here is the environment that I'm using':

  • NXP iMX RT1061
  • Core/runtime: FreeRTOS
  • IDE: MCU Xpresso

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions