Jsonnode* json_copy(const Jsonnode *node){ assert(node); Jsonnode *dst=malloc(sizeof(Jsonnode)); assert(dst); dst->type=node->type; switch(node->type){ case JSON_NUMBER: dst->numval=node->numval; break; case JSON_STRING: dst->strval=copyofstring(node->strval); break; case JSON_BOOL: dst->boolval=node->boolval; break; case JSON_NULL: break; case JSON_ARRAY:{ int len=node->arrval.length; size_t cap=node->arrval.capacity; dst->arrval.capacity=cap; dst->arrval.length=len; dst->arrval.elems=malloc(cap*sizeof(Jsonnode*)); assert(dst->arrval.elems); for(int i=0;i<len;i++){ dst->arrval.elems[i]=json_copy(node->arrval.elems[i]); } break; } case JSON_OBJECT:{ int nk=node->objval.numkeys; size_t cap=node->objval.capacity; dst->objval.capacity=cap; dst->objval.numkeys=nk; dst->objval.keys=malloc(cap*sizeof(char*)); assert(dst->objval.keys); dst->objval.values=malloc(cap*sizeof(Jsonnode*)); assert(dst->objval.values); for(int i=0;i<nk;i++){ dst->objval.keys[i]=copyofstring(node->objval.keys[i]); dst->objval.values[i]=json_copy(node->objval.values[i]); } break; } } return dst; }
/** * Get the heater value */ json_t * b_device_get_heater (json_t * device, const char * heater_name, void * device_ptr) { y_log_message(Y_LOG_LEVEL_INFO, "device-mock - Running command get_heater for heater %s on device %s", heater_name, json_string_value(json_object_get(device, "name"))); if (0 == o_strcmp(heater_name, "he1")) { json_t * heater = json_copy(json_object_get(json_object_get((json_t *)device_ptr, "heaters"), heater_name)); json_object_set_new(heater, "result", json_integer(WEBSERVICE_RESULT_OK)); return heater; } else if (0 == o_strcmp(heater_name, "he2")) { json_t * heater = json_copy(json_object_get(json_object_get((json_t *)device_ptr, "heaters"), heater_name)); json_object_set_new(heater, "result", json_integer(WEBSERVICE_RESULT_OK)); return heater; } else { return json_pack("{si}", "result", WEBSERVICE_RESULT_NOT_FOUND); } }
struct json *json_copy(struct json *jbase) { struct json *new_json = xmalloc(sizeof(struct json)); struct json_childs *jchilds = jbase->jchilds; new_json->name.len = jbase->name.len; new_json->name.buf = xmalloc(sizeof(char) * (new_json->name.len + 1)); memcpy(new_json->name.buf, jbase->name.buf, new_json->name.len + 1); new_json->value.len = jbase->value.len; if (jbase->value.len) { new_json->value.buf = xmalloc(sizeof(char) * (new_json->value.len + 1)); memcpy(new_json->value.buf, jbase->value.buf, new_json->value.len + 1); } else { new_json->value.buf = NULL; } new_json->prev = NULL; new_json->next = NULL; new_json->jfather = NULL; new_json->jchilds = NULL; if (jbase->next != NULL) { new_json->next = json_copy(jbase->next); new_json->next->prev = new_json; } while (jchilds != NULL) { struct json_childs *new_child = xmalloc(sizeof(struct json_childs)); new_child->type = jchilds->type; new_child->child = json_copy(jchilds->child); new_child->child->jfather = new_json; new_child->next = new_json->jchilds; new_json->jchilds = new_child; jchilds = jchilds->next; } return new_json; }
//native Handle:json_copy(Handle:hObj); static cell_t Native_json_copy(IPluginContext *pContext, const cell_t *params) { HandleError err; HandleSecurity sec; sec.pOwner = NULL; sec.pIdentity = myself->GetIdentity(); // Param 1: hObj json_t *object; Handle_t hndlObject = static_cast<Handle_t>(params[1]); if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None) { pContext->ThrowNativeError("Invalid <JSON Object> handle %x (error %d)", hndlObject, err); return BAD_HANDLE; } if(object == NULL) { pContext->ThrowNativeError("JSON Object is NULL."); return BAD_HANDLE; } json_t *copy = json_copy(object); if(copy == NULL) { pContext->ThrowNativeError("Could not copy JSON Object."); return BAD_HANDLE; } Handle_t hndlResult = g_pHandleSys->CreateHandle(htJanssonObject, copy, pContext->GetIdentity(), myself->GetIdentity(), NULL); if(hndlResult == BAD_HANDLE) { pContext->ThrowNativeError("Could not create handle for copied JSON Object."); } return hndlResult; }
Jsonnode *json_object_get_item(const Jsonobject *obj, const char *key) { for (int i = 0; i < obj->numkeys; i++) { if (strcmp(obj->keys[i], key) == 0) { return json_copy(obj->values[i]); } } return NULL; }
void BE_get_request() { char request[1024]; int n = ME_sock_recv(the_context.driverfd, request); if (n <= 0 || !(*request)) return 1; //Parse out JSON json_t *root, *params; json_error_t error; root = json_loads(request, 0, &error); params = json_object_get(root,"params"); char * RLI_expr = json_string_value(json_array_get(params,0)); json_t *id_copy = json_copy(json_object_get(root,"id")); ME_RLI_IR_value value_result = BE_rhandler_dispatch(RLI_expr); json_decref(root); //Send response root = json_object(); json_object_set_new(root, "jsonrpc", json_string("2.0")); if (value_result.type == ME_RLI_IR_VALUE_MEASUREMENT) { json_object_set_new(root, "result", ME_measurement_toJSON(value_result.vdata.ms)); } else { json_object_set_new(root, "result", json_null()); } json_object_set_new(root, "id", id_copy); char * response; response = json_dumps( root, 0 ); ME_sock_send(the_context.driverfd, response); json_decref(root); if (quitting) { close(the_context.driverfd); exit(-1); } }
struct json *json_copy(struct json *jbase) { struct json *new_json = xmalloc(sizeof(struct json)); struct json_childs *jchilds = jbase->jchilds; new_json->name = xstrdup(jbase->name); new_json->value = (jbase->value != NULL ? xstrdup(jbase->value) : NULL); new_json->prev = NULL; new_json->next = NULL; new_json->jfather = NULL; new_json->jchilds = NULL; if (jbase->next != NULL) { new_json->next = json_copy(jbase->next); new_json->next->prev = new_json; } while (jchilds != NULL) { struct json_childs *new_child = xmalloc(sizeof(struct json_childs)); new_child->type = jchilds->type; new_child->child = json_copy(jchilds->child); new_child->child->jfather = new_json; new_child->next = new_json->jchilds; new_json->jchilds = new_child; jchilds = jchilds->next; } return new_json; }
struct json *get_json_object_user(USERS *user) { json *jstr = NULL; if (user != NULL) { set_json("pubid", user->pipe->pubid, &jstr); set_json("casttype", "uni", &jstr); if (user->properties != NULL) { int has_prop = 0; json *jprop = NULL; extend *eTmp = user->properties; while (eTmp != NULL) { if (eTmp->visibility == EXTEND_ISPUBLIC) { if (!has_prop) { has_prop = 1; set_json("properties", NULL, &jstr); } if (eTmp->type == EXTEND_JSON) { json *jcopy = json_copy(eTmp->val); set_json(eTmp->key, NULL, &jprop); json_attach(jprop, jcopy, JSON_OBJECT); } else { set_json(eTmp->key, eTmp->val, &jprop); } } eTmp = eTmp->next; } if (has_prop) { json_attach(jstr, jprop, JSON_OBJECT); } } } else { set_json("pubid", SERVER_NAME, &jstr); } return jstr; }
void json_object_add_key(Jsonobject *obj, const char *key, const Jsonnode *val) { char *k = copyofstring(key); Jsonnode *v = json_copy(val); for (int i = 0; i < obj->numkeys; i++) { if (strcmp(obj->keys[i], key) == 0) { DBG("overwriting previous object key '%s'\n", key); free(obj->keys[i]); json_free(obj->values[i]); obj->keys[i] = k; obj->values[i] = v; return; } } obj->numkeys++; json_object_ensure_capacity(obj, obj->numkeys); obj->keys[obj->numkeys - 1] = k; obj->values[obj->numkeys - 1] = v; }
bool round_json_array_appendreplica(RoundJSONObject* obj, RoundJSONObject* valObj) { if (!round_json_object_isarray(obj)) return false; bool isAdded = false; #if defined(ROUND_USE_JSON_PARSER_JANSSON) if (!valObj->jsonObj) return false; json_t* copyJsonObj = json_copy(valObj->jsonObj); if (!copyJsonObj) return false; isAdded = (json_array_append(obj->jsonObj, copyJsonObj) == 0) ? true : false; json_decref(copyJsonObj); #endif return isAdded; }
struct json *get_json_object_channel(CHANNEL *chan) { json *jstr = NULL; //set_json("topic", chan->topic, &jstr); //set_json("name", chan->name, &jstr); // See below set_json("pubid", chan->pipe->pubid, &jstr); set_json("casttype", "multi", &jstr); //if (chan->properties != NULL) { json *jprop = NULL; set_json("properties", NULL, &jstr); extend *eTmp = chan->properties; while (eTmp != NULL) { if (eTmp->visibility == EXTEND_ISPUBLIC) { if (eTmp->type == EXTEND_JSON) { json *jcopy = json_copy(eTmp->val); set_json(eTmp->key, NULL, &jprop); json_attach(jprop, jcopy, JSON_OBJECT); } else { set_json(eTmp->key, eTmp->val, &jprop); } } eTmp = eTmp->next; } /* a little hack to have the same behaviour than user */ set_json("name", chan->name, &jprop); json_attach(jstr, jprop, JSON_OBJECT); //} return jstr; }
struct ast_json *ast_json_copy(const struct ast_json *value) { return (struct ast_json *)json_copy((json_t *)value); }
json_t *json_object_dup(json_t *val, const char *entry) { return json_copy(json_object_get(val, entry)); }
void json_array_add_item(Jsonarray *arr, const Jsonnode *item) { Jsonnode *copy = json_copy(item); arr->length++; json_array_ensure_capacity(arr, arr->length); arr->elems[arr->length - 1] = copy; }
la_codec_value_t *la_codec_copy(la_codec_value_t *value) { return (la_codec_value_t *) json_copy((json_t *) value); }
/** * Set the dimmer command */ json_t * b_device_set_dimmer (json_t * device, const char * dimmer_name, const int command, void * device_ptr) { y_log_message(Y_LOG_LEVEL_INFO, "device-mock - Running command set_dimmer for dimmer %s on device %s with the value %d", dimmer_name, json_string_value(json_object_get(device, "name")), command); if (0 == o_strcmp(dimmer_name, "di1") || 0 == o_strcmp(dimmer_name, "di2")) { if (command < 101) { json_object_set_new(json_object_get((json_t *)device_ptr, "dimmers"), dimmer_name, json_integer(command)); if (command > 0) { json_object_set_new(json_object_get((json_t *)device_ptr, "dimmers_values"), dimmer_name, json_integer(command)); } } else { json_object_set_new(json_object_get((json_t *)device_ptr, "dimmers"), dimmer_name, json_copy(json_object_get(json_object_get((json_t *)device_ptr, "dimmers_values"), dimmer_name))); } return json_pack("{sisI}", "result", WEBSERVICE_RESULT_OK, "value", json_integer_value(json_object_get(json_object_get((json_t *)device_ptr, "dimmers"), dimmer_name))); } else { return json_pack("{si}", "result", WEBSERVICE_RESULT_NOT_FOUND); } }
int main() { json_error_t error; json_t *root = json_load_file("char.js", NULL, &error); //load the json character file if(root != NULL) { //check if the file was loaded correctly json_t *allitems = json_object_get(root, "items"); //we only need the item object of the json file int i = 0, j = 0; json_t *equippeditems = json_array(); //store the currently equipped items in a new json array(the list of itemslots is globally defined at the top for(i = 0; i < json_array_size(allitems); i++) { json_t *currentitem = json_array_get(allitems, i); for(j = 0; j < 10; j++) { if(!strcmp(json_string_value(json_object_get(currentitem, "inventoryId")), itemslots[j])) { json_array_append(equippeditems, json_copy(currentitem)); break; } } } json_t *modlist = json_array(); //an array of all mods that are on the currently equipped items for(i = 0; i < json_array_size(equippeditems); i++) { json_t *currentitem = json_array_get(equippeditems, i); json_t *implicitmods = json_object_get(currentitem, "implicitMods"); json_t *explicitmods = json_object_get(currentitem, "explicitMods"); // if the item has implicit Mods add them to the Mod list if(json_array_size(implicitmods) > 0) { for(j = 0; j < json_array_size(implicitmods); j++) { json_array_append(modlist, json_copy(json_array_get(implicitmods, j))); } } //Same as implicit mods but for explicit if(json_array_size(explicitmods) > 0) { for(j = 0; j < json_array_size(explicitmods); j++) { json_array_append(modlist, json_copy(json_array_get(explicitmods, j))); } } } //A test print to check if the loop above works correctly printf("%s\n", json_dumps(modlist, NULL)); } else { printf("Error loading character file\n"); } }
/** * create a new request based on the source elements * returned value must be free'd */ struct _u_request * ulfius_duplicate_request(const struct _u_request * request) { struct _u_request * new_request = NULL; if (request != NULL) { new_request = malloc(sizeof(struct _u_request)); if (new_request == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request"); return NULL; } if (ulfius_init_request(new_request) == U_OK) { new_request->http_verb = nstrdup(request->http_verb); new_request->http_url = nstrdup(request->http_url); if (new_request->http_verb == NULL || new_request->http_url == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for ulfius_duplicate_request"); ulfius_clean_request_full(new_request); return NULL; } if (request->client_address != NULL) { new_request->client_address = malloc(sizeof(struct sockaddr)); if (new_request->client_address == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request->client_address"); ulfius_clean_request_full(new_request); return NULL; } memcpy(new_request->client_address, request->client_address, sizeof(struct sockaddr)); } u_map_clean_full(new_request->map_url); u_map_clean_full(new_request->map_header); u_map_clean_full(new_request->map_cookie); u_map_clean_full(new_request->map_post_body); new_request->map_url = u_map_copy(request->map_url); new_request->map_header = u_map_copy(request->map_header); new_request->map_cookie = u_map_copy(request->map_cookie); new_request->map_post_body = u_map_copy(request->map_post_body); new_request->json_body = json_copy(request->json_body); new_request->json_has_error = request->json_has_error; if ((new_request->map_url == NULL && request->map_url != NULL) || (new_request->map_header == NULL && request->map_header != NULL) || (new_request->map_cookie == NULL && request->map_cookie != NULL) || (new_request->map_post_body == NULL && request->map_post_body != NULL) || (new_request->json_body == NULL && request->json_body != NULL)) { ulfius_clean_request_full(new_request); return NULL; } if (request->binary_body != NULL && request->binary_body_length > 0) { new_request->binary_body = malloc(request->binary_body_length); if (new_request->binary_body == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request->binary_body"); ulfius_clean_request_full(new_request); return NULL; } memcpy(new_request->binary_body, request->binary_body, request->binary_body_length); } else { new_request->binary_body_length = 0; new_request->binary_body = NULL; } new_request->binary_body_length = request->binary_body_length; } else { free(new_request); new_request = NULL; } } return new_request; }