if(iter.val == NULL)
sprintbuf(pb, "null");
else
- iter.val->_to_json_string(iter.val, pb, level+1,flags);
+ if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
+ return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
{
}
}
-void json_object_object_add_ex(struct json_object* jso,
+int json_object_object_add_ex(struct json_object* jso,
const char *const key,
struct json_object *const val,
const unsigned opts)
{
const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
(const void *)key : strdup(key);
- lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
- return;
+ if (k == NULL)
+ return -1;
+ return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
}
existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
+ return 0;
}
int json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
{
- // We lookup the entry and replace the value, rather than just deleting
- // and re-adding it, so the existing key remains valid.
- json_object *existing_value = NULL;
- struct lh_entry *existing_entry;
- const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
- existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object,
- (const void *)key, hash);
- if (!existing_entry)
- {
- char *keydup = strdup(key);
- if (keydup == NULL)
- return -1;
-
- return lh_table_insert_w_hash(jso->o.c_object, keydup, val, hash, 0);
- }
- existing_value = (json_object *)lh_entry_v(existing_entry);
- if (existing_value)
- json_object_put(existing_value);
- existing_entry->v = val;
-
- return 0;
+ return json_object_object_add_ex(jso, key, val, 0);
}
{
int had_children = 0;
size_t ii;
+
sprintbuf(pb, "[");
if (flags & JSON_C_TO_STRING_PRETTY)
sprintbuf(pb, "\n");
if(val == NULL)
sprintbuf(pb, "null");
else
- val->_to_json_string(val, pb, level+1, flags);
+ if (val->_to_json_string(val, pb, level+1, flags) < 0)
+ return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
{
* @param opts process-modifying options. To specify multiple options, use
* arithmetic or (OPT1|OPT2)
*/
-extern void json_object_object_add_ex(struct json_object* obj, const char *key,
+extern int json_object_object_add_ex(struct json_object* obj, const char *key,
struct json_object *val, const unsigned opts);
/** Get the json_object associate with a given object field.