mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-17 21:56:40 +01:00
add const char variation for getattr
This commit is contained in:
@@ -7,17 +7,20 @@
|
||||
#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 int verifyJsonObject(JsonObject &json)
|
||||
{
|
||||
return json.success();
|
||||
};
|
||||
String toJsonString(){
|
||||
String toJsonString()
|
||||
{
|
||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||
JsonObject &root = jsonBuffer.createObject();
|
||||
mapJsonObject(root);
|
||||
@@ -26,31 +29,58 @@ struct JsonStruct {
|
||||
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);
|
||||
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;
|
||||
@@ -58,20 +88,24 @@ struct JsonStruct {
|
||||
|
||||
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();
|
||||
valid = configFile && verifyJsonObject(json);
|
||||
if(valid){
|
||||
if (valid)
|
||||
{
|
||||
mapJsonObject(json);
|
||||
json.printTo(configFile);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user