int json_bind(struct sip_msg* msg, pv_spec_t* dest, pv_spec_t* src) { pv_json_t * var ; json_t * obj; json_name * id ; pv_param_t *pvp; pvp = &src->pvp; id = (json_name *) pvp->pvn.u.dname; var = get_pv_json(pvp); if( var == NULL ) { LM_ERR("Variable named:%.*s not found\n",id->name.len,id->name.s); return -1; } obj = get_object(var, pvp, NULL, 0, 1); if( obj == NULL ) { LM_NOTICE("Could not find object with that path\n"); return -1; } json_object_get(obj); if( pv_add_json( &dest->pvp, obj ) ) return -1; return 1; };
int pv_get_json (struct sip_msg* msg, pv_param_t* pvp, pv_value_t* val) { pv_json_t * var ; json_t * obj; json_name * id = (json_name *) pvp->pvn.u.dname; UNUSED(id); if( expand_tag_list( msg, ((json_name *)pvp->pvn.u.dname)->tags ) < 0) { LM_ERR("Cannot expand variables in path\n"); return pv_get_null( msg, pvp, val); } var = get_pv_json(pvp); if( var == NULL ) { /* this is not an error - we simply came across a json spec * pointing a json var which was never set/init */ LM_DBG("Variable named:%.*s not found\n",id->name.len,id->name.s); return pv_get_null( msg, pvp, val); } obj = get_object(var, pvp, NULL, 0); memset(val, 0, sizeof(pv_value_t)); if( obj == NULL ) return pv_get_null( msg, pvp, val); if( json_object_is_type(obj, json_type_int) ) { val->rs.s = sint2str(json_object_get_int(obj), &val->rs.len); val->ri = json_object_get_int(obj);; val->flags |= PV_VAL_INT|PV_TYPE_INT|PV_VAL_STR; } else if( json_object_is_type(obj, json_type_string)) { val->flags = PV_VAL_STR; val->rs.s = (char*)json_object_get_string( obj ); #if JSON_LIB_VERSION >= 10 val->rs.len = json_object_get_string_len( obj ); #else val->rs.len = strlen(val->rs.s); #endif } else { val->flags = PV_VAL_STR; val->rs.s = (char*)json_object_to_json_string( obj ); val->rs.len = strlen(val->rs.s); } return 0; }
int pv_add_json ( pv_param_t* pvp, json_t * obj ) { json_t *dest; json_name * id; pv_json_t * var; json_tag * tag; int poz; id = (json_name *) pvp->pvn.u.dname; var = get_pv_json(pvp); if( var == NULL ) { if( id->tags ) { LM_ERR("Object is not initialized yet\n"); return -1; } var = (pv_json_t *) pkg_malloc(sizeof(pv_json_t)); if( var == NULL ) { LM_ERR("Out of memory\n"); return -1; } memset(var,0,sizeof(pv_json_t)); var->name = id->name; var->next = all; var->data = obj; all = var; return 0; } if( id ->tags == NULL) { if( var->data ) json_object_put(var->data); var->data = obj; return 0; } dest = get_object(var, pvp, &tag, 1, 1); if( dest == NULL ) { LM_NOTICE("Could not find object with that path\n"); return -1; } if( tag->type & TAG_KEY ) { memcpy(buff,tag->key.s,tag->key.len); buff[tag->key.len] = 0; if( obj == NULL ) json_object_object_del(dest,buff); else json_object_object_add(dest,buff,obj); } if( tag->type & TAG_IDX ) { poz = tag->idx; if( tag->type & TAG_END ) { if( obj == NULL) { LM_ERR("Invalid parameter for deletion\n"); return -1; } json_object_array_add(dest,obj); return 0; } if( poz < 0 ) poz += json_object_array_length(dest); if( poz<0 || poz >= json_object_array_length(dest)) { LM_ERR("Attempting to replace at invalid index in array:%d\n", poz); return -1; } if( obj == NULL) { if( poz >= json_object_array_length(dest)) { LM_ERR("Index out of bounds for deletion\n"); return -1; } json_object_array_del(dest,poz); } else json_object_array_put_idx(dest,poz,obj); } return 0; }
int pv_get_json_ext(struct sip_msg* msg, pv_param_t* pvp, pv_value_t* val, int flags) { pv_json_t * var ; json_t * obj; json_name * id = (json_name *) pvp->pvn.u.dname; UNUSED(id); if( expand_tag_list( msg, ((json_name *)pvp->pvn.u.dname)->tags ) < 0) { LM_ERR("Cannot expand variables in path\n"); return pv_get_null( msg, pvp, val); } var = get_pv_json(pvp); if( var == NULL ) { /* this is not an error - we simply came across a json spec * pointing a json var which was never set/init */ LM_DBG("Variable named:%.*s not found\n",id->name.len,id->name.s); return pv_get_null( msg, pvp, val); } obj = get_object(var, pvp, NULL, 0, 0); memset(val, 0, sizeof(pv_value_t)); if( obj == NULL ) return pv_get_null( msg, pvp, val); if (pvp->pvi.type == PV_IDX_INT) { if (pv_json_iterate(&obj, pvp, id, val) < 0) { LM_DBG("Failed to iterate\n"); return pv_get_null(msg, pvp, val); } if (val->flags == PV_VAL_STR || val->flags == PV_VAL_NULL) /* val is set */ return 0; /* else we got an object */ } else if (pvp->pvi.type == PV_IDX_ALL) { LM_ERR("\"[*]\" index only supported in for each statement\n"); return pv_get_null(msg, pvp, val); } if( json_object_is_type(obj, json_type_int) ) { val->rs.s = sint2str(json_object_get_int(obj), &val->rs.len); val->ri = json_object_get_int(obj);; val->flags |= PV_VAL_INT|PV_TYPE_INT|PV_VAL_STR; } else if( json_object_is_type(obj, json_type_string)) { val->flags = PV_VAL_STR; val->rs.s = (char*)json_object_get_string( obj ); #if JSON_C_VERSION_NUM >= JSON_C_VERSION_010 val->rs.len = json_object_get_string_len( obj ); #else val->rs.len = strlen(val->rs.s); #endif } else { val->flags = PV_VAL_STR; val->rs.s = (char*)json_object_to_json_string_ext( obj, flags); val->rs.len = strlen(val->rs.s); } return 0; }