add bed cooler project.
This commit is contained in:
106
src/util/json_utils.cpp
Normal file
106
src/util/json_utils.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "app.h"
|
||||
|
||||
static int findJsonKey(const String &json, const char *key) {
|
||||
String needle = "\"" + String(key) + "\"";
|
||||
int p = json.indexOf(needle);
|
||||
if (p < 0) return -1;
|
||||
p = json.indexOf(':', p + needle.length());
|
||||
return p < 0 ? -1 : p + 1;
|
||||
}
|
||||
|
||||
String jsonEscape(const String &s) {
|
||||
String out;
|
||||
out.reserve(s.length() + 8);
|
||||
for (size_t i = 0; i < s.length(); i++) {
|
||||
char c = s[i];
|
||||
if (c == '"' || c == '\\') {
|
||||
out += '\\';
|
||||
out += c;
|
||||
} else if (c == '\n') {
|
||||
out += "\\n";
|
||||
} else if (c == '\r') {
|
||||
out += "\\r";
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
String jsonOk(const String &payload) {
|
||||
return String("{\"success\":true") + (payload.length() ? "," + payload : "") + "}";
|
||||
}
|
||||
|
||||
String jsonError(const String &message) {
|
||||
return "{\"success\":false,\"error\":\"" + jsonEscape(message) + "\"}";
|
||||
}
|
||||
|
||||
void sendJson(int code, const String &body) {
|
||||
server.sendHeader("Cache-Control", "no-store");
|
||||
server.send(code, "application/json", body);
|
||||
}
|
||||
|
||||
String requestBody() {
|
||||
return server.hasArg("plain") ? server.arg("plain") : "";
|
||||
}
|
||||
|
||||
String jsonStringValue(const String &json, const char *key, const String &fallback) {
|
||||
int p = findJsonKey(json, key);
|
||||
if (p < 0) return fallback;
|
||||
while (p < (int)json.length() && isspace(json[p])) p++;
|
||||
if (p >= (int)json.length() || json[p] != '"') return fallback;
|
||||
p++;
|
||||
String out;
|
||||
bool esc = false;
|
||||
for (; p < (int)json.length(); p++) {
|
||||
char c = json[p];
|
||||
if (esc) {
|
||||
out += c == 'n' ? '\n' : c == 'r' ? '\r' : c;
|
||||
esc = false;
|
||||
} else if (c == '\\') {
|
||||
esc = true;
|
||||
} else if (c == '"') {
|
||||
return out;
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
int jsonIntValue(const String &json, const char *key, int fallback) {
|
||||
int p = findJsonKey(json, key);
|
||||
if (p < 0) return fallback;
|
||||
while (p < (int)json.length() && isspace(json[p])) p++;
|
||||
return json.substring(p).toInt();
|
||||
}
|
||||
|
||||
bool jsonBoolValue(const String &json, const char *key, bool fallback) {
|
||||
int p = findJsonKey(json, key);
|
||||
if (p < 0) return fallback;
|
||||
while (p < (int)json.length() && isspace(json[p])) p++;
|
||||
if (json.substring(p, p + 4) == "true") return true;
|
||||
if (json.substring(p, p + 5) == "false") return false;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
String jsonRolesValue(const String &json, const char *key, const String &fallback) {
|
||||
int p = findJsonKey(json, key);
|
||||
if (p < 0) return fallback;
|
||||
while (p < (int)json.length() && isspace(json[p])) p++;
|
||||
if (json[p] == '"') return jsonStringValue(json, key, fallback);
|
||||
if (json[p] != '[') return fallback;
|
||||
String roles;
|
||||
p++;
|
||||
while (p < (int)json.length() && json[p] != ']') {
|
||||
while (p < (int)json.length() && json[p] != '"' && json[p] != ']') p++;
|
||||
if (p >= (int)json.length() || json[p] == ']') break;
|
||||
p++;
|
||||
String role;
|
||||
while (p < (int)json.length() && json[p] != '"') role += json[p++];
|
||||
if (roles.length()) roles += "|";
|
||||
roles += role;
|
||||
p++;
|
||||
}
|
||||
return roles.length() ? roles : fallback;
|
||||
}
|
||||
Reference in New Issue
Block a user