void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
SWITCH_DECLARE(cJSON *)cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
/* Render a value to text. */ static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p) { char *out=0; if (!item) { return 0; } if (p) { switch ((item->type)&255) { case cJSON_NULL: { out=ensure(p,5); if (out) { strcpy(out,"null"); } break; } case cJSON_False: { out=ensure(p,6); if (out) { strcpy(out,"false"); } break; } case cJSON_True: { out=ensure(p,5); if (out) { strcpy(out,"true"); } break; } case cJSON_Number: out=print_number(item,p); break; case cJSON_String: out=print_string(item,p); break; case cJSON_Array: out=print_array(item,depth,fmt,p); break; case cJSON_Object: out=print_object(item,depth,fmt,p); break; } } else { switch ((item->type)&255) { case cJSON_NULL: out=cJSON_strdup("null"); break; case cJSON_False: out=cJSON_strdup("false"); break; case cJSON_True: out=cJSON_strdup("true"); break; case cJSON_Number: out=print_number(item,0); break; case cJSON_String: out=print_string(item,0); break; case cJSON_Array: out=print_array(item,depth,fmt,0); break; case cJSON_Object: out=print_object(item,depth,fmt,0); break; } } return out; }
cJSON *cJSON_CreateString(mp_pool_t *p,const char *string) {cJSON *item=cJSON_New_Item(p);if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(p,string);}return item;}
/* Render the cstring provided to an escaped version that can be printed. */ static char *print_string_ptr(const char *str) { const char *ptr; char *ptr2,*out; int len=0; unsigned char token; if (!str) return cJSON_strdup(""); ptr=str; while ((token=*ptr) && ++len) { if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5; ptr++; } out=(char*)cJSON_malloc(len+3); if (!out) return 0; ptr2=out; ptr=str; *ptr2++='\"'; while (*ptr) { if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; else { *ptr2++='\\'; switch (token=*ptr++) { case '\\': *ptr2++='\\'; break; case '\"': *ptr2++='\"'; break; case '\b': *ptr2++='b'; break; case '\f': *ptr2++='f'; break; case '\n': *ptr2++='n'; break; case '\r': *ptr2++='r'; break; case '\t': *ptr2++='t'; break; default: sprintf(ptr2,"u%04x",token); ptr2+=5; break; /* escape and print */ } } } *ptr2++='\"'; *ptr2++=0; return out; }
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);if(!item->valuestring){cJSON_Delete(item);return 0;}}return item;}
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();item->type=cJSON_String;item->valuestring=cJSON_strdup(string);return item;}
/* Duplication */ cJSON *cJSON_Duplicate(const cJSON *item, cjbool recurse) { cJSON *newitem = NULL; cJSON *cptr = NULL; cJSON *nptr = NULL; cJSON *newchild = NULL; /* Bail on bad ptr */ if (!item) { return NULL; } /* Create new item */ newitem = cJSON_New_Item(); if (!newitem) { return NULL; } /* Copy over all vars */ newitem->type = item->type & (~cJSON_IsReference); newitem->valueint = item->valueint; newitem->valuedouble = item->valuedouble; if (item->valuestring) { newitem->valuestring = cJSON_strdup(item->valuestring); if (!newitem->valuestring) { cJSON_Delete(newitem); return NULL; } } if (item->string) { newitem->string = (item->type&cJSON_StringIsConst) ? item->string : cJSON_strdup(item->string); if (!newitem->string) { cJSON_Delete(newitem); return NULL; } } /* If non-recursive, then we're done! */ if (!recurse) { return newitem; } /* Walk the ->next chain for the child. */ cptr = item->child; while (cptr) { newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */ if (!newchild) { cJSON_Delete(newitem); return NULL; } if (nptr) { /* If newitem->child already set, then crosswire ->prev and ->next and move on */ nptr->next = newchild; newchild->prev = nptr; nptr = newchild; } else { /* Set newitem->child and move to it */ newitem->child = newchild; nptr = newchild; } cptr = cptr->next; } return newitem; }
/*********************************************************** * Function: gw_lc_bind_device * Input: * Output: * Return: ***********************************************************/ OPERATE_RET gw_lc_bind_device(IN CONST DEV_DESC_IF_S *dev_if,\ IN CONST CHAR *sch_json_arr) { if(NULL == dev_if || \ NULL == sch_json_arr) { return OPRT_INVALID_PARM; } if(get_dev_cntl(dev_if->id)) { return OPRT_OK; } cJSON *root = NULL; OPERATE_RET op_ret = OPRT_OK; root = cJSON_Parse(sch_json_arr); if(NULL == root) { return OPRT_CJSON_PARSE_ERR; } INT dp_num; DEV_CNTL_N_S *dev_cntl = NULL; dp_num = cJSON_GetArraySize(root); if(0 == dp_num) { op_ret = OPRT_INVALID_PARM; goto ERR_EXIT; } dev_cntl = (DEV_CNTL_N_S *)Malloc(sizeof(DEV_CNTL_N_S)+dp_num*sizeof(DP_CNTL_S)); if(NULL == dev_cntl) { op_ret = OPRT_MALLOC_FAILED; goto ERR_EXIT; } memset(dev_cntl,0,sizeof(DEV_CNTL_N_S)+dp_num*sizeof(DP_CNTL_S)); memcpy(&dev_cntl->dev_if,dev_if,sizeof(DEV_DESC_IF_S)); dev_cntl->dp_num = dp_num; // dp schema parse INT i; DP_DESC_IF_S *dp_desc; DP_PROP_VALUE_U *prop; cJSON *cjson; cJSON *next; for(i = 0;i < dev_cntl->dp_num;i++) { dp_desc = &(dev_cntl->dp[i].dp_desc); prop = &(dev_cntl->dp[i].prop); cjson = cJSON_GetArrayItem(root,i); if(NULL == cjson) { op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } // id next = cJSON_GetObjectItem(cjson,"id"); if(NULL == next) { PR_ERR("get id null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } if(next->type == cJSON_String) { dp_desc->dp_id = atoi(next->valuestring); }else { dp_desc->dp_id = next->valueint; } // mode next = cJSON_GetObjectItem(cjson,"mode"); if(NULL == next) { PR_ERR("get mode null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } if(!strcmp(next->valuestring,"rw")) { dp_desc->mode = M_RW; }else if(!strcmp(next->valuestring,"ro")) { dp_desc->mode = M_RO; }else { dp_desc->mode = M_WR; } // passive next = cJSON_GetObjectItem(cjson,"passive"); if(next == NULL) { dp_desc->passive = FALSE; }else { dp_desc->passive = next->type; dev_cntl->preprocess = TRUE; } // trigger next = cJSON_GetObjectItem(cjson,"trigger"); if(NULL == next) { dp_desc->trig_t = TRIG_PULSE; }else { if(!strcmp(next->valuestring,"pulse")) { dp_desc->trig_t = TRIG_PULSE; }else { dp_desc->trig_t = TRIG_DIRECT; } } // type next = cJSON_GetObjectItem(cjson,"type"); if(NULL == next) { PR_ERR("get type null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } if(!strcmp(next->valuestring,"obj")) { dp_desc->type = T_OBJ; }else if(!strcmp(next->valuestring,"raw")) { dp_desc->type = T_RAW; continue; }else { dp_desc->type = T_FILE; continue; } // property next = cJSON_GetObjectItem(cjson,"property"); if(NULL == next) { PR_ERR("get property null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } cJSON *child; child = cJSON_GetObjectItem(next,"type"); if(NULL == next) { PR_ERR("get type null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } if(!strcmp(child->valuestring,"bool")) { dp_desc->prop_tp = PROP_BOOL; }else if(!strcmp(child->valuestring,"value")) { dp_desc->prop_tp = PROP_VALUE; CHAR *str[] = {"max","min","scale"}; INT i; for(i = 0; i < CNTSOF(str);i++) { child = cJSON_GetObjectItem(next,str[i]); if(NULL == child && (i != CNTSOF(str)-1)) { PR_ERR("get property null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; }else if(NULL == child && (i == CNTSOF(str)-1)) { prop->prop_value.scale = 0; }else { switch(i) { case 0: prop->prop_value.max = child->valueint; break; case 1: prop->prop_value.min = child->valueint; break; //case 2: prop->prop_value.step = child->valueint; break; case 2: prop->prop_value.scale = child->valueint; break; } } } }else if(!strcmp(child->valuestring,"string")) { dp_desc->prop_tp = PROP_STR; child = cJSON_GetObjectItem(next,"maxlen"); if(NULL == child) { PR_ERR("get maxlen null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } prop->prop_str.max_len = child->valueint; prop->prop_str.value = Malloc(prop->prop_str.max_len+1); if(NULL == prop->prop_str.value) { PR_ERR("malloc error"); op_ret = OPRT_MALLOC_FAILED; goto ERR_EXIT; } }else { dp_desc->prop_tp = PROP_ENUM; child = cJSON_GetObjectItem(next,"range"); if(NULL == child) { PR_ERR("get range null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } INT i,num; num = cJSON_GetArraySize(child); if(num == 0) { PR_ERR("get array size error"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } prop->prop_enum.pp_enum = Malloc(num*sizeof(CHAR *)); if(NULL == prop->prop_enum.pp_enum) { PR_ERR("malloc error"); op_ret = OPRT_MALLOC_FAILED; goto ERR_EXIT; } prop->prop_enum.cnt = num; for(i = 0;i < num;i++) { cJSON *c_child = cJSON_GetArrayItem(child,i); if(NULL == c_child) { PR_ERR("get array null"); op_ret = OPRT_CJSON_GET_ERR; goto ERR_EXIT; } prop->prop_enum.pp_enum[i] = cJSON_strdup(c_child->valuestring); if(NULL == prop->prop_enum.pp_enum[i]) { PR_ERR("malloc error"); op_ret = OPRT_MALLOC_FAILED; goto ERR_EXIT; } } } } os_mutex_get(&gw_mutex, OS_WAIT_FOREVER); if(NULL == gw_cntl.dev) { gw_cntl.dev = dev_cntl; }else { DEV_CNTL_N_S *tmp_dev_cntl = gw_cntl.dev; while(tmp_dev_cntl->next) { tmp_dev_cntl = tmp_dev_cntl->next; } tmp_dev_cntl->next = dev_cntl; } gw_cntl.dev_num++; os_mutex_put(&gw_mutex); if(root) { cJSON_Delete(root); } return OPRT_OK; ERR_EXIT: if(dev_cntl) { Free(dev_cntl); } if(root) { cJSON_Delete(root); } return op_ret; }
cJSON *cJSON_CreateString(const char *string, ngx_pool_t *pool) {cJSON *item=cJSON_New_Item(pool);if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string, pool);}return item;}
/* Duplication */ cJSON *ICACHE_FLASH_ATTR cJSON_Duplicate(cJSON *item, int recurse) { cJSON *newitem, *cptr, *nptr = 0, *newchild; /* Bail on bad ptr */ if (!item) { return 0; } /* Create new item */ newitem = cJSON_New_Item(); if (!newitem) { return 0; } /* Copy over all vars */ newitem->type = item->type & (~cJSON_IsReference), newitem->valueint = item->valueint, newitem->valuedouble = item->valuedouble; if (item->valuestring) { newitem->valuestring = cJSON_strdup(item->valuestring); if (!newitem->valuestring) { cJSON_Delete(newitem); return 0; } } if (item->string) { newitem->string = cJSON_strdup(item->string); if (!newitem->string) { cJSON_Delete(newitem); return 0; } } /* If non-recursive, then we're done! */ if (!recurse) { return newitem; } /* Walk the ->next chain for the child. */ cptr = item->child; while (cptr) { newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */ if (!newchild) { cJSON_Delete(newitem); return 0; } if (nptr) { nptr->next = newchild, newchild->prev = nptr; /* If newitem->child already set, then crosswire ->prev and ->next and move on */ nptr = newchild; } else { newitem->child = newchild; /* Set newitem->child and move to it */ nptr = newchild; } cptr = cptr->next; } return newitem; }
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=cJSON_GetObjectItemV2(object,string,&i);if(c){if(newitem->string) cJSON_free(newitem->string);newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}