struct json *json_object_create(void);
void json_object_put(struct json *, const char *name, struct json *value);
+void json_object_put_nocopy(struct json *, char *name, struct json *value);
void json_object_put_string(struct json *,
const char *name, const char *value);
void json_object_put_format(struct json *,
bool shash_add_once(struct shash *, const char *, const void *);
void shash_add_assert(struct shash *, const char *, const void *);
void *shash_replace(struct shash *, const char *, const void *data);
+void *shash_replace_nocopy(struct shash *, char *name, const void *data);
void shash_delete(struct shash *, struct shash_node *);
char *shash_steal(struct shash *, struct shash_node *);
struct shash_node *shash_find(const struct shash *, const char *);
json_destroy(shash_replace(json->u.object, name, value));
}
+void
+json_object_put_nocopy(struct json *json, char *name, struct json *value)
+{
+ json_destroy(shash_replace_nocopy(json->u.object, name, value));
+}
+
void
json_object_put_string(struct json *json, const char *name, const char *value)
{
{
struct json_parser_node *node = json_parser_top(p);
if (node->json->type == JSON_OBJECT) {
- json_object_put(node->json, p->member_name, value);
- free(p->member_name);
+ json_object_put_nocopy(node->json, p->member_name, value);
p->member_name = NULL;
} else if (node->json->type == JSON_ARRAY) {
json_array_add(node->json, value);
}
}
+/* Searches for 'name' in 'sh'. If it does not already exist, adds it along
+ * with 'data' and returns NULL. If it does already exist, replaces its data
+ * by 'data' and returns the data that it formerly contained.
+ *
+ * Takes ownership of 'name'. */
+void *
+shash_replace_nocopy(struct shash *sh, char *name, const void *data)
+{
+ size_t hash = hash_name(name);
+ struct shash_node *node;
+
+ node = shash_find__(sh, name, strlen(name), hash);
+ if (!node) {
+ shash_add_nocopy__(sh, name, data, hash);
+ return NULL;
+ } else {
+ free(name);
+ void *old_data = node->data;
+ node->data = CONST_CAST(void *, data);
+ return old_data;
+ }
+}
+
/* Deletes 'node' from 'sh' and frees the node's name. The caller is still
* responsible for freeing the node's data, if necessary. */
void