ArduinoJson icon indicating copy to clipboard operation
ArduinoJson copied to clipboard

serializeJson() version 7.4.1 do not create element names in fully

Open sezaicelikdogan opened this issue 6 months ago • 10 comments

Starting from version 7.2.1 and above serializeJson(doc, Serial) do not create pair names correctly. Only the first letters of the value pairs are stored.

For example: doc["cat"] = "sensor"; doc["type"] = "rpm";

is serialized as {"c":"s","t":"r"}

below 7.2.1 this problem not occurs

sezaicelikdogan avatar May 22 '25 20:05 sezaicelikdogan

It works fine here: https://wandbox.org/permlink/fv1IVJA0QIB6bdN6 Please provide an MCVE.

bblanchon avatar May 23 '25 06:05 bblanchon

Platform : Arduino ide 2.3.6 OS : windows 11 ArduinoJson Lib : 7.4.1

Code : #include <ArduinoJson.h>

void setup() { Serial.begin(115200); while (!Serial) continue; }

void loop() { JsonDocument doc; doc["sensor"] = "gps"; doc["type"] = "rpm"; serializeJson(doc, Serial); delay(1000); }

Serial monitor output : {"s":"g","t":"r"}{"s":"g","t":"r"}...

sezaicelikdogan avatar May 23 '25 07:05 sezaicelikdogan

I had the same issue on the Particle platform. Got around it by following this thread on the Particle forums.

Try changing doc["cat"] to doc[String("cat")].

Note, I am also setting ARDUINOJSON_ENABLE_ARDUINO_STRING to 1.

atwalsh avatar May 23 '25 14:05 atwalsh

I successfully tested the program above with the following boards:

  1. Arduino Leonardo
  2. Arduino UNO R4 WiFi
  3. Adafruit ESP8266 Feather Huzzah
  4. A generic ESP32

My setup:

  • ArduinoJson 7.4.1
  • Arduino IDE 2.3.6
  • Windows 11
  • All cores updated to the latest version

@sezaicelikdogan, @atwalsh, please provide more information about your setup. Also, please try to uninstall and reinstall the library.

bblanchon avatar May 23 '25 17:05 bblanchon

Sorry I forgot to write : Board : Arduino Due (original)

I uninstall and reinstall the lib but still give the same error.

sezaicelikdogan avatar May 25 '25 14:05 sezaicelikdogan

I had the same issue on the Particle platform. Got around it by following this thread on the Particle forums.

Try changing doc["cat"] to doc[String("cat")].

Note, I am also setting ARDUINOJSON_ENABLE_ARDUINO_STRING to 1.

This solved the problem.

and also I wrote the same for the valu 👍

doc[String("cat")] = String("sensor")

sezaicelikdogan avatar May 25 '25 14:05 sezaicelikdogan

@atwalsh, using String is slower than using string literal, produces larger code, and increases heap fragmentation, so you should only use this as a temporary fix until we fully understand what's happening.

@sezaicelikdogan, I don't own an Arduino Due, and unfortunately, the Due has a dedicated core that isn't shared with any other board. Could you try on a different board?

bblanchon avatar May 25 '25 16:05 bblanchon

I also successfully tested on:

  1. Arduino MKR1000 WiFi
  2. Adafruit ItsyBitsy M4
  3. Teensy 3.1
  4. Nucleo-32
  5. Teensy 4.1
  6. Arduino Nano Every

bblanchon avatar May 26 '25 06:05 bblanchon

@atwalsh, using String is slower than using string literal, produces larger code, and increases heap fragmentation, so you should only use this as a temporary fix until we fully understand what's happening.

@sezaicelikdogan, I don't own an Arduino Due, and unfortunately, the Due has a dedicated core that isn't shared with any other board. Could you try on a different board?

Thanks for your kind info. I can try with other boards of course and I will soon inform the result, but unfortunately this project has to be done with Arduino Due and if problem can not be corrected than I have to continue with creating the json string manually. But thanks for your efforts.

sezaicelikdogan avatar May 26 '25 07:05 sezaicelikdogan

Starting from version 7.2.1 and above serializeJson(doc, Serial) do not create pair names correctly.

@sezaicelikdogan, do you mean that version 7.2.0 works fine? Can you double-check?

bblanchon avatar Jun 05 '25 07:06 bblanchon

Dear Benoit, thank you for a quick reply to issue 2183 - closed as a duplicate. Indeed same issue: using the simple Serialization example, only the first characters of strings are retained. For me this happens on a DUE. Same code works ok on an MKR 1010 WiFi.

You asked for the version. I re-tested this morning with various versions:

  • Version 7.2.1 works ok
  • Version 7.3.0 is where the issue starts
  • Version 7.4.1, and I suppose intermediates, show the same issue.

This coincides with what I remember from a few months ago, namely I stopped upgrading ArduinoJson and kept working with 7.2.1.

Thanks again, kind regards, Ben

BenTubbing avatar Jun 16 '25 07:06 BenTubbing

Thanks, Ben. I could not find any explanation by looking at the changes in 7.3.0, so I ordered a Due from the Arduino store. It should arrive shortly.

bblanchon avatar Jun 16 '25 08:06 bblanchon

I received the Due and ran git bisect. The problem begins with commit ed5f890d2800152f930f2b586e5546d0cc26480c. I'm still trying to figure out what's going on.

bblanchon avatar Jun 19 '25 08:06 bblanchon

I found the bug! The Arduino core for SAM3X defines a preprocessor macro that replaces the built-in C++ keyword true:

#define true 0x1
#define false 0x0

Since true is now a synonym for 1, it is not a boolean anymore but an integer. So, when we call JsonString("ABC", true), it is in fact JsonString("ABC", 1) that is called. This explains why all the static strings have a length of one.

This bug in the Arduino core for SAM3X was already reported in arduino/ArduinoCore-sam#50 but the issue was closed by the author.

Until I find a better solution, you can fix this issue by adding #undef true before including ArduinoJson.h, like so:

+ #undef true
  #include <ArduinoJson.h>

bblanchon avatar Jun 19 '25 09:06 bblanchon

Dear Benoit, Thank you very much, amazing .... the subtleties of it all. I will do as you suggest. Thanks again for tracking this down. Kind regards, Ben

BenTubbing avatar Jun 19 '25 10:06 BenTubbing

Fix published in ArduinoJson 7.4.2

bblanchon avatar Jun 20 '25 07:06 bblanchon