Suppose you have a
doc, and you want to print the value
If you do this:
you’ll get the following compilation error:
call of overloaded 'println(ArduinoJson6101_00000::enable_if<true, ArduinoJson6101_00000::MemberProxy<ArduinoJson6101_00000::JsonDocument&, const char*> >::type)' is ambiguous
Quite scary, right?
It’s not that complicated. The compiler tells us that we passed some obscure type to
Serial.println() and that he didn’t know which overload of
println() to choose.
What’s an overload, you ask? When the same function has several versions with different parameters, we call each version an “overload.” In our case,
Serial.println() has 12 overloads:
size_t println(const __FlashStringHelper *); size_t println(const String &s); size_t println(const char); size_t println(char); size_t println(unsigned char, int = DEC); size_t println(int, int = DEC); size_t println(unsigned int, int = DEC); size_t println(long, int = DEC); size_t println(unsigned long, int = DEC); size_t println(double, int = 2); size_t println(const Printable&); size_t println(void);
As you can see, there is no overload for
JsonVariant, so how can the compiler decide which one to use?
There are two ways to solve the ambiguity:
- You can explicitly cast the
JsonVariantto type supported by
- You can avoid
The first solution only works if you know the type of the value. For example, if you know that
"myValue" contains an integer, you can write:
However, if you don’t know the type, you must use the second solution: replace
As you saw above,
println() has an overload that takes a
Printable, an interface that allows supporting custom types. If
Printable, the call to
println() would not be ambiguous. So why ArduinoJson doesn’t do that?
Printable makes the code of the library significantly bigger, and having a small footprint is one of the primary goals of ArduinoJson. Early versions supported
Printable, but this feature was removed because it was not worth the extra bytes.