diff -r e8f354a25ac8 -r 68754c7de906 src/cx/json.h --- a/src/cx/json.h Thu Nov 07 20:22:56 2024 +0100 +++ b/src/cx/json.h Thu Nov 07 22:46:58 2024 +0100 @@ -173,13 +173,14 @@ // TODO: add support for CxAllocator -__attribute__((__nonnull__)) +cx_attr_nonnull void cxJsonInit(CxJson *json); -__attribute__((__nonnull__)) +cx_attr_nonnull void cxJsonDestroy(CxJson *json); -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_access_r(2, 3) int cxJsonFilln(CxJson *json, const char *buf, size_t len); #define cxJsonFill(json, str) _Generic((str), \ @@ -189,7 +190,7 @@ const char*: cx_json_fill_str) \ (json, str) -__attribute__((__nonnull__)) +cx_attr_nonnull static inline int cx_json_fill_cxstr( CxJson *json, cxstring str @@ -197,7 +198,7 @@ return cxJsonFilln(json, str.ptr, str.length); } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline int cx_json_fill_mutstr( CxJson *json, cxmutstr str @@ -205,7 +206,8 @@ return cxJsonFilln(json, str.ptr, str.length); } -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_cstr_arg(2) static inline int cx_json_fill_str( CxJson *json, const char *str @@ -213,78 +215,79 @@ return cxJsonFilln(json, str, strlen(str)); } +void cxJsonValueFree(CxJsonValue *value); -__attribute__((__nonnull__)) +// TODO: if the CxJsonValue was a returned value, we could reference cxJsonValueFree() as deallocator +cx_attr_nonnull int cxJsonNext(CxJson *json, CxJsonValue **value); -void cxJsonValueFree(CxJsonValue *value); - -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsObject(CxJsonValue *value) { return value->type == CX_JSON_OBJECT; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsArray(CxJsonValue *value) { return value->type == CX_JSON_ARRAY; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsString(CxJsonValue *value) { return value->type == CX_JSON_STRING; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsNumber(CxJsonValue *value) { return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsInteger(CxJsonValue *value) { return value->type == CX_JSON_INTEGER; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsLiteral(CxJsonValue *value) { return value->type == CX_JSON_LITERAL; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsBool(CxJsonValue *value) { return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsTrue(CxJsonValue *value) { return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsFalse(CxJsonValue *value) { return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonIsNull(CxJsonValue *value) { return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; } -__attribute__((__nonnull__, __returns_nonnull__)) +cx_attr_nonnull +cx_attr_returns_nonnull static inline char *cxJsonAsString(CxJsonValue *value) { return value->value.string.ptr; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline cxstring cxJsonAsCxString(CxJsonValue *value) { return cx_strcast(value->value.string); } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) { return value->value.string; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline double cxJsonAsDouble(CxJsonValue *value) { if (value->type == CX_JSON_INTEGER) { return (double) value->value.integer; @@ -293,7 +296,7 @@ } } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline int64_t cxJsonAsInteger(CxJsonValue *value) { if (value->type == CX_JSON_INTEGER) { return value->value.integer; @@ -302,23 +305,26 @@ } } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline bool cxJsonAsBool(CxJsonValue *value) { return value->value.literal == CX_JSON_TRUE; } -__attribute__((__nonnull__)) +cx_attr_nonnull static inline size_t cxJsonArrSize(CxJsonValue *value) { return value->value.array.array_size; } -__attribute__((__nonnull__, __returns_nonnull__)) +cx_attr_nonnull +cx_attr_returns_nonnull CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index); // TODO: add cxJsonArrIter() // TODO: implement cxJsonObjGet as a _Generic with support for cxstring -__attribute__((__nonnull__, __returns_nonnull__)) +cx_attr_nonnull +cx_attr_returns_nonnull +cx_attr_cstr_arg(2) CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char* name); #ifdef __cplusplus