Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a char as a member of a JsonDocument produces a convertToJson error #2043

Closed
smaslincosi opened this issue Jan 25, 2024 · 3 comments
Closed
Labels
question v6 ArduinoJson 6 v7 ArduinoJson 7

Comments

@smaslincosi
Copy link

smaslincosi commented Jan 25, 2024

Description
I am in the process of migrating existing code to ArduinoJson v7, and I have had issues with compilation that I believe stem from changes to the library. In previous versions of the library, I was able to add a char to a StaticJsonDocument without issue. In v7 adding a char to a JsonDocument produces an error with the convertToJson function.
My intended target is Teensy 3.2, but I have been able to reproduce this issue with the provided code on other platforms such as Arduino Uno.
My provided reproduction code works with version 6.19.4, but not with 6.20.0 or thereafter.

Troubleshooter's report

  1. The program uses ArduinoJson 7
  2. The issue happens at compile time
  3. Error says "no matching function for call to ..."
  4. Error says "no matching function for call to convertToJson(...)"
  5. Converting the value doesn't fix the issue

Environment

  • Microcontroller: Teensy 3.2
  • Core/Framework: Teensyduino
  • IDE: Arduino IDE 2.2.1

Reproduction code

#include <ArduinoJson.h>

void setup() {

  JsonDocument doc;

  //This will compile:
  //const char* cs = "A";
  //doc["val"] = cs;

  //This will not compile with ArduinoJson version 6.20.0 or greater - error: no matching function for call to 'convertToJson(const char&, ArduinoJson::V702L1::JsonVariant&)'
  char c = 'A';
  doc["val"] = c;
}

void loop() {
  // put your main code here, to run repeatedly:

}
bblanchon added a commit to bblanchon/ArduinoJsonTroubleshooter that referenced this issue Jan 26, 2024
@bblanchon
Copy link
Owner

Hi @smaslincosi,

ArduinoJson 6.18 deprecated the support for "naked" char, then ArduinoJson 6.20 removed it entirely.

JSON supports numbers and strings but has no "character" type.
If you want to store an integer, you can use signed char, unsigned char, or another integer type.
If you want to store a string, you can use const char*, String, or any supported string type.

If you cannot live without char support, you must create a custom converter.
For example, the following converter stores chars as one-character strings.

namespace ArduinoJson {
template <>
struct Converter<char> {
  static void toJson(char c, JsonVariant var) {
    char buf[] = {c, 0};
    var.set(buf);
  }

  static char fromJson(JsonVariantConst src) {
    auto p = src.as<const char*>();
    return p ? p[0] : 0;
  }
};
}

Best regards,
Benoit

bblanchon added a commit that referenced this issue Jan 26, 2024
@smaslincosi
Copy link
Author

Thank you for the incredibly helpful information!

@bblanchon
Copy link
Owner

You're welcome, @smaslincosi!
Thank you for using ArduinoJson.
Don't forget to cast a star to support the project 😉

@bblanchon bblanchon added v7 ArduinoJson 7 v6 ArduinoJson 6 labels Feb 1, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6 v7 ArduinoJson 7
Projects
None yet
Development

No branches or pull requests

2 participants