Open mairas opened 2 hours ago
A minimal test case using PlatformIO:
#include <Arduino.h>
#include "ArduinoJson.h"
template <typename T>
class Nullable {
public:
Nullable() : value_{} {}
Nullable(T value) : value_{value} {}
operator T() const { return value_; }
operator T &() const { return value_; }
bool is_valid() const { return value_ != invalid_value_; }
T value() const { return value_; }
private:
T value_;
static T invalid_value_;
};
template <> float Nullable<float>::invalid_value_ = std::numeric_limits<float>::lowest();
template <typename T>
void convertToJson(const Nullable<T> &src, JsonVariant dst) {
if (src.is_valid()) {
dst.set(src.value());
} else {
dst.clear();
}
}
void setup() {
Nullable<float> nullable_value = Nullable<float>{123.4};
JsonDocument doc;
doc["value"] = nullable_value;
Serial.begin(115200);
delay(100);
serializeJson(doc, Serial);
}
void loop() {}
Oh, and I also tried defining convertToJson
for Nullable<float>
directly (without the template), but that resulted in the same error.
Describe the issue
I am working with numeric types that have a magic value indicating invalid/non-existing value (originating from NMEA 2000). When converting those to Signal K compatible Json, the invalid values need to be serialized to
null
.I have created the following class template:
I have also defined the following custom converter:
Elsewhere, I have this:
I would expect
obj["elevation"]
to become either a number or null.Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
Troubleshooter does not have that particular error message:
Environment
Here is the environment that I'm using':
Reproduction
Provided above.
I'll create an isolated test case and provide it as a comment to this issue.