From 27c2d2d0c982e69eeab3a0e770ebeed7493392d1 Mon Sep 17 00:00:00 2001
From: Darrell
Date: Sat, 20 Jan 2024 20:13:17 -0500
Subject: [PATCH] Ensure json is formatted correctly (#5)
---
.gitignore | 1 +
src/AsyncWiFiSettings.cpp | 41 ++++++++++++++++++++++++++++++---------
2 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/.gitignore b/.gitignore
index 86b10c5..2f0a3be 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ node_modules
cmake-build-*
CMakeLists.txt
CMakeListsPrivate.txt
+.DS_Store
diff --git a/src/AsyncWiFiSettings.cpp b/src/AsyncWiFiSettings.cpp
index 8d2992b..5c34ceb 100644
--- a/src/AsyncWiFiSettings.cpp
+++ b/src/AsyncWiFiSettings.cpp
@@ -69,6 +69,29 @@ namespace { // Helpers
return r;
}
+ String json_encode(const String &raw) {
+ String r;
+ for (unsigned int i = 0; i < raw.length(); i++) {
+ char c = raw.charAt(i);
+ switch (c) {
+ case '\"': r += "\\\""; break;
+ case '\\': r += "\\\\"; break;
+ case '\b': r += "\\b"; break;
+ case '\f': r += "\\f"; break;
+ case '\n': r += "\\n"; break;
+ case '\r': r += "\\r"; break;
+ case '\t': r += "\\t"; break;
+ default:
+ if (c < ' ' || c > '~') {
+ r += Sprintf("\\u%04x", c);
+ } else {
+ r += c;
+ }
+ }
+ }
+ return r;
+ }
+
struct AsyncWiFiSettingsParameter {
String name;
String label;
@@ -102,8 +125,8 @@ namespace { // Helpers
String json() {
if (value == "") return "";
String j = F("\"{name}\":\"{value}\"");
- j.replace("{name}", name);
- j.replace("{value}", value);
+ j.replace("{name}", json_encode(name));
+ j.replace("{value}", json_encode(value));
return j;
}
@@ -147,8 +170,8 @@ namespace { // Helpers
String json() {
if (value == "") return "";
String j = F("\"{name}\":\"{value}\"");
- j.replace("{name}", name);
- j.replace("{value}", value);
+ j.replace("{name}", json_encode(name));
+ j.replace("{value}", json_encode(value));
return j;
}
@@ -189,8 +212,8 @@ namespace { // Helpers
String json() {
if (value == "") return "";
String j = F("\"{name}\":\"{value}\"");
- j.replace("{name}", name);
- j.replace("{value}", value);
+ j.replace("{name}", json_encode(name));
+ j.replace("{value}", String(value.toInt()));
return j;
}
@@ -213,8 +236,8 @@ namespace { // Helpers
String json() {
if (value == "") return "";
String j = F("\"{name}\":{value}");
- j.replace("{name}", name);
- j.replace("{value}", value);
+ j.replace("{name}", json_encode(name));
+ j.replace("{value}", String(value.toFloat()));
return j;
}
@@ -237,7 +260,7 @@ namespace { // Helpers
String json() {
if (value == "") return "";
String j = F("\"{name}\":{value}");
- j.replace("{name}", name);
+ j.replace("{name}", json_encode(name));
j.replace("{value}", value.toInt() ? "true" : "false");
return j;
}