SimpleOTA client v1.1.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_json.h File Reference

Minimal flat-JSON value extraction for SimpleOTA responses. More...

Go to the source code of this file.

Functions

bool sota_json_get_string (const char *json, const char *key, char *out, size_t out_cap)
 Extract a string value.
 
bool sota_json_escape (const char *src, char *out, size_t out_cap)
 Write src as the inside of a JSON string: " and \ are escaped, newline, carriage return and tab become escapes, and other control characters are dropped.
 
bool sota_json_is_object (const char *s)
 True when s is one JSON object whose brackets pair up and whose strings close, nested at most 8 deep, with nothing after it: something that can go into a request body as a member's value without breaking out of it.
 
bool sota_json_has_key (const char *json, const char *key)
 True when the key is present, whatever its value.
 
bool sota_json_get_bool (const char *json, const char *key, bool *out)
 Extract true/false.
 
bool sota_json_get_u32 (const char *json, const char *key, uint32_t *out)
 Extract a non-negative integer (digits only, as the server sends).
 

Detailed Description

Minimal flat-JSON value extraction for SimpleOTA responses.

Finds a "key" among the top-level object's members and reads its string / bool / unsigned value. Keys inside nested objects and arrays are not matched, so a nested field cannot stand in for a top-level one. This is NOT a general JSON parser; it is a C port of the hardware-proven scanner the Arduino client has used since its first release. Chosen over cJSON because the bundled json component left the ESP-IDF core in IDF 6, and a vendored copy would risk duplicate symbols in apps that use cJSON themselves.

Portable (no platform headers): host-tested in test/host/.

Function Documentation

◆ sota_json_get_string()

bool sota_json_get_string ( const char *  json,
const char *  key,
char *  out,
size_t  out_cap 
)

Extract a string value.

Decodes \n, \t, \r, \b, \f and \uXXXX (as UTF-8, a surrogate pair as one character); any other backslash escape yields the character after it.

Returns
true only when the key exists, is a string with its closing quote, AND the decoded value fits in out_cap-1 bytes. An oversized value, one cut off by the end of the text, and a \uXXXX that is malformed, U+0000 or a surrogate outside a pair all fail loudly rather than yield part of a value, so a clipped URL can never half-work.

◆ sota_json_escape()

bool sota_json_escape ( const char *  src,
char *  out,
size_t  out_cap 
)

Write src as the inside of a JSON string: " and \ are escaped, newline, carriage return and tab become escapes, and other control characters are dropped.

Returns
true when the result and its terminating NUL fit in out_cap.

◆ sota_json_is_object()

bool sota_json_is_object ( const char *  s)

True when s is one JSON object whose brackets pair up and whose strings close, nested at most 8 deep, with nothing after it: something that can go into a request body as a member's value without breaking out of it.

Not a full validator.