How do we get this error?

If you try to compile the following program:

StaticJsonDocument<200> doc;
doc["example"] = Serial;

you’ll get the following compiler output:

In file included from ArduinoJson/src/ArduinoJson.hpp:30:0,
                 from ArduinoJson/src/ArduinoJson.h:9,
                 from MyProject.ino:9:
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp: In instantiation of 'static bool Converter<T, Enable>::toJson(VariantRef, const T&) [with T = HardwareSerial; Enable = void]':
ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp:88:32:   required from 'bool VariantRef::set(const T&) const [with T = HardwareSerial]'
ArduinoJson/src/ArduinoJson/Object/MemberProxy.hpp:63:5:   required from 'typename enable_if<(! is_array<T>::value), MemberProxy<TParent, TStringRef>&>::type MemberProxy<TParent, TStringRef>::operator=(const TValue&) [with TValue = HardwareSerial; TObject = JsonDocument&; TStringRef = const char*; typename enable_if<(! is_array<T>::value), MemberProxy<TParent, TStringRef>&>::type = MemberProxy<JsonDocument&, const char*>&]'
MyProject.ino:30:19:   required from here
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:17:25: error: no matching function for call to 'convertToJson(VariantRef&, const HardwareSerial&)'
     return convertToJson(variant, value); // Error here? See https://arduinojson.org/v6/unsupported-set/
            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from ArduinoJson/src/ArduinoJson/Document/BasicJsonDocument.hpp:7:0,
                 from ArduinoJson/src/ArduinoJson/Document/DynamicJsonDocument.hpp:7,
                 from ArduinoJson/src/ArduinoJson.hpp:21,
                 from ArduinoJson/src/ArduinoJson.h:9,
                 from MyProject.ino:9:
ArduinoJson/src/ArduinoJson/Document/JsonDocument.hpp:340:13: note: candidate: bool convertToJson(VariantRef, const JsonDocument&)
 inline bool convertToJson(VariantRef variant, const JsonDocument& doc) {
             ^~~~~~~~~~~~~
ArduinoJson/src/ArduinoJson/Document/JsonDocument.hpp:340:13: note:   no known conversion for argument 2 from 'const HardwareSerial' to 'const JsonDocument&'
In file included from ArduinoJson/src/ArduinoJson.hpp:30:0,
                 from ArduinoJson/src/ArduinoJson.h:9,
                 from MyProject.ino:9:
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:254:13: note: candidate: bool convertToJson(VariantRef, const Printable&)
 inline bool convertToJson(VariantRef variant, const ::Printable& value) {
             ^~~~~~~~~~~~~
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:254:13: note:   no known conversion for argument 2 from 'const HardwareSerial' to 'const Printable&'
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:141:59: note: candidate: template<class T> typename enable_if<IsString<T>::value, bool>::type convertToJson(VariantRef, const T&)
 inline typename enable_if<IsString<T>::value, bool>::type convertToJson(
                                                           ^~~~~~~~~~~~~
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:141:59: note:   template argument deduction/substitution failed:
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp: In substitution of 'template<class T> typename enable_if<IsString<T>::value, bool>::type convertToJson(VariantRef, const T&) [with T = HardwareSerial]':
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:17:25:   required from 'static bool Converter<T, Enable>::toJson(VariantRef, const T&) [with T = HardwareSerial; Enable = void]'
ArduinoJson/src/ArduinoJson/Variant/VariantRef.hpp:88:32:   required from 'bool VariantRef::set(const T&) const [with T = HardwareSerial]'
ArduinoJson/src/ArduinoJson/Object/MemberProxy.hpp:63:5:   required from 'typename enable_if<(! is_array<T>::value), MemberProxy<TParent, TStringRef>&>::type MemberProxy<TParent, TStringRef>::operator=(const TValue&) [with TValue = HardwareSerial; TObject = JsonDocument&; TStringRef = const char*; typename enable_if<(! is_array<T>::value), MemberProxy<TParent, TStringRef>&>::type = MemberProxy<JsonDocument&, const char*>&]'
MyProject.ino:30:19:   required from here
ArduinoJson/src/ArduinoJson/Variant/ConverterImpl.hpp:141:59: error: no type named 'type' in 'struct enable_if<false, bool>'

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

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

MyProject.ino:30:19:   required from here

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 supports this type.

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

bool convertToJson(JsonVariant variant, const tm& t) {
  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.