Files
Crosspoint/lib/JsonParser/ReleaseJsonParser.h
Zach Nelson aa7a31b3db fix: Read GH release JSON as stream in OTA updater (#1810)
## Summary

The GitHub recent release API returns lots of data, including the full
release notes. For 1.2.0, that data is 30,530 bytes. The existing
OtaUpdater HTTP handler and single-buffer JSON parsing can't reliably
handle that much data in the constrained ESP32 environment.

This change does a few things:
1. Adds a very simple lib/JsonParser/StreamingJsonParser.cpp with
SAX-style callbacks to read JSON data incrementally.
2. Adds a very simple lib/JsonParser/ReleaseJsonParser.cpp building on
StreamingJsonParser, which specifically parses the GitHub release JSON
for the release version, URL, and size.
3. Updates OtaUpdater.cpp to use an instance of ReleaseJsonParser to
incrementally parse the large response it may receive from GitHub.

Building from this commit while overriding my local version to 1.1.9, I
was able to run the OTA update process ~5 times in a row successfully.

Fixes #1561 (second part, after #1805).

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**YES**_
2026-05-03 20:48:06 -05:00

69 lines
1.5 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include "StreamingJsonParser.h"
class ReleaseJsonParser {
public:
ReleaseJsonParser();
ReleaseJsonParser(const ReleaseJsonParser&) = delete;
ReleaseJsonParser& operator=(const ReleaseJsonParser&) = delete;
void reset();
void feed(const char* data, size_t len);
bool foundTag() const;
bool foundFirmware() const;
const char* getTagName() const;
const char* getFirmwareUrl() const;
size_t getFirmwareSize() const;
private:
enum class Position : uint8_t {
TOP_LEVEL,
IN_ASSETS_ARRAY,
IN_ASSET_OBJECT,
};
enum class LastKey : uint8_t {
NONE,
TAG_NAME,
ASSETS,
ASSET_NAME,
ASSET_URL,
ASSET_SIZE,
};
static void sOnKey(void* ctx, const char* key, size_t len);
static void sOnString(void* ctx, const char* value, size_t len);
static void sOnNumber(void* ctx, const char* value, size_t len);
static void sOnBool(void* ctx, bool value);
static void sOnNull(void* ctx);
static void sOnObjectStart(void* ctx);
static void sOnObjectEnd(void* ctx);
static void sOnArrayStart(void* ctx);
static void sOnArrayEnd(void* ctx);
void commitAsset();
StreamingJsonParser parser;
Position position;
LastKey lastKey;
uint8_t depth;
uint8_t assetDepth;
char tagName[32];
char firmwareUrl[512];
size_t firmwareSize;
bool tagFound;
bool firmwareFound;
char currentAssetName[32];
char currentAssetUrl[512];
size_t currentAssetSize;
};