mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 05:24:30 +01:00
read config from json on SPIFFS
This commit is contained in:
@@ -12,16 +12,29 @@ struct JsonStruct {
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
virtual void mapJsonObject(JsonObject& json);
|
||||
virtual void fromJsonObject(JsonObject& json);
|
||||
virtual int verifyJsonObject(JsonObject& json){
|
||||
return json.success();
|
||||
//&& json.containsKey(JSON_DOMAIN)
|
||||
};
|
||||
virtual String toJsonString();
|
||||
String toJsonString(){
|
||||
//StaticJsonBuffer<200> StringjsonBuffer;
|
||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||
JsonObject& root = jsonBuffer.createObject();
|
||||
mapJsonObject(root);
|
||||
String jsonString;
|
||||
root.printTo(jsonString);
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
String getAttrFromJson(JsonObject& json, const char* attr){
|
||||
if(json.containsKey(attr)){
|
||||
return json[attr];
|
||||
if(json.containsKey(String(attr))){
|
||||
const char *value = json[attr];
|
||||
return String(value);
|
||||
//return json[attr];
|
||||
}
|
||||
return "";
|
||||
return "no value";
|
||||
}
|
||||
int getIntAttrFromJson(JsonObject& json, const char* attr){
|
||||
if(json.containsKey(attr)){
|
||||
@@ -30,14 +43,52 @@ struct JsonStruct {
|
||||
return 0;
|
||||
}
|
||||
// Map a json object to this struct.
|
||||
virtual int fromJsonObject(JsonObject& json);
|
||||
// Parse a json string and map parsed object
|
||||
int fromJsonString(String& str){
|
||||
void fromJsonString(String& str){
|
||||
//StaticJsonBuffer<200> jsonBuffer;
|
||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||
JsonObject& json = jsonBuffer.parseObject(str);
|
||||
return fromJsonObject(json);
|
||||
valid = verifyJsonObject(json);
|
||||
if(valid) {
|
||||
fromJsonObject(json);
|
||||
}
|
||||
};
|
||||
|
||||
void fromFile(const char* path) {
|
||||
Serial.println("read json");
|
||||
File configFile = SPIFFS.open(path, "r");
|
||||
String cfgFileStr = configFile.readString();
|
||||
|
||||
// Allocate a buffer to store contents of the file.
|
||||
//size_t size = configFile.size();
|
||||
//std::unique_ptr<char[]> buf(new char[size]);
|
||||
//configFile.readBytes(buf.get(), size);
|
||||
//StaticJsonBuffer<1024> jsonBuffer;
|
||||
//JsonObject& json = jsonBuffer.parseObject(buf.get());
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.parseObject(cfgFileStr);
|
||||
|
||||
valid = verifyJsonObject(json);
|
||||
|
||||
if(configFile) {
|
||||
Serial.println("map json object");
|
||||
fromJsonObject(json);
|
||||
}
|
||||
if(!valid){
|
||||
Serial.println("read json failed");
|
||||
}
|
||||
configFile.close();
|
||||
}
|
||||
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){
|
||||
mapJsonObject(json);
|
||||
json.printTo(configFile);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user