2022-08-31 11:54:14 -05:00
|
|
|
// vi:ft=c
|
|
|
|
#ifndef JSON_H_
|
|
|
|
#define JSON_H_
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
2022-08-31 11:59:06 -05:00
|
|
|
#include <stdint.h>
|
2022-08-31 11:54:14 -05:00
|
|
|
|
|
|
|
typedef struct __attribute__((packed, aligned(8))) {
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t len;
|
|
|
|
} JSONHeader;
|
|
|
|
|
|
|
|
_Static_assert(sizeof(JSONHeader) == 8, "JSONHeader size isn't 8 bytes");
|
|
|
|
_Static_assert(sizeof(double) == 8, "double size isn't 8 bytes");
|
|
|
|
_Static_assert(CHAR_BIT / sizeof(char) == 8, "Byte isn't 8 bit");
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
String = 1,
|
|
|
|
Number = 2,
|
|
|
|
Object = 3,
|
|
|
|
Array = 4,
|
|
|
|
Boolean = 5,
|
|
|
|
Null = 6,
|
|
|
|
} JSONType;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
NoError = 0,
|
|
|
|
DstOverflow = 1,
|
|
|
|
SrcOverflow = 2,
|
|
|
|
BadKeyword = 3,
|
|
|
|
BadChar = 4,
|
|
|
|
StringBadChar = 5,
|
|
|
|
StringBadUnicode = 6,
|
|
|
|
StringBadEscape = 7,
|
|
|
|
NumberBadChar = 8,
|
|
|
|
ObjectBadChar = 9,
|
|
|
|
JERRORNO_MAX = 10
|
|
|
|
} JSONError;
|
|
|
|
|
|
|
|
#ifdef JSON_C_
|
2022-08-31 11:59:06 -05:00
|
|
|
static const char *JSONErrorMessage[JERRORNO_MAX + 1] = {
|
2022-08-31 11:54:14 -05:00
|
|
|
"No error",
|
|
|
|
"Destination buffer is not big enough",
|
|
|
|
"Source buffer overflowed before parsing finished",
|
|
|
|
"Unknown keyword",
|
|
|
|
"Unexpected character",
|
|
|
|
"Unexpected character in string",
|
|
|
|
"Bad unicoded escape in string",
|
|
|
|
"Illegal escape in string",
|
|
|
|
"Unexpected character in number",
|
|
|
|
"Unexpected character in object",
|
2022-08-31 11:59:06 -05:00
|
|
|
"?",
|
2022-08-31 11:54:14 -05:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-08-31 16:24:26 -05:00
|
|
|
// See client.c for usage of adapters
|
2022-08-31 11:54:14 -05:00
|
|
|
typedef struct {
|
2022-08-31 11:59:06 -05:00
|
|
|
char *path;
|
2022-08-31 11:54:14 -05:00
|
|
|
JSONType type;
|
2022-08-31 11:59:06 -05:00
|
|
|
size_t offset;
|
2022-08-31 11:54:14 -05:00
|
|
|
} JSONAdapter;
|
|
|
|
|
2022-08-31 11:59:06 -05:00
|
|
|
void json_adapt(uint8_t *buf, JSONAdapter *adapters, size_t adapter_count, void *ptr);
|
|
|
|
int json_parse(const char *src, size_t src_len, uint8_t *dst, size_t dst_len);
|
2022-08-31 16:24:26 -05:00
|
|
|
void json_print_value(uint8_t *buf);
|
2022-08-31 11:59:06 -05:00
|
|
|
const char *json_strerr();
|
2022-08-31 16:24:26 -05:00
|
|
|
size_t json_errloc();
|
|
|
|
JSONError json_errno();
|
2022-08-31 11:54:14 -05:00
|
|
|
|
|
|
|
#endif
|