diff --git a/include/Utils.h b/include/Utils.h index 759402b19..39994f323 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include class Utils { @@ -12,4 +13,5 @@ class Utils { static bool checkJsonAlloc(const JsonDocument& doc, const char* function, const uint16_t line); static void removeAllFiles(); static String generateMd5FromFile(String file); + static void skipBom(File& f); }; diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 04bcfa658..10fd92fd9 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -159,6 +159,7 @@ bool ConfigurationClass::write() bool ConfigurationClass::read() { File f = LittleFS.open(CONFIG_FILENAME, "r", false); + Utils::skipBom(f); JsonDocument doc; diff --git a/src/PinMapping.cpp b/src/PinMapping.cpp index bd5ea9952..1a721e041 100644 --- a/src/PinMapping.cpp +++ b/src/PinMapping.cpp @@ -4,6 +4,7 @@ */ #include "PinMapping.h" #include "MessageOutput.h" +#include "Utils.h" #include #include #include @@ -199,6 +200,8 @@ bool PinMappingClass::init(const String& deviceMapping) return false; } + Utils::skipBom(f); + JsonDocument doc; // Deserialize the JSON document DeserializationError error = deserializeJson(doc, f); diff --git a/src/Utils.cpp b/src/Utils.cpp index e5746079d..c567ec116 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -111,3 +111,15 @@ String Utils::generateMd5FromFile(String file) return md5.toString(); } + +void Utils::skipBom(File& f) +{ + // skip Byte Order Mask (BOM). valid JSON docs always start with '{' or '['. + while (f.available() > 0) { + int c = f.peek(); + if (c == '{' || c == '[') { + break; + } + f.read(); + } +}