joboccara / NamedType

Implementation of strong types in C++
MIT License
766 stars 85 forks source link

Adding skill to explicitly convert from ArduinoJson JsonVariantConst #71

Closed Bascy closed 1 year ago

Bascy commented 2 years ago

I'm using the NamedType library (a simplified copy, using C++11 on an ESP32 platform) to implement a NodeId type, based on an uint16_t. The configuration of our device is read form a json file, and we're using ArduinoJson library for that. I would like a way to create a NodeId instance from an element of that Json document, a JsonVariantConst instance from ArduinoJson.

I added an extra constructor to the NamedType template class and this works:

NamedType(JsonVariantConst json) : value_(json.as<T>()) {}

The <T> of course refers to the base type of the NamedType, so this only works if ArduinoJson actually supports that type in their as<T>() method.

I think it would be better if I could add such a feature as a Skill so I can also define NamedTypes which do not poses this skill, but my personal templating skills are not good enough to get this done.

Can anybody help me with this?