Skip to content

Commit

Permalink
Fix: skip BOM in JSON files (pin_mapping and config)
Browse files Browse the repository at this point in the history
based on #2387
  • Loading branch information
tbnobody committed Nov 1, 2024
1 parent 6c903ab commit 55c98ef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <ArduinoJson.h>
#include <LittleFS.h>
#include <cstdint>

class Utils {
Expand All @@ -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);
};
1 change: 1 addition & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ bool ConfigurationClass::write()
bool ConfigurationClass::read()
{
File f = LittleFS.open(CONFIG_FILENAME, "r", false);
Utils::skipBom(f);

JsonDocument doc;

Expand Down
3 changes: 3 additions & 0 deletions src/PinMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "PinMapping.h"
#include "MessageOutput.h"
#include "Utils.h"
#include <ArduinoJson.h>
#include <LittleFS.h>
#include <string.h>
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 55c98ef

Please sign in to comment.