Skip to content

Issue with JsonVariant.as<T>() where T is char * Need to update docs! #1860

@djaus2

Description

@djaus2

Describe the bug
When using the example to iterate thjrough elements of a Json Document ins the part .as<T>() can't use char* for T

Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:

  1. The issue happens at compile time
  2. Error says "no matching function for call to ...(<unresolved overloaded function type>)"
  3. Adding the parentheses doesn't fix the issue .. Weren't missing in the parenthesis first place.

PS Liked that trouble shooter.

Environment
Here is the environment that I used:

  • Microcontroller: RP2040
  • RPI Pico W
  • IDE: Arduino IDE 1.2.03

Reproduction
This is a compile time error only.
Small sketch with same code as per documentation:

  DynamicJsonDocument doc(1024);
  deserializeJson(doc, "{\"first\":\"\",\"second\":\"second\"}");
  JsonObject root = doc.as<JsonObject>();

  // using C++11 syntax (preferred):
  for (JsonPair kv : root) {
      Serial.println(kv.key().c_str());
      Serial.println(kv.value().as<char *>());
  }

Workaround: replace char * with String

Further: I see its deprecated anyway
const char* as<char*>() const; // ⚠️ deprecated since 6.18
const char* as<const char*>() const;

SO DOCUMENTATION NEEDS UPDATE.. I spent hours on this!
But thanks anyway for the library.

Compiler output
Compilation error: no matching function for call to 'ArduinoJson6200_F1::JsonVariant::as<char*>()'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv6ArduinoJson 6

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions