I am trying to use json module in script
with json_get_field exported api, I found in it's
implementation, the api will call struct json_object *j =
json_tokener_parse(json_s.s); but didn't call
json_object_put for this j object, then the object will
leak. So my question is how to release the new j object?
We can't release in json_get_field(), that will cause the
returned string become to rubbish, thanks!
int json_get_field(struct sip_msg* msg, char* json,
char* field, char* dst)
{
str json_s;
str field_s;
pv_spec_t *dst_pv;
pv_value_t dst_val;
if (fixup_get_svalue(msg, (gparam_p)json,
&json_s) != 0) {
LM_ERR("cannot get json string value\n");
return -1;
}
if (fixup_get_svalue(msg, (gparam_p)field,
&field_s) != 0) {
LM_ERR("cannot get field string value\n");
return -1;
}
dst_pv = (pv_spec_t *)dst;
struct json_object *j =
json_tokener_parse(json_s.s);
if (is_error(j)) {
LM_ERR("empty or invalid JSON\n");
return -1;
}
char *value =
(char*)json_object_to_json_string(json_object_object_get(j,
field_s.s));
dst_val.rs.s = value;
dst_val.rs.len = strlen(value);
dst_val.flags = PV_VAL_STR;
dst_pv->setf(msg, &dst_pv->pvp,
(int)EQ_T, &dst_val);
return 1;
}