How do we get this error?

If you try to compile the following program:

JsonDocument doc;
doc["example"] = Serial;

you’ll get the following compiler output:

In file included from .../ArduinoJson.hpp:42,
                 from .../ArduinoJson.h:9,
                 from MyProject.ino:1:
.../ArduinoJson/Variant/ConverterImpl.hpp: In instantiation of 'static void ArduinoJson::V701PB2::Converter<T, Enable>::toJson(const T&, ArduinoJson::V701PB2::JsonVariant) [with T = HardwareSerial; Enable = void]':
.../ArduinoJson/Variant/VariantRefBaseImpl.hpp:135:57:   required from 'bool ArduinoJson::V701PB2::detail::VariantRefBase<TDerived>::set(const T&) const [with T = HardwareSerial; TDerived = ArduinoJson::V701PB2::detail::MemberProxy<ArduinoJson::V701PB2::JsonDocument&, const char*>]'
.../ArduinoJson/Object/MemberProxy.hpp:33:14:   required from 'ArduinoJson::V701PB2::detail::MemberProxy< <template-parameter-1-1>, <template-parameter-1-2> >& ArduinoJson::V701PB2::detail::MemberProxy< <template-parameter-1-1>, <template-parameter-1-2> >::operator=(const T&) [with T = HardwareSerial; TUpstream = ArduinoJson::V701PB2::JsonDocument&; TStringRef = const char*]'
MyProject.ino:6:18:   required from here
.../ArduinoJson/Variant/ConverterImpl.hpp:26:18: error: no matching function for call to 'convertToJson(const HardwareSerial&, ArduinoJson::V701PB2::JsonVariant&)'
   26 |     convertToJson(src, dst); // Error here? See
      |     ~~~~~~~~~~~~~^~~~~~~~~~

This output can be quite intimidating, but really, only two lines are important:

MyProject.ino:6:18:   required from here

error: no matching function for call to 'convertToJson(const HardwareSerial&, JsonVariant&)'

With this message, the compiler tells us that it has been looking for a function called convertToJson() that supports HardwareSerial, but it didn’t find one. The “required from here” line shows the line in your program that triggered this error.

HardwareSerial is the type of Serial which we used in the assignment in the second line of the program. Since ArduinoJson doesn’t know how to convert a HardwareSerial to JSON, you cannot use it as a JSON value.

How to fix this error?

As we just saw, we get this error when we try to set a value for an unsupported type. Most of the time, this is a problem in the program’s design, like the example above (it doesn’t make sense to use Serial as a value in a JsonDocument).
In this case, you need to rethink the design of your program.

In some cases, however, you may want to use a type that makes sense as a JSON value but is not natively supported by ArduinoJson.
In this case, you can add an overload of convertToJson() to support this type.

For example, if you want to add support for struct tm:

bool convertToJson(const tm& t, JsonVariant variant) {
  char buffer[32];
  strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S.%f", &t);
  return variant.set(buffer);

You must declare this function in the same namespace as the type you want to convert to JSON so that the compiler can find it through ADL.

See also