src/cx/json.h

changeset 1338
cc153bffea28
parent 1337
6dfa1eb58ce3
--- a/src/cx/json.h	Thu Aug 14 23:03:01 2025 +0200
+++ b/src/cx/json.h	Fri Aug 15 17:42:01 2025 +0200
@@ -1341,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"
 
@@ -1356,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
 /**
@@ -1397,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

mercurial