intjson_object_object_add(struct json_object* jso,constchar*key,struct json_object *val){returnjson_object_object_add_ex(jso, key, val,0);}intjson_object_object_add_ex(struct json_object* jso,constchar*const key,struct json_object *const val,constunsigned opts){struct json_object *existing_value =NULL;struct lh_entry *existing_entry;unsignedlong hash;assert(json_object_get_type(jso)== json_type_object);// We lookup the entry and replace the value, rather than just deleting// and re-adding it, so the existing key remains valid.
hash =lh_get_hash(jso->o.c_object,(constvoid*)key);
existing_entry =(opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)?NULL:lh_table_lookup_entry_w_hash(jso->o.c_object,(constvoid*)key, hash);// The caller must avoid creating loops in the object tree, but do a// quick check anyway to make sure we're not creating a trivial loop.if(jso == val)return-1;if(!existing_entry){constvoid*const k =(opts & JSON_C_OBJECT_KEY_IS_CONSTANT)?(constvoid*)key :strdup(key);if(k ==NULL)return-1;returnlh_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;return0;}
constchar*json_object_get_string(struct json_object *jso){if(!jso)returnNULL;switch(jso->o_type){case json_type_string:returnget_string_component(jso);default:returnjson_object_to_json_string(jso);}}/* helper for accessing the optimized string data component in json_object
*/staticconstchar*get_string_component(conststruct json_object *jso){return(jso->o.c_string.len < LEN_DIRECT_STRING_DATA)?
jso->o.c_string.str.data : jso->o.c_string.str.ptr;}/* backwards-compatible conversion to string */constchar*json_object_to_json_string(struct json_object *jso){returnjson_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);}constchar*json_object_to_json_string_ext(struct json_object *jso,int flags){returnjson_object_to_json_string_length(jso, flags,NULL);}/* extended conversion to string */constchar*json_object_to_json_string_length(struct json_object *jso,int flags, size_t *length){constchar*r =NULL;
size_t s =0;if(!jso){
s =4;
r ="null";}elseif((jso->_pb)||(jso->_pb =printbuf_new())){printbuf_reset(jso->_pb);if(jso->_to_json_string(jso, jso->_pb,0, flags)>=0){
s =(size_t)jso->_pb->bpos;
r = jso->_pb->buf;}}if(length)*length = s;return r;}