Files
Crosspoint/lib/JsonParser/StreamingJsonParser.h
T
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

74 lines
1.6 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
struct JsonCallbacks {
void* ctx;
void (*onKey)(void* ctx, const char* key, size_t len);
void (*onString)(void* ctx, const char* value, size_t len);
void (*onNumber)(void* ctx, const char* value, size_t len);
void (*onBool)(void* ctx, bool value);
void (*onNull)(void* ctx);
void (*onObjectStart)(void* ctx);
void (*onObjectEnd)(void* ctx);
void (*onArrayStart)(void* ctx);
void (*onArrayEnd)(void* ctx);
};
class StreamingJsonParser {
public:
static constexpr size_t TOKEN_BUF_SIZE = 512;
static constexpr size_t MAX_NESTING = 32;
explicit StreamingJsonParser(const JsonCallbacks& callbacks);
void reset();
void feed(const char* data, size_t len);
bool hasError() const { return error; }
private:
enum class State : uint8_t {
SCANNING,
IN_STRING_KEY,
IN_STRING_VALUE,
IN_NUMBER,
IN_LITERAL,
SKIP_STRING,
};
enum class Container : uint8_t {
NONE,
OBJECT,
ARRAY,
};
void handleScanning(char c);
void handleStringChar(char c);
void handleNumber(char c);
void handleLiteral(char c);
void handleSkipString(char c);
void appendToken(char c);
void emitToken();
bool inArray() const { return nestingDepth > 0 && nestingStack[nestingDepth - 1] == Container::ARRAY; }
JsonCallbacks cb;
char tokenBuf[TOKEN_BUF_SIZE];
size_t tokenLen;
State state;
bool expectingValue;
bool escaped;
bool tokenOverflow;
bool error;
Container nestingStack[MAX_NESTING];
uint8_t nestingDepth;
char literalExpected[6];
uint8_t literalLen;
uint8_t literalPos;
};