Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #1843
Closed
Open
Issue created Dec 28, 2022 by Jeroen88@Jeroen88

Overwrite string values (non-const char *) of the same length instead of allocating them into the Json doc?

Is your feature request related to a problem? Please describe.
I use a DynamicJsonDocument to frequently store c strings (not const) and these strings are always of the same length. If I use doc["key"] = value; repeatedly, the document is overflowed after a while (I am aware of the information in "I found a memory leak in the library!")

Describe the solution you'd like
Would it be possible to overwrite c strings of the same length (or even strings that are shorter) at the same key instead of allocating them into the Json document?

Describe alternatives you've considered
If I do this:

JsonVariant test = doc["key"];
// Make sure the object at 'key' is already present and it's value is equal or longer than the new value
if(!test.isNull() && strlen(test.as<const char *>() >= the lengh of the new value) {  
  const char * ptr = test.as<const char *>();
  strcpy((char *) ptr, the addess of the new value, the length of the new value);
} else {
  // Do a regular insertion into the Json document
  doc["key"] = the new value;
}

This perfectly works (my c strings are dynamically created and get out of scope, so I can not use the 'const char *' feature: they need to be copied into the Json document).

Additional context
Although I am not sure, a #define like is used here might optionally add such a feature? But maybe it is impossible to add this to the monotonic allocator?

Assignee
Assign to
Time tracking