/** * @brief * add node to json list * * @param[in] ntype - node type * @param[in] vtype - value type * @param[in] key - node key * @param[in] value - value for node * * @return structure handle * @retval structure handle to JsonNode list success * @retval NULL error * */ JsonNode* add_json_node(JsonNodeType ntype, JsonValueType vtype, JsonEscapeType esc_type, char *key, void *value) { int rc = 0; char *ptr = NULL; char *pc = NULL; JsonNode *node = NULL; node = create_json_node(); if (node == NULL) { fprintf(stderr, "Json Node: out of memory\n"); return NULL; } node->node_type = ntype; if (key != NULL) { ptr = strdup((char *)key); if (ptr == NULL) { fprintf(stderr, "Json Node: out of memory\n"); return NULL; } node->key = ptr; } if (vtype == JSON_NULL && value != NULL) { (void)strtod(value, &pc); while (pc) { if (isspace(*pc)) pc++; else break; } if (strcmp(pc, "") == 0) { node->value_type = JSON_NUMERIC; ptr = strdup(value); if (ptr == NULL) return NULL; node->value.string = ptr; } else node->value_type = JSON_STRING; } else { node->value_type = vtype; if (node->value_type == JSON_INT) node->value.inumber = *((long int *)value); else if (node->value_type == JSON_FLOAT) node->value.fnumber = *((double *)value); } if (node->value_type == JSON_STRING) { if (value != NULL) { ptr = strdup_escape(esc_type, value); if (ptr == NULL) return NULL; } node->value.string = ptr; } rc = link_node(node); if (rc) { free(node); fprintf(stderr, "Json link: out of memory\n"); return NULL; } return node; }
/** * @brief * add node to json list * * @param[in] ntype - node type * @param[in] vtype - value type * @param[in] key - node key * @param[in] value - value for node * * @return structure handle * @retval structure handle to JsonNode list success * @retval NULL error * */ JsonNode* add_json_node(JsonNodeType ntype, JsonValueType vtype, JsonEscapeType esc_type, char *key, void *value) { int rc = 0; char *ptr = NULL; char *pc = NULL; double val = 0; long int ivalue = 0; JsonNode *node = NULL; node = create_json_node(); if (node == NULL) { fprintf(stderr, "Json Node: out of memory\n"); return NULL; } node->node_type = ntype; if (key != NULL) { ptr = strdup((char *)key); if (ptr == NULL) { fprintf(stderr, "Json Node: out of memory\n"); return NULL; } node->key = ptr; } if (vtype == JSON_NULL && value != NULL) { val = strtod(value, &pc); while (pc) { if (isspace(*pc)) pc++; else break; } if (strcmp(pc, "") == 0) { ivalue = (long int) val; if (val == ivalue) {/* This checks if value have any non zero fractional part after decimal. If not then value has to be represented as integer otherwise as float. */ node->value_type = JSON_INT; node->value.inumber = ivalue; } else { node->value_type = JSON_FLOAT; node->value.fnumber = val; } } else node->value_type = JSON_STRING; } else { node->value_type = vtype; if (node->value_type == JSON_INT) node->value.inumber = *((long int *)value); else if (node->value_type == JSON_FLOAT) node->value.fnumber = *((double *)value); } if (node->value_type == JSON_STRING) { if (value != NULL) { ptr = strdup_escape(esc_type, value); if (ptr == NULL) return NULL; } node->value.string = ptr; } rc = link_node(node); if (rc) { free(node); fprintf(stderr, "Json link: out of memory\n"); return NULL; } return node; }