mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-18 06:06:40 +01:00
add const char variation for getattr
This commit is contained in:
@@ -7,71 +7,105 @@
|
|||||||
#include "SPIFFS.h"
|
#include "SPIFFS.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct JsonStruct {
|
struct JsonStruct
|
||||||
|
{
|
||||||
int valid = 0;
|
int valid = 0;
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
virtual void mapJsonObject(JsonObject& json);
|
virtual void mapJsonObject(JsonObject &json);
|
||||||
virtual void fromJsonObject(JsonObject& json);
|
virtual void fromJsonObject(JsonObject &json);
|
||||||
virtual int verifyJsonObject(JsonObject& json){
|
virtual int verifyJsonObject(JsonObject &json)
|
||||||
|
{
|
||||||
return json.success();
|
return json.success();
|
||||||
};
|
};
|
||||||
String toJsonString(){
|
String toJsonString()
|
||||||
|
{
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
JsonObject &root = jsonBuffer.createObject();
|
||||||
mapJsonObject(root);
|
mapJsonObject(root);
|
||||||
String jsonString;
|
String jsonString;
|
||||||
root.printTo(jsonString);
|
root.printTo(jsonString);
|
||||||
return jsonString;
|
return jsonString;
|
||||||
}
|
}
|
||||||
|
|
||||||
String getAttrFromJson(JsonObject& json, const char* attr, String defautValue = ""){
|
String getAttrFromJson(JsonObject &json, const char *attr, String defautValue = "")
|
||||||
if(json.containsKey(String(attr))){
|
{
|
||||||
|
if (json.containsKey(String(attr)))
|
||||||
|
{
|
||||||
const char *value = json[attr];
|
const char *value = json[attr];
|
||||||
return String(value);
|
return String(value);
|
||||||
}
|
}
|
||||||
return defautValue;
|
return defautValue;
|
||||||
}
|
}
|
||||||
int getIntAttrFromJson(JsonObject& json, const char* attr, int defautValue = 0){
|
const char *getAttr(JsonObject &json, const char *attr, const char *defautValue = "")
|
||||||
if(json.containsKey(attr)){
|
{
|
||||||
|
if (json.containsKey(String(attr)))
|
||||||
|
{
|
||||||
|
const char *value = json[attr];
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return defautValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getStrAttrFromJson(JsonObject &json, const char *attr, String defautValue = "")
|
||||||
|
{
|
||||||
|
if (json.containsKey(String(attr)))
|
||||||
|
{
|
||||||
|
const char *value = json[attr];
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
return defautValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getIntAttrFromJson(JsonObject &json, const char *attr, int defautValue = 0)
|
||||||
|
{
|
||||||
|
if (json.containsKey(attr))
|
||||||
|
{
|
||||||
return json[attr];
|
return json[attr];
|
||||||
}
|
}
|
||||||
return defautValue;
|
return defautValue;
|
||||||
}
|
}
|
||||||
// Map a json object to this struct.
|
// Map a json object to this struct.
|
||||||
// Parse a json string and map parsed object
|
// Parse a json string and map parsed object
|
||||||
void fromJsonString(String& str){
|
void fromJsonString(String &str)
|
||||||
|
{
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& json = jsonBuffer.parseObject(str);
|
JsonObject &json = jsonBuffer.parseObject(str);
|
||||||
valid = verifyJsonObject(json);
|
valid = verifyJsonObject(json);
|
||||||
if(valid) {
|
if (valid)
|
||||||
|
{
|
||||||
fromJsonObject(json);
|
fromJsonObject(json);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void fromFile(const char* path) {
|
void fromFile(const char *path)
|
||||||
|
{
|
||||||
File configFile = SPIFFS.open(path, "r");
|
File configFile = SPIFFS.open(path, "r");
|
||||||
String cfgFileStr = configFile.readString();
|
String cfgFileStr = configFile.readString();
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.parseObject(cfgFileStr);
|
JsonObject &json = jsonBuffer.parseObject(cfgFileStr);
|
||||||
|
|
||||||
valid = verifyJsonObject(json);
|
valid = verifyJsonObject(json);
|
||||||
|
|
||||||
if(configFile) {
|
if (configFile)
|
||||||
|
{
|
||||||
fromJsonObject(json);
|
fromJsonObject(json);
|
||||||
}
|
}
|
||||||
if(!valid){
|
if (!valid)
|
||||||
|
{
|
||||||
Serial.println("ERROR: read failed for " + String(path));
|
Serial.println("ERROR: read failed for " + String(path));
|
||||||
}
|
}
|
||||||
configFile.close();
|
configFile.close();
|
||||||
}
|
}
|
||||||
void saveFile(const char* path) {
|
void saveFile(const char *path)
|
||||||
|
{
|
||||||
File configFile = SPIFFS.open(path, "w");
|
File configFile = SPIFFS.open(path, "w");
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& json = jsonBuffer.createObject();
|
JsonObject &json = jsonBuffer.createObject();
|
||||||
valid = configFile && verifyJsonObject(json);
|
valid = configFile && verifyJsonObject(json);
|
||||||
if(valid){
|
if (valid)
|
||||||
|
{
|
||||||
mapJsonObject(json);
|
mapJsonObject(json);
|
||||||
json.printTo(configFile);
|
json.printTo(configFile);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user