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.hpp:30:0,
                 from [...]/ArduinoJson.h:9,
                 from MyProject.ino:1:
[...]/ArduinoJson/Variant/ConverterImpl.hpp: In instantiation of 'static void Converter<T, Enable>::toJson(const T&, VariantRef) [with T = HardwareSerial; Enable = void]':
[...]/ArduinoJson/Variant/VariantRef.hpp:88:25:   required from 'bool VariantRef::set(const T&) const [with T = HardwareSerial]'
[...]/ArduinoJson/Object/MemberProxy.hpp:49: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:6:18:   required from here
[...]/ArduinoJson/Variant/ConverterImpl.hpp:17:18: error: no matching function for call to 'convertToJson(const HardwareSerial&, VariantRef&)'
     convertToJson(src, dst); // Error here? See https://arduinojson.org/v6/unsupported-set/
     ~~~~~~~~~~~~~^~~~~~~~~~
In file included from [...]/ArduinoJson/Document/BasicJsonDocument.hpp:7:0,
                 from [...]/ArduinoJson/Document/DynamicJsonDocument.hpp:7,
                 from [...]/ArduinoJson.hpp:21,
                 from [...]/ArduinoJson.h:9,
                 from MyProject.ino:1:
[...]/ArduinoJson/Document/JsonDocument.hpp:340:13: note: candidate: void convertToJson(const JsonDocument&, VariantRef)
 inline void convertToJson(const JsonDocument& src, VariantRef dst) {
             ^~~~~~~~~~~~~
[...]/ArduinoJson/Document/JsonDocument.hpp:340:13: note:   no known conversion for argument 1 from 'const HardwareSerial' to 'const JsonDocument&'
In file included from [...]/ArduinoJson.hpp:30:0,
                 from [...]/ArduinoJson.h:9,
                 from MyProject.ino:1:
[...]/ArduinoJson/Variant/ConverterImpl.hpp:240:13: note: candidate: void convertToJson(const Printable&, VariantRef)
 inline void convertToJson(const ::Printable& src, VariantRef dst) {
             ^~~~~~~~~~~~~
[...]/ArduinoJson/Variant/ConverterImpl.hpp:240:13: note:   no known conversion for argument 1 from 'const HardwareSerial' to 'const Printable&'
[...]/ArduinoJson/Variant/ConverterImpl.hpp:150:59: note: candidate: template<class T> typename enable_if<IsString<TString, void>::value, bool>::type convertToJson(const T&, VariantRef)
 inline typename enable_if<IsString<T>::value, bool>::type convertToJson(
                                                           ^~~~~~~~~~~~~
[...]/ArduinoJson/Variant/ConverterImpl.hpp:150:59: note:   template argument deduction/substitution failed:
[...]/ArduinoJson/Variant/ConverterImpl.hpp: In substitution of 'template<class T> typename enable_if<IsString<TString, void>::value, bool>::type convertToJson(const T&, VariantRef) [with T = HardwareSerial]':
[...]/ArduinoJson/Variant/ConverterImpl.hpp:17:18:   required from 'static void Converter<T, Enable>::toJson(const T&, VariantRef) [with T = HardwareSerial; Enable = void]'
[...]/ArduinoJson/Variant/VariantRef.hpp:88:25:   required from 'bool VariantRef::set(const T&) const [with T = HardwareSerial]'
[...]/ArduinoJson/Object/MemberProxy.hpp:49: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:6:18:   required from here
[...]/ArduinoJson/Variant/ConverterImpl.hpp:150: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:6:18:   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(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