2020-05-19 23:32:49 +00:00
|
|
|
#ifndef JsonUtils_h
|
|
|
|
#define JsonUtils_h
|
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
#include <Arduino.h>
|
2021-03-09 09:17:03 +00:00
|
|
|
#include <IPUtils.h>
|
2020-05-14 22:23:45 +00:00
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
class JsonUtils {
|
|
|
|
public:
|
2020-07-07 21:22:38 +00:00
|
|
|
static void readIP(JsonObject& root, const String& key, IPAddress& ip, const String& def) {
|
|
|
|
IPAddress defaultIp = {};
|
|
|
|
if (!defaultIp.fromString(def)) {
|
|
|
|
defaultIp = INADDR_NONE;
|
|
|
|
}
|
|
|
|
readIP(root, key, ip, defaultIp);
|
|
|
|
}
|
|
|
|
static void readIP(JsonObject& root, const String& key, IPAddress& ip, const IPAddress& defaultIp = INADDR_NONE) {
|
2020-05-21 07:42:21 +00:00
|
|
|
if (!root[key].is<String>() || !ip.fromString(root[key].as<String>())) {
|
2020-07-07 21:22:38 +00:00
|
|
|
ip = defaultIp;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-21 07:42:21 +00:00
|
|
|
static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) {
|
2021-03-09 09:17:03 +00:00
|
|
|
if (IPUtils::isSet(ip)) {
|
2020-05-21 07:42:21 +00:00
|
|
|
root[key] = ip.toString();
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-05-19 23:32:49 +00:00
|
|
|
|
|
|
|
#endif // end JsonUtils
|