// get the limit price according to the requested volume double getLimitPrice(Parameters& params, double volume, bool isBid){ json_t* root; if (isBid) { root = json_object_get(getJsonFromUrl(params, "http://api.796.com/v3/futures/depth.html?type=weekly", ""), "bids"); } else { root = json_object_get(getJsonFromUrl(params, "http://api.796.com/v3/futures/depth.html?type=weekly", ""), "asks"); } // loop on volume *params.logFile << "<SevenNintySix> Looking for a limit price to fill " << volume << "BTC..." << std::endl; double tmpVol = 0.0; int i = 0; while (tmpVol < volume) { // volumes are added up until the requested volume is reached double p = json_real_value(json_array_get(json_array_get(root, i), 0)); double v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<SevenNintySix> order book: " << v << "@$" << p << std::endl; tmpVol += .1; //v; had to comment this out in order to advance past this call in sendOrder, as v was always 0 i++; } double limPrice = 0.0; limPrice = atof(json_string_value(json_array_get(json_array_get(root, i), 0))); json_decref(root); return limPrice; }
static void decode_int_as_real() { json_t *json; json_error_t error; #if JSON_INTEGER_IS_LONG_LONG const char *imprecise; json_int_t expected; #endif json = json_loads("42", JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error); if (!json || !json_is_real(json) || json_real_value(json) != 42.0) fail("json_load decode int as real failed - int"); json_decref(json); #if JSON_INTEGER_IS_LONG_LONG /* This number cannot be represented exactly by a double */ imprecise = "9007199254740993"; expected = 9007199254740992ll; json = json_loads(imprecise, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error); if (!json || !json_is_real(json) || expected != (json_int_t)json_real_value(json)) fail("json_load decode int as real failed - expected imprecision"); json_decref(json); #endif }
// get the limit price according to the requested volume double getLimitPrice(Parameters& params, double volume, bool isBid){ json_t* root; if (isBid) { root = json_object_get(getJsonFromUrl(params, "http://api.796.com/v3/futures/depth.html?type=weekly", ""), "bids"); } else { root = json_object_get(getJsonFromUrl(params, "http://api.796.com/v3/futures/depth.html?type=weekly", ""), "asks"); } // loop on volume *params.logFile << "<SevenNintySix> Looking for a limit price to fill " << volume << "BTC..." << std::endl; double tmpVol = 0.0; int i = 0; while (tmpVol < volume) { // volumes are added up until the requested volume is reached double p = json_real_value(json_array_get(json_array_get(root, i), 0)); double v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<SevenNintySix> order book: " << v << "@$" << p << std::endl; tmpVol += v; i++; } // return the next offer double limPrice = 0.0; if (params.aggressiveVolume) { limPrice = atof(json_string_value(json_array_get(json_array_get(root, i), 0))); } else { limPrice = atof(json_string_value(json_array_get(json_array_get(root, i+1), 0))); } json_decref(root); return limPrice; }
double getQuote(Parameters& params, bool isBid) { bool GETRequest = false; json_t* root = getJsonFromUrl(params, "https://btc-e.com/api/3/ticker/btc_usd", "", GETRequest); double quoteValue; if (isBid) { quoteValue = json_real_value(json_object_get(json_object_get(root, "btc_usd"), "buy")); } else { quoteValue = json_real_value(json_object_get(json_object_get(root, "btc_usd"), "sell")); } json_decref(root); return quoteValue; }
double getLimitPrice(Parameters& params, double volume, bool isBid) { json_t* root; double limPrice = 0.0; if (isBid) { root = json_object_get(getJsonFromUrl(params, "https://www.okcoin.com/api/v1/depth.do", ""), "bids"); // loop on volume *params.logFile << "<OKCoin> Looking for a limit price to fill " << volume << " BTC..." << std::endl; double tmpVol = 0.0; double p; double v; size_t i = 0; while (tmpVol < volume * 2.0) { p = json_real_value(json_array_get(json_array_get(root, i), 0)); v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<OKCoin> order book: " << v << "@$" << p << std::endl; tmpVol += v; i++; } if (params.aggressiveVolume) { limPrice = json_real_value(json_array_get(json_array_get(root, i-1), 0)); } else { p = json_real_value(json_array_get(json_array_get(root, i), 0)); v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<OKCoin> order book: " << v << "@$" << p << " (non-aggressive)" << std::endl; limPrice = p; } } else { root = json_object_get(getJsonFromUrl(params, "https://www.okcoin.com/api/v1/depth.do", ""), "asks"); // loop on volume *params.logFile << "<OKCoin> Looking for a limit price to fill " << volume << " BTC..." << std::endl; double tmpVol = 0.0; double p; double v; size_t i = json_array_size(root) - 1; while (tmpVol < volume * 2.0) { p = json_real_value(json_array_get(json_array_get(root, i), 0)); v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<OKCoin> order book: " << v << "@$" << p << std::endl; tmpVol += v; i--; } if (params.aggressiveVolume) { limPrice = json_real_value(json_array_get(json_array_get(root, i+1), 0)); } else { p = json_real_value(json_array_get(json_array_get(root, i), 0)); v = json_real_value(json_array_get(json_array_get(root, i), 1)); *params.logFile << "<OKCoin> order book: " << v << "@$" << p << " (non-aggressive)" << std::endl; limPrice = p; } } json_decref(root); return limPrice; }
struct sensor sensorFromJSON(json_t* sensor) { struct sensor theSensor; json_t* tmp = json_object_get(sensor, "angle"); theSensor.angle = json_real_value(tmp); tmp = json_object_get(sensor, "width"); theSensor.width = json_real_value(tmp); tmp = json_object_get(sensor, "range"); theSensor.range = json_integer_value(tmp); tmp = json_object_get(sensor, "turret"); theSensor.turret = json_integer_value(tmp); return theSensor; }
static void run_tests() { json_t *integer, *real; json_int_t i; double d; integer = json_integer(5); real = json_real(100.1); if(!integer) fail("unable to create integer"); if(!real) fail("unable to create real"); i = json_integer_value(integer); if(i != 5) fail("wrong integer value"); d = json_real_value(real); if(d != 100.1) fail("wrong real value"); d = json_number_value(integer); if(d != 5.0) fail("wrong number value"); d = json_number_value(real); if(d != 100.1) fail("wrong number value"); json_decref(integer); json_decref(real); #ifdef NAN real = json_real(NAN); if(real != NULL) fail("could construct a real from NaN"); real = json_real(1.0); if(json_real_set(real, NAN) != -1) fail("could set a real to NaN"); if(json_real_value(real) != 1.0) fail("real value changed unexpectedly"); json_decref(real); #endif #ifdef INFINITY test_inifity(); #endif }
cxBool cxJsonPushValue(json_t *value) { if(json_is_integer(value)){ lua_pushinteger(gL,(int)json_integer_value(value)); }else if(json_is_string(value)){ lua_pushstring(gL, json_string_value(value)); }else if(json_is_number(value)){ lua_pushnumber(gL, json_number_value(value)); }else if(json_is_true(value)){ lua_pushboolean(gL, 1); }else if(json_is_false(value)){ lua_pushboolean(gL, 0); }else if(json_is_null(value)){ lua_pushnil(gL); }else if(json_is_real(value)){ lua_pushnumber(gL, json_real_value(value)); }else if(json_is_object(value)){ cxJsonPushObject(value); }else if(json_is_array(value)){ cxJsonPushArray(value); }else{ return false; } return true; }
int json_object_to_lua (lua_State *L, json_t *o) { if (o == NULL) { lua_pushnil (L); return (1); } switch (json_typeof (o)) { case JSON_OBJECT: json_object_to_lua_table (L, o); break; case JSON_ARRAY: json_array_to_lua (L, o); break; case JSON_STRING: lua_pushstring (L, json_string_value (o)); break; case JSON_INTEGER: lua_pushinteger (L, json_integer_value (o)); break; case JSON_REAL: lua_pushnumber (L, json_real_value (o)); break; case JSON_TRUE: lua_pushboolean (L, 1); break; case JSON_FALSE: lua_pushboolean (L, 0); break; case JSON_NULL: /* XXX: crap. */ break; } return (1); }
static QList<QVariant> listFromJSON(json_t *array) { QList<QVariant> ret; size_t array_size = json_array_size(array); json_t *value; for(size_t index = 0; index < array_size && (value = json_array_get(array, index)); ++index) { /* block of code that uses index and value */ QVariant v; if (json_is_object(value)) { v = mapFromJSON(value, NULL); } else if (json_is_array(value)) { v = listFromJSON(value); } else if (json_is_string(value)) { v = QString::fromUtf8(json_string_value(value)); } else if (json_is_integer(value)) { v = json_integer_value(value); } else if (json_is_real(value)) { v = json_real_value(value); } else if (json_is_boolean(value)) { v = json_is_true(value); } if (v.isValid()) { ret.push_back(v); } } return ret; }
int w_bser_dump(const bser_ctx_t* ctx, json_t *json, void *data) { int type = json_typeof(json); if (!is_bser_version_supported(ctx)) { return -1; } switch (type) { case JSON_NULL: return ctx->dump(&bser_null, sizeof(bser_null), data); case JSON_TRUE: return ctx->dump(&bser_true, sizeof(bser_true), data); case JSON_FALSE: return ctx->dump(&bser_false, sizeof(bser_false), data); case JSON_REAL: return bser_real(ctx, json_real_value(json), data); case JSON_INTEGER: return bser_int(ctx, json_integer_value(json), data); case JSON_STRING: return bser_bytestring(ctx, json_string_value(json), data); case JSON_ARRAY: return bser_array(ctx, json, data); case JSON_OBJECT: return bser_object(ctx, json, data); default: return -1; } }
struct data_t *deserialize_data(json_t *json_data) { struct data_t *data = (struct data_t *)malloc(sizeof(struct data_t)); if(json_is_integer(json_data)) { data->type = RPC_INT; data->value = (json_int_t *)malloc(sizeof(json_int_t)); *(json_int_t *)data->value = json_integer_value(json_data); } else if(json_is_real(json_data)) { data->type = RPC_REAL; data->value = (double *)malloc(sizeof(double)); *(double *)data->value = json_real_value(json_data); } else if(json_is_string(json_data)) { data->type = RPC_STRING; data->value = private_strdup(json_string_value(json_data)); } else if(json_is_array(json_data)) { data->type = RPC_VECTOR; data->length = json_array_size(json_data); data->child = (struct data_t **)malloc(sizeof(struct data_t *)*data->length); for(int i = 0; i < data->length; i++) { data->child[i] = deserialize_data(json_array_get(json_data, i)); } } else { return NULL; } return data; }
void on_list_update(struct DSLink *link, ref_t *req_ref, json_t *resp) { (void) link; RequestHolder *holder = req_ref->data; json_t *updates = json_object_get(resp, "updates"); size_t index; json_t *value; const char* path = json_string_value(json_object_get(holder->req, "path")); printf("======= List %s =======\n", path); json_array_foreach(updates, index, value) { json_t *name = json_array_get(value, 0); json_t *val = json_array_get(value, 1); if (val->type == JSON_ARRAY || val->type == JSON_OBJECT) { char *data = json_dumps(val, JSON_INDENT(0)); printf("%s = %s\n", json_string_value(name), data); dslink_free(data); } else if (val->type == JSON_STRING) { printf("%s = %s\n", json_string_value(name), json_string_value(val)); } else if (val->type == JSON_INTEGER) { printf("%s = %lli\n", json_string_value(name), json_integer_value(val)); } else if (val->type == JSON_REAL) { printf("%s = %f\n", json_string_value(name), json_real_value(val)); } else if (val->type == JSON_NULL) { printf("%s = NULL\n", json_string_value(name)); } else if (val->type == JSON_TRUE) { printf("%s = true\n", json_string_value(name)); } else if (val->type == JSON_FALSE) { printf("%s = false\n", json_string_value(name)); } else { printf("%s = (Unknown Type)\n", json_string_value(name)); } }
void debug_show_value(GhbValue *gval) { GhbType tp; tp = ghb_value_type(gval); if (tp == GHB_STRING) { g_message("Type %s value %s", "string", json_string_value(gval)); } else if (tp == GHB_INT) { g_message("Type %s value %"JSON_INTEGER_FORMAT, "int", json_integer_value(gval)); } else if (tp == GHB_DOUBLE) { g_message("Type %s value %f", "double", json_real_value(gval)); } else if (tp == GHB_BOOL) { g_message("Type %s value %d", "boolean", json_is_true(gval)); } else if (tp == GHB_ARRAY) { g_message("Type %s", "array"); } else if (tp == GHB_DICT) { g_message("Type %s", "dict"); } }
/*=========================================================================*\ Get real number from repository return 0.0 on error of if key is not found \*=========================================================================*/ double persistGetReal( const char *key ) { json_t *jObj; /*------------------------------------------------------------------------*\ Get Object \*------------------------------------------------------------------------*/ jObj = persistGetJSON( key ); if( !jObj ) { DBGMSG( "persistGetReal: (%s) not set", key ); return 0; } /*------------------------------------------------------------------------*\ Check type \*------------------------------------------------------------------------*/ if( !json_is_real(jObj) ) { logwarn( "Value for key \"%s\" is not a real number (%d): ", key, json_typeof(jObj) ); return 0; } /*------------------------------------------------------------------------*\ return real value \*------------------------------------------------------------------------*/ double value = json_real_value( jObj ); DBGMSG( "persistGetReal: (%s)=%g", key, value ); return value; }
int w_bser_dump(json_t *json, json_dump_callback_t dump, void *data) { int type = json_typeof(json); switch (type) { case JSON_NULL: return dump(&bser_null, sizeof(bser_null), data); case JSON_TRUE: return dump(&bser_true, sizeof(bser_true), data); case JSON_FALSE: return dump(&bser_false, sizeof(bser_false), data); case JSON_REAL: return bser_real(json_real_value(json), dump, data); case JSON_INTEGER: return bser_int(json_integer_value(json), dump, data); case JSON_STRING: return bser_string(json_string_value(json), dump, data); case JSON_ARRAY: return bser_array(json, dump, data); case JSON_OBJECT: return bser_object(json, dump, data); default: return -1; } }
//--------------------------------------------------------------------------- int Json::get_type_node(Value &v) { switch (json_typeof(current_node)) { case JSON_INTEGER: v.type = Value::CONTAINER_TYPE_INTEGER; v.l = json_integer_value(current_node); break; case JSON_REAL: v.type = Value::CONTAINER_TYPE_REAL; v.d = json_real_value(current_node); break; case JSON_STRING: v.type = Value::CONTAINER_TYPE_STRING; v.s = json_string_value(current_node); break; case JSON_TRUE: v.type = Value::CONTAINER_TYPE_BOOL; v.b = true; break; case JSON_FALSE: v.type = Value::CONTAINER_TYPE_BOOL; v.b = false; break; case JSON_NULL: v.type = Value::CONTAINER_TYPE_NULL; break; default: return -1; } return 0; }
static int wunder_parse(json_t *root, weather_data_t *weather) { json_t *node, *subnode; node = json_object_get(root, "current_observation"); if(!json_is_object(node)) { fprintf(stderr, "[-] wunder/parse: cannot get current_observation\n"); return 0; } /* grab location */ subnode = json_object_get(node, "display_location"); if(!json_is_object(subnode)) { fprintf(stderr, "[-] wunder/parse: cannot get display_location\n"); return 0; } if(!(json_string_value(json_object_get(subnode, "full")))) { fprintf(stderr, "[-] wunder/parse: cannot get full\n"); return 0; } strncpy( weather->location, json_string_value(json_object_get(subnode, "full")), sizeof(weather->location) ); /* grab temperature */ if(!(json_is_number(json_object_get(node, "temp_c")))) { fprintf(stderr, "[-] wunder/parse: cannot get temp_c\n"); return 0; } if(json_is_real(json_object_get(node, "temp_c"))) weather->temp = json_real_value(json_object_get(node, "temp_c")); else weather->temp = (double) json_integer_value(json_object_get(node, "temp_c")); /* grab humidity */ if(!(json_string_value(json_object_get(node, "relative_humidity")))) { fprintf(stderr, "[-] wunder/parse: cannot get relative_humidity\n"); return 0; } weather->humidity = atof(json_string_value(json_object_get(node, "relative_humidity"))); /* grab date */ if(!(json_string_value(json_object_get(node, "observation_time_rfc822")))) { fprintf(stderr, "[-] wunder/parse: cannot get observation_time_rfc822\n"); return 0; } strncpy( weather->date, json_string_value(json_object_get(node, "observation_time_rfc822")), sizeof(weather->date) ); return 1; }
ctr_object* ctr_json_create_object(json_t* root, ctr_object* gt) { switch(json_typeof(root)) { case JSON_OBJECT: { ctr_object* sub = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_set_link_all(sub, gt); // size_t size; const char *key; json_t *value; ctr_argument* argl = ctr_heap_allocate(sizeof(*argl)); argl->next = ctr_heap_allocate(sizeof(*argl)); // size = json_object_size(root); json_object_foreach(root, key, value) { char* k = (char*)key; ctr_object* ko = ctr_build_string_from_cstring(k); ctr_object* vo = ctr_json_create_object(value, gt); argl->object = vo; argl->next->object = ko; sub = ctr_map_put(sub, argl); } ctr_heap_free(argl->next); ctr_heap_free(argl); return sub; } case JSON_ARRAY: { ctr_object* arr = ctr_array_new(CtrStdArray, NULL); ctr_argument* arg = ctr_heap_allocate(sizeof(ctr_argument)); size_t i; size_t size = json_array_size(root); for (i = 0; i < size; i++) { arg->object = ctr_json_create_object(json_array_get(root, i), gt); ctr_array_push(arr, arg); } ctr_heap_free(arg); return arr; } case JSON_STRING: { ctr_object* str = ctr_build_string((char*)json_string_value(root), json_string_length(root)); return str; } case JSON_INTEGER: { return ctr_build_number_from_float(json_integer_value(root)); } case JSON_REAL: { return ctr_build_number_from_float(json_real_value(root)); } case JSON_FALSE: { return ctr_build_bool(0); } case JSON_TRUE: { return ctr_build_bool(1); } case JSON_NULL: { return ctr_build_nil(); } default: { CtrStdFlow = ctr_build_string_from_cstring("Unrecognized JSON type"); return CtrStdNil; } }
static double GetJsonFloat(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, double defval) { json_t* obj = (key != NULL) ? json_object_get(jsonbuf->jsonobj, key) : jsonbuf->jsonobj; if(json_is_real(obj)) { return json_real_value(obj); } return defval; }
static int json_t_to_avro_value_helper( const avro_schema_t schema, const json_t *json, avro_datum_t datum) { switch (schema->type) { case AVRO_BOOLEAN: check_param(EINVAL, json_is_boolean(json), "JSON boolean"); if (avro_boolean_set(datum, json_is_true(json))) return EINVAL; return 0; case AVRO_DOUBLE: check_param(EINVAL, json_is_real(json), "JSON float"); if (avro_double_set(datum, json_real_value(json))) return EINVAL; return 0; case AVRO_FLOAT: check_param(EINVAL, json_is_real(json), "JSON float"); if (avro_float_set(datum, json_real_value(json))) return EINVAL; return 0; case AVRO_INT32: check_param(EINVAL, json_is_integer(json), "JSON integer"); if (avro_int32_set(datum, json_integer_value(json))) return EINVAL; return 0; case AVRO_INT64: check_param(EINVAL, json_is_integer(json), "JSON integer"); if (avro_int64_set(datum, json_integer_value(json))) return EINVAL; return 0; case AVRO_NULL: check_param(EINVAL, json_is_null(json), "JSON null"); return 0; case AVRO_STRING: { check_param(EINVAL, json_is_string(json), "JSON string"); if (avro_string_set(datum, json_string_value(json))) return EINVAL; return 0; } case AVRO_BYTES: case AVRO_ARRAY: case AVRO_ENUM: case AVRO_FIXED: case AVRO_MAP: case AVRO_RECORD: case AVRO_UNION: default: // TODO avro_set_error("json_t_to_avro_value: unimplemented type"); return EINVAL; } }
int BaseGameObject::deserialize(json_t * root) { json_t *tmp_obj; if (!root) { fprintf(stderr, "%s/%s+%d: unable to allocate json object\n", __FILE__, __FUNCTION__, __LINE__); return (1); } id = (int) json_integer_value(json_object_get(root, "id")); fps = (float) json_real_value(json_object_get(root, "fps")); sorting_layer = (int) json_integer_value(json_object_get(root, "sorting_layer")); sorting_order = (int) json_integer_value(json_object_get(root, "sorting_order")); enabled = json_boolean_value(json_object_get(root, "enabled")); draw_sprite = json_boolean_value(json_object_get(root, "draw_sprite")); position.deserialize(json_object_get(root, "position")); size.deserialize(json_object_get(root, "size")); tmp_obj = json_object_get(root, "dst"); dst.x = (int) json_integer_value(json_object_get(tmp_obj, "x")); dst.y = (int) json_integer_value(json_object_get(tmp_obj, "y")); dst.w = (int) json_integer_value(json_object_get(tmp_obj, "w")); dst.h = (int) json_integer_value(json_object_get(tmp_obj, "h")); // read the sprite filenames tmp_obj = json_object_get(root, "sprites"); if (tmp_obj) { sprite_filenames.resize(json_array_size(tmp_obj)); for (size_t i = 0; i < json_array_size(tmp_obj); i++) { json_t *entry = json_array_get(tmp_obj, i); int index = (int) json_integer_value(json_object_get(entry, "index")); json_t *file_object = json_object_get(entry, "file"); const char *file = json_string_value(file_object); if (index < 0) { index = 0; } sprite_filenames[index] = (char*) calloc(json_string_length(file_object) + 1, sizeof(char)); strcpy_s(sprite_filenames[index], json_string_length(file_object) + 1, file); } } // read the children tmp_obj = json_object_get(root, "children"); if (tmp_obj) { children.resize(json_array_size(tmp_obj)); for (size_t i = 0; i < json_array_size(tmp_obj); i++) { children[i] = new BaseGameObject(); children[i]->deserialize(json_array_get(tmp_obj, i)); } } update_dst(); init(); return 0; }
double json_number_value(const json_t *json) { if(json_is_integer(json)) return json_integer_value(json); else if(json_is_real(json)) return json_real_value(json); else return 0.0; }
static int ProcessEntry(char *outbuf, KeyValueNode *pair, char label) { char *json_answer=NULL; json_t *root, *results; json_error_t error; char url[512]; int entrySize=0, nresults; /* cplusplus stuff! */ try { sprintf(url, addressFormat, pair->value); json_answer = (char*) DownloadToBuffer(url); // process the query result if ((root = json_loads(json_answer, 0, &error)) == NULL) { printf("json error on line %d: %s\n", error.line, error.text); // release the response buffer if (json_answer != NULL) free(json_answer); return -1; } results = json_object_get(root, "results"); nresults = json_array_size(results); for (int i=0; i < nresults; ++i) { double lat, lng; json_t *result, *geometry, *location; result = json_array_get(results, i); location = json_object_get(json_object_get(result, "geometry"), "location"); lat = json_real_value(json_object_get(location, "lat")); lng = json_real_value(json_object_get(location, "lng")); entrySize+=sprintf(outbuf+entrySize, markerFormat, "blue", label, lat, lng); } if (json_answer != NULL) free(json_answer); // release json resources json_decref(root); return entrySize; } catch (...) { if (json_answer != NULL) free(json_answer); // release json resources json_decref(root); return -1; } }
int createRevenueMovementList(FILE * revenue) { json_error_t * error; size_t index; json_t *value; json_t * json_revenue = json_loadf(revenue, 0, error); json_revenue = json_object_get(json_revenue, "revenue"); if (json_revenue == NULL) { return 0; } json_array_foreach(json_revenue, index, value) { struct revenue * newRevenue; newRevenue = calloc(1, sizeof(struct revenue)); json_t * json_title = json_object_get(value, "title"); json_t * json_value = json_object_get(value, "value"); json_t * json_date = json_object_get(value, "date"); json_t * json_desc = json_object_get(value, "description"); char * title = strdup(json_string_value(json_title)); char * description = strdup(json_string_value(json_desc)); float value = json_real_value(json_value); time_t date = json_integer_value(json_date); newRevenue->title = malloc(sizeof(title)); newRevenue->title = title; newRevenue->description = malloc(sizeof(description)); newRevenue->description = description; newRevenue->value = value; newRevenue->date = date; newRevenue->next = firstRevenueMovement; firstRevenueMovement = newRevenue; } return 1; }
Value NDKHelper::getValueFromJson(json_t *obj) { if (obj == NULL) { return Value::Null; } if (json_is_object(obj)) { ValueMap valueMap; const char *key; json_t *value; void *iter = json_object_iter(obj); while (iter) { key = json_object_iter_key(iter); value = json_object_iter_value(iter); valueMap[key] = NDKHelper::getValueFromJson(value); iter = json_object_iter_next(obj, iter); } return Value(valueMap); } else if (json_is_array(obj)) { ValueVector valueVector; size_t sizeArray = json_array_size(obj); for (unsigned int i = 0; i < sizeArray; i++) { valueVector.push_back(NDKHelper::getValueFromJson(json_array_get(obj, i))); } return Value(valueVector); } else if (json_is_boolean(obj)) { if (json_is_true(obj)) { return Value(true); } else { return Value(false); } } else if (json_is_integer(obj)) { int value = (int) json_integer_value(obj); return Value(value); } else if (json_is_real(obj)) { double value = json_real_value(obj); return Value(value); } else if (json_is_string(obj)) { std::string value = json_string_value(obj); return Value(value); } return Value::Null; }
static void decode_int_as_real() { json_t *json; json_error_t error; #if JSON_INTEGER_IS_LONG_LONG const char *imprecise; json_int_t expected; #endif char big[311]; json = json_loads("42", JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error); if (!json || !json_is_real(json) || json_real_value(json) != 42.0) fail("json_load decode int as real failed - int"); json_decref(json); #if JSON_INTEGER_IS_LONG_LONG /* This number cannot be represented exactly by a double */ imprecise = "9007199254740993"; expected = 9007199254740992ll; json = json_loads(imprecise, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error); if (!json || !json_is_real(json) || expected != (json_int_t)json_real_value(json)) fail("json_load decode int as real failed - expected imprecision"); json_decref(json); #endif /* 1E309 overflows. Here we create 1E309 as a decimal number, i.e. 1000...(309 zeroes)...0. */ big[0] = '1'; memset(big + 1, '0', 309); big[310] = '\0'; json = json_loads(big, JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error); if (json || strcmp(error.text, "real number overflow") != 0 || json_error_code(&error) != json_error_numeric_overflow) fail("json_load decode int as real failed - expected overflow"); json_decref(json); }
boost::any configuration_parser::convert_json(json_t* _val) { switch( json_typeof( _val )) { case JSON_INTEGER: return boost::any((int)json_integer_value(_val)); case JSON_STRING: return boost::any(std::string(json_string_value(_val))); case JSON_REAL: return boost::any(json_real_value(_val)); case JSON_TRUE: return boost::any(json_true()); case JSON_FALSE: return boost::any(json_false()); case JSON_NULL: return boost::any(std::string("NULL")); case JSON_ARRAY: { std::vector<boost::any> vector; size_t idx = 0; json_t* element = NULL; json_array_foreach(_val, idx, element) { try { vector.push_back(convert_json(element)); } catch (const irods::exception& e) { irods::log(e); } } return boost::any(vector); } case JSON_OBJECT: { std::unordered_map<std::string, boost::any> map; const char* key = NULL; json_t* subval = NULL; json_object_foreach( _val, key, subval ) { try { map.insert(std::pair<std::string, boost::any>(std::string(key), convert_json(subval))); } catch (const irods::exception& e) { irods::log(e); } } return boost::any(map); } } THROW( -1, (boost::format("unhandled type in json_typeof: %d") % json_typeof(_val) ).str()); } // parse_json_object
bool round_json_object_getreal(RoundJSONObject* obj, double* value) { if (!obj) return false; if (!round_json_object_isreal(obj)) return false; *value = json_real_value(obj->jsonObj); return true; }
const double load_config_dashThickness(void) { json_t *root; json_error_t error; root = json_load_file("config.json",0,&error); if(root == 0) { printf("Erro no arquivo de configuraĆ§Ć£o, linha %d\n",error.line); exit(1); } json_t *jglt = json_object_get(root, "dashThickness"); const double dashThickness = json_real_value(jglt); return dashThickness; }