error: no matching function for call to convertFromJson(...)
(applies to ArduinoJson 6.18+)
This error tells you that ArduinoJson cannot return the type you requested.
The type in question is the type of the second parameter of convertFromJson()
.
For example, in the following error message, the type is char
:
error: no matching function for call to 'convertFromJson(JsonVariantConst&, char&)'
ArduinoJson 7 doesn’t support char
, you must use int8_t
, uint8_t
, or any other integral type instead.
It doesn’t support char*
either, you must use const char*
instead.
How do we get this error?
As an example, we’ll take the tm
structure, which describes an instant in time.
This structure is declared in the standard C header <time.h>
.
You could be tempted to extract a tm
instance from a JsonDocument
like so:
tm timestamp = doc["timestamp"];
However, if you do that, you’ll receive the following compilation error:
In file included from .../ArduinoJson.hpp:42,
from .../ArduinoJson.h:9,
from MyProject.ino:1:
.../ArduinoJson/Variant/ConverterImpl.hpp: In instantiation of 'static T ArduinoJson::V701PB2::Converter<T, Enable>::fromJson(ArduinoJson::V701PB2::JsonVariantConst) [with T = tm; Enable = void]':
.../ArduinoJson/Variant/VariantRefBase.hpp:52:34: required from 'typename ArduinoJson::V701PB2::detail::enable_if<(! ArduinoJson::V701PB2::detail::ConverterNeedsWriteableRef<T>::value), T>::type ArduinoJson::V701PB2::detail::VariantRefBase<TDerived>::as() const [with T = tm; TDerived = ArduinoJson::V701PB2::detail::MemberProxy<ArduinoJson::V701PB2::JsonDocument&, const char*>; typename ArduinoJson::V701PB2::detail::enable_if<(! ArduinoJson::V701PB2::detail::ConverterNeedsWriteableRef<T>::value), T>::type = tm]'
.../ArduinoJson/Variant/VariantRefBase.hpp:64:17: required from 'ArduinoJson::V701PB2::detail::VariantRefBase<TDerived>::operator T() const [with T = tm; <template-parameter-2-2> = void; TDerived = ArduinoJson::V701PB2::detail::MemberProxy<ArduinoJson::V701PB2::JsonDocument&, const char*>]'
MyProject.ino:13:31: required from here
.../ArduinoJson/Variant/ConverterImpl.hpp:33:20: error: no matching function for call to 'convertFromJson(ArduinoJson::V701PB2::JsonVariantConst&, tm&)'
33 | convertFromJson(src, result); // Error here? See https://arduinojson.org/v7/unsupported-as/
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
...
convertFromJson()
is a function of ArduinoJson that converts a JSON value (the JsonVariantConst
) to another type (tm
, in this case).
Here, the call fails because no overload of convertFromJson()
supports the tm
type.
In other words, ArduinoJson doesn’t know how to extract a tm
value from a JsonDocument
.
How to fix this error?
I’ll show how to fix the error for tm
, but you can apply the same resolution for any default-constructible type (see next section for non-default-constructible types).
As we just saw, this error is due to the fact that ArduinoJson doesn’t know how to extract a tm
from a JsonDocument
, so you need to tell him how to do that.
You must create an overload of convertFromJson()
that supports tm
.
For example, you could write:
void convertFromJson(JsonVariantConst src, tm& dst) {
strptime(src.as<const char*>(), "%FT%TZ", &dst);
}
You must declare this function in the same namespace as the type you want to convert (the global namespace in the case of tm
) so that the compiler can find it through ADL.
How to support non-default-constructible types?
Before calling convertFromJson()
, ArduinoJson creates an instance of the target type to pass a reference as a second argument.
This requires that the type is default-constructible; otherwise, ArduinoJson wouldn’t know how to construct the instance.
Therefore, you cannot use convertFromJson()
for non-default-constructible types.
Instead, you must use a lower-level hook based on the specialization of Converter<T>
, like so:
namespace ArduinoJson {
template <>
struct Converter<tm> {
static tm fromJson(JsonVariantConst src) {
tm result;
strptime(src.as<const char*>(), "%FT%TZ", &dst);
return result;
}
};
}
Of course, tm
is default-constructible, but you get the idea, right?
See also
- ArduinoJson 6.18: custom converters
- “Advanced Techniques” chapter in Mastering ArduinoJson