void srjson_DestroyDoc(srjson_doc_t *doc) { if(!doc) return; srjson_Delete(doc, doc->root); memset(doc, 0, sizeof(srjson_doc_t)); }
void srjson_DeleteDoc(srjson_doc_t *doc) { void (*f) (void *); if(!doc) return; srjson_Delete(doc, doc->root); f = doc->free_fn; f(doc); }
/* Parse an object - create a new root, and populate. */ srjson_t *srjson_Parse(srjson_doc_t *doc, const char *value) { srjson_t *c = srjson_New_Item(doc); ep = 0; if (!c) return 0; /* memory fail */ if (!parse_value(doc, c, skip(value))) { srjson_Delete(doc, c); return 0; } return c; }
/* Delete a srjson structure. */ void srjson_Delete(srjson_doc_t *doc, srjson_t *c) { srjson_t *next; while (c) { next = c->next; if (!(c->type & srjson_IsReference) && c->child) srjson_Delete(doc, c->child); if (!(c->type & srjson_IsReference) && c->valuestring) doc->free_fn(c->valuestring); if (c->string) doc->free_fn(c->string); doc->free_fn(c); c = next; } }
/* Replace array/object items with new ones. */ void srjson_ReplaceItemInArray(srjson_doc_t *doc, srjson_t *array, int which, srjson_t *newitem) { srjson_t *c = array->child; while (c && which > 0) c = c->next, which--; if (!c) return; newitem->next = c->next; newitem->prev = c->prev; if (newitem->next) newitem->next->prev = newitem; if (c == array->child) array->child = newitem; else newitem->prev->next = newitem; c->next = c->prev = 0; srjson_Delete(doc, c); }
int cfgt_get_json(struct sip_msg* msg, unsigned int mask, srjson_doc_t *jdoc, srjson_t *head) { int i; pv_value_t value; pv_cache_t **_pv_cache = pv_cache_get_table(); pv_cache_t *el = NULL; srjson_t *jobj = NULL; str item_name = STR_NULL; static char iname[128]; if(_pv_cache==NULL) { LM_ERR("cannot access pv_cache\n"); return -1; } if(jdoc==NULL){ LM_ERR("jdoc is null\n"); return -1; } if(head==NULL){ LM_ERR("head is null\n"); return -1; } memset(_cfgt_xavp_dump, 0, sizeof(str*)*CFGT_XAVP_DUMP_SIZE); for(i=0;i<PV_CACHE_SIZE;i++) { el = _pv_cache[i]; while(el) { if(!(el->spec.type==PVT_AVP|| el->spec.type==PVT_SCRIPTVAR|| el->spec.type==PVT_XAVP|| el->spec.type==PVT_OTHER)|| !((el->spec.type==PVT_AVP&&mask&CFGT_DP_AVP)|| (el->spec.type==PVT_XAVP&&mask&CFGT_DP_XAVP)|| (el->spec.type==PVT_SCRIPTVAR&&mask&CFGT_DP_SCRIPTVAR)|| (el->spec.type==PVT_OTHER&&mask&CFGT_DP_OTHER))|| (el->spec.trans!=NULL)) { el = el->next; continue; } jobj = NULL; item_name.len = 0; item_name.s = 0; iname[0] = '\0'; if(el->spec.type==PVT_AVP) { if(el->spec.pvp.pvi.type==PV_IDX_ALL|| (el->spec.pvp.pvi.type==PV_IDX_INT&&el->spec.pvp.pvi.u.ival!=0)) { el = el->next; continue; } else { if(_cfgt_get_array_avp_vals(msg, &el->spec.pvp, jdoc, &jobj, &item_name)!=0) { LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s); el = el->next; continue; } if(srjson_GetArraySize(jdoc, jobj)==0 && !(mask&CFGT_DP_NULL)) { el = el->next; continue; } snprintf(iname, 128, "$avp(%.*s)", item_name.len, item_name.s); } } else if(el->spec.type==PVT_XAVP) { if(_cfgt_xavp_dump_lookup(&el->spec.pvp)!=0) { el = el->next; continue; } if(_cfgt_get_obj_xavp_vals(msg, &el->spec.pvp, jdoc, &jobj, &item_name)!=0) { LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s); el = el->next; continue; } if(srjson_GetArraySize(jdoc, jobj)==0 && !(mask&CFGT_DP_NULL)) { el = el->next; continue; } snprintf(iname, 128, "$xavp(%.*s)", item_name.len, item_name.s); } else { if(pv_get_spec_value(msg, &el->spec, &value)!=0) { LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s); el = el->next; continue; } if(value.flags&(PV_VAL_NULL|PV_VAL_EMPTY|PV_VAL_NONE)) { if(mask&CFGT_DP_NULL) { jobj = srjson_CreateNull(jdoc); } else { el = el->next; continue; } }else if(value.flags&(PV_VAL_INT)){ jobj = srjson_CreateNumber(jdoc, value.ri); }else if(value.flags&(PV_VAL_STR)){ jobj = srjson_CreateStr(jdoc, value.rs.s, value.rs.len); }else { LM_WARN("el->pvname[%.*s] value[%d] unhandled\n", el->pvname.len, el->pvname.s, value.flags); el = el->next; continue; } if(jobj==NULL) { LM_ERR("el->pvname[%.*s] empty json object\n", el->pvname.len, el->pvname.s); goto error; } snprintf(iname, 128, "%.*s", el->pvname.len, el->pvname.s); } if(jobj!=NULL) { srjson_AddItemToObject(jdoc, head, iname, jobj); } el = el->next; } } return 0; error: srjson_Delete(jdoc, head); return -1; }
/** * json serialization of dialog profiles */ int dlg_profiles_to_json(dlg_cell_t *dlg, srjson_doc_t *jdoc) { dlg_profile_link_t *l; srjson_t *aj = NULL; srjson_t *pj = NULL; LM_DBG("serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); if(dlg==NULL || dlg->profile_links==NULL) return -1; LM_DBG("start of serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); for (l = dlg->profile_links ; l ; l=l->next) { if(aj==NULL) { aj = srjson_CreateArray(jdoc); if(aj==NULL) { LM_ERR("cannot create json profiles array object\n"); goto error; } } pj = srjson_CreateObject(jdoc); if(pj==NULL) { LM_ERR("cannot create json dynamic profiles obj\n"); goto error; } srjson_AddStrStrToObject(jdoc, pj, "name", 4, l->profile->name.s, l->profile->name.len); if(l->profile->has_value) { srjson_AddStrStrToObject(jdoc, pj, "value", 5, l->hash_linker.value.s, l->hash_linker.value.len); } if(l->hash_linker.puid[0]!='\0') srjson_AddStringToObject(jdoc, pj, "puid", l->hash_linker.puid); if(l->hash_linker.expires!=0) srjson_AddNumberToObject(jdoc, pj, "expires", l->hash_linker.expires); if(l->hash_linker.flags!=0) srjson_AddNumberToObject(jdoc, pj, "flags", l->hash_linker.flags); srjson_AddItemToArray(jdoc, aj, pj); } if(jdoc->root==NULL) { jdoc->root = srjson_CreateObject(jdoc); if(jdoc->root==NULL) { LM_ERR("cannot create json root\n"); goto error; } } if(aj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "profiles", aj); if(jdoc->buf.s != NULL) { jdoc->free_fn(jdoc->buf.s); jdoc->buf.s = NULL; jdoc->buf.len = 0; } jdoc->buf.s = srjson_PrintUnformatted(jdoc, jdoc->root); if(jdoc->buf.s!=NULL) { jdoc->buf.len = strlen(jdoc->buf.s); LM_DBG("serialized profiles for dlg[%u:%u] = [[%.*s]]\n", dlg->h_entry, dlg->h_id, jdoc->buf.len, jdoc->buf.s); return 0; } return -1; error: srjson_Delete(jdoc, aj); return -1; }
/** * json serialization of dialog profiles */ int dlg_profiles_to_json(dlg_cell_t *dlg, srjson_doc_t *jdoc) { dlg_profile_link_t *l; srjson_t *sj = NULL; srjson_t *dj = NULL; LM_DBG("serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); if(dlg==NULL || dlg->profile_links==NULL) return -1; LM_DBG("start of serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); for (l = dlg->profile_links ; l ; l=l->next) { if(l->profile->has_value) { if(dj==NULL) { dj = srjson_CreateObject(jdoc); if(dj==NULL) { LM_ERR("cannot create json dynamic profiles obj\n"); goto error; } } srjson_AddStrStrToObject(jdoc, dj, l->profile->name.s, l->profile->name.len, l->hash_linker.value.s, l->hash_linker.value.len); } else { if(sj==NULL) { sj = srjson_CreateArray(jdoc); if(sj==NULL) { LM_ERR("cannot create json static profiles obj\n"); goto error; } } srjson_AddItemToArray(jdoc, sj, srjson_CreateStr(jdoc, l->profile->name.s, l->profile->name.len)); } } if(jdoc->root==NULL) { jdoc->root = srjson_CreateObject(jdoc); if(jdoc->root==NULL) { LM_ERR("cannot create json root\n"); goto error; } } if(dj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "dprofiles", dj); if(sj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "sprofiles", sj); if(jdoc->buf.s != NULL) { jdoc->free_fn(jdoc->buf.s); jdoc->buf.s = NULL; jdoc->buf.len = 0; } jdoc->buf.s = srjson_PrintUnformatted(jdoc, jdoc->root); if(jdoc->buf.s!=NULL) { jdoc->buf.len = strlen(jdoc->buf.s); LM_DBG("serialized profiles for dlg[%u:%u] = [[%.*s]]\n", dlg->h_entry, dlg->h_id, jdoc->buf.len, jdoc->buf.s); return 0; } return -1; error: srjson_Delete(jdoc, dj); srjson_Delete(jdoc, sj); return -1; }
void srjson_DeleteItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string) { srjson_Delete(doc, srjson_DetachItemFromObject(doc, object, string)); }
void srjson_DeleteItemFromArray(srjson_doc_t *doc, srjson_t *array, int which) { srjson_Delete(doc, srjson_DetachItemFromArray(doc, array, which)); }