serializeJson() version 7.4.1 do not create element names in fully
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
It works fine here: https://wandbox.org/permlink/fv1IVJA0QIB6bdN6 Please provide an MCVE.
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"}...
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.
I successfully tested the program above with the following boards:
- Arduino Leonardo
- Arduino UNO R4 WiFi
- Adafruit ESP8266 Feather Huzzah
- 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.
Sorry I forgot to write : Board : Arduino Due (original)
I uninstall and reinstall the lib but still give the same error.
I had the same issue on the Particle platform. Got around it by following this thread on the Particle forums.
Try changing
doc["cat"]todoc[String("cat")].Note, I am also setting
ARDUINOJSON_ENABLE_ARDUINO_STRINGto 1.
This solved the problem.
and also I wrote the same for the valu 👍
doc[String("cat")] = String("sensor")
@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?
I also successfully tested on:
- Arduino MKR1000 WiFi
- Adafruit ItsyBitsy M4
- Teensy 3.1
- Nucleo-32
- Teensy 4.1
- Arduino Nano Every
@atwalsh, using
Stringis 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.
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?
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
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.
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.
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>
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
Fix published in ArduinoJson 7.4.2