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_set_json (struct sip_msg* msg, pv_param_t* pvp, int flag , pv_value_t* val) { json_t * obj; enum json_tokener_error parse_status; if( expand_tag_list( msg, ((json_name *)pvp->pvn.u.dname)->tags ) < 0) { LM_ERR("Cannot expand variables in path\n"); return -1; } /* delete value */ if( val == NULL) { return pv_add_json(pvp,NULL); } /* If we want the value to be interpreted prepare the object */ if( flag == COLONEQ_T ) { if( ! (val->flags & PV_VAL_STR) ) { LM_ERR("Trying to interpret a non-string value\n"); return -1; } obj = json_parse( val->rs.s, val->rs.len,&parse_status); if (obj == NULL) { LM_ERR("Error parsing json: %s\n", #if JSON_C_VERSION_NUM >= JSON_C_VERSION_010 json_tokener_error_desc(parse_status) #else json_tokener_errors[(unsigned long)obj] #endif ); pv_add_json(pvp, NULL); return -1; } } else { if( val->flags & PV_VAL_INT ) { obj = json_object_new_int(val->ri); } else { obj = json_object_new_string_len( val->rs.s, val->rs.len); } } return pv_add_json(pvp,obj); }
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; }