--- a/ucx/cx/json.h Tue Sep 09 16:01:30 2025 +0200 +++ b/ucx/cx/json.h Tue Sep 09 20:56:47 2025 +0200 @@ -1284,6 +1284,23 @@ CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index); /** + * Removes an element from a JSON array. + * + * If the @p value is not a JSON array, the behavior is undefined. + * + * This function, in contrast to cxJsonArrayGet(), returns @c NULL + * when the index is out of bounds. + * + * @param value the JSON value + * @param index the index in the array + * @return the removed value from the specified index or @c NULL when the index was out of bounds + * @see cxJsonIsArray() + */ +cx_attr_nonnull +cx_attr_export +CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index); + +/** * Returns an iterator over the JSON array elements. * * The iterator yields values of type @c CxJsonValue* . @@ -1324,6 +1341,13 @@ cx_attr_export CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name); +/** + * @copydoc cxJsonObjRemove() + */ +cx_attr_nonnull +cx_attr_export +CxJsonValue *cx_json_obj_remove_cxstr(CxJsonValue *value, cxstring name); + #ifdef __cplusplus } // extern "C" @@ -1339,6 +1363,18 @@ return cx_json_obj_get_cxstr(value, cx_str(name)); } +static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, cxstring name) { + return cx_json_obj_remove_cxstr(value, name); +} + +static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, cxmutstr name) { + return cx_json_obj_remove_cxstr(value, cx_strcast(name)); +} + +static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, const char *name) { + return cx_json_obj_remove_cxstr(value, cx_str(name)); +} + extern "C" { #else /** @@ -1380,6 +1416,43 @@ static inline CxJsonValue *cx_json_obj_get_str(const CxJsonValue *value, const char *name) { return cx_json_obj_get_cxstr(value, cx_str(name)); } + +/** + * Removes and returns a value corresponding to a key in a JSON object. + * + * If the @p value is not a JSON object, the behavior is undefined. + * + * This function, in contrast to cxJsonObjGet() returns @c NULL when the + * object does not contain @p name. + * + * @param value the JSON object + * @param name the key to look up + * @return the value corresponding to the key or @c NULL when the key is not part of the object + * @see cxJsonIsObject() + */ +#define cxJsonObjRemove(value, name) _Generic((name), \ + cxstring: cx_json_obj_remove_cxstr, \ + cxmutstr: cx_json_obj_remove_mutstr, \ + char*: cx_json_obj_remove_str, \ + const char*: cx_json_obj_remove_str) \ + (value, name) + +/** + * @copydoc cxJsonObjRemove() + */ +cx_attr_nonnull +static inline CxJsonValue *cx_json_obj_remove_mutstr(CxJsonValue *value, cxmutstr name) { + return cx_json_obj_remove_cxstr(value, cx_strcast(name)); +} + +/** + * @copydoc cxJsonObjRemove() + */ +cx_attr_nonnull +cx_attr_cstr_arg(2) +static inline CxJsonValue *cx_json_obj_remove_str(CxJsonValue *value, const char *name) { + return cx_json_obj_remove_cxstr(value, cx_str(name)); +} #endif #ifdef __cplusplus