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