/*=========================================================================*\ Get boolean value from repository return false on error of if key is not found \*=========================================================================*/ bool persistGetBool( const char *key ) { json_t *jObj; DBGMSG( "persistGetBool: (%s)", key ); /*------------------------------------------------------------------------*\ Get Object \*------------------------------------------------------------------------*/ jObj = persistGetJSON( key ); if( !jObj ) { DBGMSG( "persistGetBool: (%s) not set", key ); return false; } /*------------------------------------------------------------------------*\ Check type \*------------------------------------------------------------------------*/ if( !json_is_true(jObj) && !json_is_false(jObj) ) { logwarn( "Value for key \"%s\" is not a boolean (%d): ", key, json_typeof(jObj) ); return false; } /*------------------------------------------------------------------------*\ return real value \*------------------------------------------------------------------------*/ bool value = json_is_true( jObj ); DBGMSG( "persistGetBool: (%s)=%s", key, value?"True":"False" ); return value; }
ServerRepo ServerRepo::fromJSON(const json_t *json, json_error_t */* error */) { ServerRepo repo; repo.id = getStringFromJson(json, "id"); repo.name = getStringFromJson(json, "name"); repo.description = getStringFromJson(json, "desc"); repo.mtime = json_integer_value(json_object_get(json, "mtime")); repo.size = json_integer_value(json_object_get(json, "size")); repo.root = getStringFromJson(json, "root"); repo.encrypted = json_is_true(json_object_get(json, "encrypted")); repo.type = getStringFromJson(json, "type"); repo.permission = getStringFromJson(json, "permission"); repo.readonly = (repo.permission == "r") ? true : false; repo._virtual = json_is_true(json_object_get(json, "virtual")); if (repo.type == "grepo") { repo.owner = getStringFromJson(json, "share_from"); repo.group_name = getStringFromJson(json, "owner"); repo.group_id = json_integer_value(json_object_get(json, "groupid")); } else { repo.owner = getStringFromJson(json, "owner"); repo.group_name = QString(); repo.group_id = 0; } return repo; }
static void submit_work(CURL *curl, struct work *work) { char *hexstr = NULL; json_t *val, *res; char s[345], timestr[64]; time_t now; struct tm *tm; now = time(NULL); /* build hex string */ hexstr = bin2hex(work->data, sizeof(work->data)); if (!hexstr) { fprintf(stderr, "submit_work OOM\n"); goto out; } /* build JSON-RPC request */ sprintf(s, "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n", hexstr); if (opt_debug) fprintf(stderr, "DBG: sending RPC call:\n%s", s); /* issue JSON-RPC request */ val = json_rpc_call(curl, rpc_url, userpass, s); if (!val) { fprintf(stderr, "submit_work json_rpc_call failed\n"); goto out; } res = json_object_get(val, "result"); tm = localtime(&now); strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm); printf("%s: PROOF OF WORK RESULT: %s\n", timestr, json_is_true(res) ? "true (yay!!!)" : "false (booooo)"); if (opt_log) { FILE *f = fopen(opt_log,"a"); fprintf(f, "%s: PROOF OF WORK RESULT: %s\n", timestr, json_is_true(res) ? "true (yay!!!)" : "false (booooo)"); fprintf(f, "%s: DATA: %s\n", timestr, hexstr); fclose(f); } json_decref(val); out: free(hexstr); }
Status loginServerOtpPending(std::list<DataChunk> users, std::list<bool> &isPending) { const auto url = ABC_SERVER_ROOT "/v1/otp/pending/check"; std::string param; std::map<std::string, bool> userMap; std::list<std::string> usersEncoded; for (const auto &u : users) { std::string username = base64Encode(u); param += (username + ","); userMap[username] = false; usersEncoded.push_back(username); } JsonObject json; ABC_CHECK(json.set("l1s", param)); HttpReply reply; ABC_CHECK(AirbitzRequest().post(reply, url, json.encode())); ServerReplyJson replyJson; ABC_CHECK(replyJson.decode(reply)); JsonArray arrayJson = replyJson.results(); size_t size = arrayJson.size(); for (size_t i = 0; i < size; i++) { json_t *pJSON_Row = arrayJson[i].get(); if (!pJSON_Row || !json_is_object(pJSON_Row)) return ABC_ERROR(ABC_CC_JSONError, "Error parsing JSON array element object"); json_t *pJSON_Value = json_object_get(pJSON_Row, "login"); if (!pJSON_Value || !json_is_string(pJSON_Value)) return ABC_ERROR(ABC_CC_JSONError, "Error otp/pending/login JSON"); std::string username(json_string_value(pJSON_Value)); pJSON_Value = json_object_get(pJSON_Row, ABC_SERVER_JSON_OTP_PENDING); if (!pJSON_Value || !json_is_boolean(pJSON_Value)) return ABC_ERROR(ABC_CC_JSONError, "Error otp/pending/pending JSON"); if (json_is_true(pJSON_Value)) { userMap[username] = json_is_true(pJSON_Value); } } isPending.clear(); for (auto &username: usersEncoded) { isPending.push_back(userMap[username]); } return Status(); }
json_t* OBSAPIMessageHandler::HandleSetVolume(OBSAPIMessageHandler* handler, json_t* message) { json_t* channel = json_object_get(message, "channel"); json_t* volume = json_object_get(message, "volume"); json_t* finalValue = json_object_get(message, "final"); if(volume == NULL) { return GetErrorResponse("Volume not specified."); } if(!json_is_number(volume)) { return GetErrorResponse("Volume not number."); } float val = (float) json_number_value(volume); val = min(1.0f, max(0.0f, val)); if(finalValue == NULL) { return GetErrorResponse("Final not specified."); } if(!json_is_boolean(finalValue)) { return GetErrorResponse("Final is not a boolean."); } if(channel != NULL && json_typeof(channel) == JSON_STRING) { const char* channelVal = json_string_value(channel); if(stricmp(channelVal, "desktop") == 0) { OBSSetDesktopVolume(val, json_is_true(finalValue)); } else if(stricmp(channelVal, "microphone") == 0) { OBSSetMicVolume(val, json_is_true(finalValue)); } else { return GetErrorResponse("Invalid channel specified."); } } else { return GetErrorResponse("Channel not specified."); } return GetOkResponse(); }
UITheme * UITheme::FromJson(const json_t * json) { const char * themeName = json_string_value(json_object_get(json, "name")); if (themeName == nullptr) { ThrowThemeLoadException(); } json_t * jsonEntries = json_object_get(json, "entries"); size_t numEntries = json_object_size(jsonEntries); UITheme * result = nullptr; try { result = new UITheme(themeName); if (json_is_true(json_object_get(json, "useLightsRide"))) { result->Flags |= UITHEME_FLAG_USE_LIGHTS_RIDE; } if (json_is_true(json_object_get(json, "useLightsPark"))) { result->Flags |= UITHEME_FLAG_USE_LIGHTS_PARK; } if (json_is_true(json_object_get(json, "useAltScenarioSelectFont"))) { result->Flags |= UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT; } const char * jkey; json_t * jvalue; size_t i = 0; json_object_foreach(jsonEntries, jkey, jvalue) { const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(jkey); if (wtDesc == nullptr) continue; UIThemeWindowEntry entry = UIThemeWindowEntry::FromJson(wtDesc, jvalue); result->SetEntry(&entry); } return result; } catch (Exception ex) { delete result; throw ex; } }
json_t *json_deep_copy(json_t *json) { if(!json) return NULL; if(json_is_object(json)) return json_object_deep_copy(json); if(json_is_array(json)) return json_array_deep_copy(json); /* for the rest of the types, deep copying doesn't differ from shallow copying */ if(json_is_string(json)) return json_string_copy(json); if(json_is_integer(json)) return json_integer_copy(json); if(json_is_real(json)) return json_real_copy(json); if(json_is_true(json) || json_is_false(json) || json_is_null(json)) return json; return NULL; }
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; }
static void draw_likes() { int likes = 0, liked = 0, liked_padding = 0; char *liked_str = NULL; json_t *photo, *likes_node, *liked_node, *count_node; photo = photos[photo_pos]; likes_node = json_object_get(photo, "likes"); count_node = json_object_get(likes_node, "count"); likes = json_integer_value(count_node); liked_padding = 7; liked_node = json_object_get(photo, "user_has_liked"); liked = json_is_true(liked_node); liked_str = malloc(sizeof(char) * 11); if (liked) { liked_padding -= 4; sprintf(liked_str, "\x1B[31m♥\x1B[0m %d", likes); } else { sprintf(liked_str, "♥ %d", likes); } printf("\033[%d;%dH%10s", 23, left + container - liked_padding, liked_str); if (liked_str) { free(liked_str); } }
json_t *json_copy(json_t *json) { if(!json) return NULL; if(json_is_object(json)) return json_object_copy(json); if(json_is_array(json)) return json_array_copy(json); if(json_is_string(json)) return json_string_copy(json); if(json_is_integer(json)) return json_integer_copy(json); if(json_is_real(json)) return json_real_copy(json); if(json_is_true(json) || json_is_false(json) || json_is_null(json)) return json; return NULL; }
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; }
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"); } }
/* 判断janson的类型 */ int my_json_type(json_t *value) { if(json_is_object(value)) { printf("json_is_object\n"); return JSON_OBJECT; } if(json_is_array(value)) { printf("json_is_array\n"); return JSON_ARRAY; } if(json_is_string(value)) { printf("json_is_string\n"); return JSON_STRING; } if(json_is_integer(value)) { printf("json_is_integer\n"); return JSON_INTEGER; } if(json_is_real(value)) { printf("json_is_real\n"); return JSON_REAL; } if(json_is_number(value)) { printf("json_is_number\n"); } if(json_is_boolean(value)) { printf("json_is_boolean\n"); } if(json_is_null(value)) { printf("json_is_null\n"); return JSON_NULL; } if(json_is_true(value)) { printf("json_boolean(1)\n"); return JSON_TRUE; } if(json_is_false(value)) { printf("json_boolean(0)\n"); return JSON_FALSE; } }
tABC_CC ABC_LoginServerOtpStatus(const Lobby &lobby, tABC_U08Buf LP1, bool *on, long *timeout, tABC_Error *pError) { tABC_CC cc = ABC_CC_Ok; json_t *pJSON_Value = NULL; JsonPtr reply; std::string url = ABC_SERVER_ROOT "/otp/status"; ABC_CHECK_RET(ABC_LoginServerOtpRequest(url.c_str(), lobby, LP1, &reply, pError)); pJSON_Value = json_object_get(reply.get(), ABC_SERVER_JSON_OTP_ON); ABC_CHECK_ASSERT((pJSON_Value && json_is_boolean(pJSON_Value)), ABC_CC_JSONError, "Error otp/on JSON"); *on = json_is_true(pJSON_Value); if (*on) { pJSON_Value = json_object_get(reply.get(), ABC_SERVER_JSON_OTP_TIMEOUT); ABC_CHECK_ASSERT((pJSON_Value && json_is_integer(pJSON_Value)), ABC_CC_JSONError, "Error otp/timeout JSON"); *timeout = json_integer_value(pJSON_Value); } exit: return cc; }
void GetEventsRequest::requestSuccess(QNetworkReply& reply) { json_error_t error; json_t *root = parseJSON(reply, &error); if (!root) { qDebug("GetEventsRequest: failed to parse json:%s\n", error.text); emit failed(ApiError::fromJsonError()); return; } QScopedPointer<json_t, JsonPointerCustomDeleter> json(root); bool more = false; int more_offset = -1; json_t* array = json_object_get(json.data(), "events"); std::vector<SeafEvent> events = SeafEvent::listFromJSON(array, &error); more = json_is_true(json_object_get(json.data(), "more")); if (more) { more_offset = json_integer_value(json_object_get(json.data(), "more_offset")); } emit success(events, more_offset); }
static void decode_any() { json_t *json; json_error_t error; json = json_loads("\"foo\"", JSON_DECODE_ANY, &error); if (!json || !json_is_string(json)) fail("json_load decoded any failed - string"); json_decref(json); json = json_loads("42", JSON_DECODE_ANY, &error); if (!json || !json_is_integer(json)) fail("json_load decoded any failed - integer"); json_decref(json); json = json_loads("true", JSON_DECODE_ANY, &error); if (!json || !json_is_true(json)) fail("json_load decoded any failed - boolean"); json_decref(json); json = json_loads("null", JSON_DECODE_ANY, &error); if (!json || !json_is_null(json)) fail("json_load decoded any failed - null"); json_decref(json); }
static void parse_config(void) { int i; json_t *val; if (!json_is_object(opt_config)) return; for (i = 0; i < ARRAY_SIZE(options); i++) { if (!options[i].name) break; if (!strcmp(options[i].name, "config")) continue; val = json_object_get(opt_config, options[i].name); if (!val) continue; if (options[i].has_arg && json_is_string(val)) { char *s = strdup(json_string_value(val)); if (!s) break; parse_arg(options[i].val, s); free(s); } else if (!options[i].has_arg && json_is_true(val)) parse_arg(options[i].val, ""); else fprintf(stderr, "JSON option %s invalid\n", options[i].name); } }
static kbool_t GetJsonBoolean(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, kbool_t defval) { json_t* obj = (key != NULL) ? json_object_get(jsonbuf->jsonobj, key) : jsonbuf->jsonobj; if(json_is_boolean(obj)) { return (kbool_t)json_is_true(obj); } return defval; }
static bool get_bool_orelse(json_t* json, const char* key, bool def) { json_t* v = json_object_get(json, key); if (v == NULL) return def; if (json_is_null(v)) return def; return json_is_true(v) ? true : false; }
static inline bool get_bool_val(json_t *service, const char *key) { json_t *bool_val = json_object_get(service, key); if (!bool_val || !json_is_boolean(bool_val)) return false; return json_is_true(bool_val); }
// Compute the effective value of the root_files configuration and // return a json reference. The caller must decref the ref when done // (we may synthesize this value). Sets enforcing to indicate whether // we will only allow watches on the root_files. // The array returned by this function (if not NULL) is guaranteed to // list .watchmanconfig as its zeroth element. json_t *cfg_compute_root_files(bool *enforcing) { json_t *ref; // This is completely undocumented and will go away soon. Do not document or // use! bool ignore_watchmanconfig = cfg_get_bool(NULL, "_ignore_watchmanconfig", false); *enforcing = false; ref = cfg_get_json(NULL, "enforce_root_files"); if (ref) { if (!json_is_boolean(ref)) { w_log(W_LOG_FATAL, "Expected config value enforce_root_files to be boolean\n"); } *enforcing = json_is_true(ref); } ref = cfg_get_json(NULL, "root_files"); if (ref) { if (!is_array_of_strings(ref)) { w_log(W_LOG_FATAL, "global config root_files must be an array of strings\n"); *enforcing = false; return NULL; } prepend_watchmanconfig_to_array(ref); json_incref(ref); return ref; } // Try legacy root_restrict_files configuration ref = cfg_get_json(NULL, "root_restrict_files"); if (ref) { if (!is_array_of_strings(ref)) { w_log(W_LOG_FATAL, "deprecated global config root_restrict_files " "must be an array of strings\n"); *enforcing = false; return NULL; } if (!ignore_watchmanconfig) { prepend_watchmanconfig_to_array(ref); } json_incref(ref); *enforcing = true; return ref; } // Synthesize our conservative default value. // .watchmanconfig MUST be first if (!ignore_watchmanconfig) { return json_pack("[ssss]", ".watchmanconfig", ".hg", ".git", ".svn"); } else { return json_pack("[sss]", ".hg", ".git", ".svn"); } }
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; }
bool Json::getBool(const char *key) const { if (!json_) { return false; } json_t *value = json_object_get(json_, key); if (json_is_false(value)) return false; return json_is_true(value) || json_integer_value(value); }
bool json_get_bool_key(const json_t* obj, const char* key) { json_t* value = json_object_get(obj, key); if (value == NULL) { fprintf(stderr, "error: missing required \"%s\" key\n", key); exit(1); } if (!json_is_boolean(value)) { fprintf(stderr, "error: \"%s\" key must be a boolean, got %s\n", key, json_strof(value)); exit(1); } return json_is_true(value); }
static cxBool jsonToBool(json_t *v,cxBool dv) { CX_RETURN(v == NULL,dv); v = jsonParseRegisterValue(v); if(json_is_string(v)){ return strcasecmp(json_string_value(v), "true") == 0; } if(json_is_integer(v)){ return json_integer_value(v) != 0; } return json_is_true(v); }
//## Boolean Json.getBool(String key); static KMETHOD Json_getBool(KonohaContext *kctx, KonohaStack *sfp) { json_t* obj = ((struct _kJson *)sfp[0].asObject)->obj; CHECK_JSON(obj, KReturnUnboxValue(false)); const char *key = S_text(sfp[1].asString); json_t* json = json_object_get(obj, key); kbool_t ret = false; if(json_is_true(json)) { ret = true; } KReturnUnboxValue(ret); }
int jansson_to_val(pv_value_t* val, char** freeme, json_t* v) { val->flags = 0; if(json_is_object(v) || json_is_array(v)) { const char* value = json_dumps(v, JSON_COMPACT|JSON_PRESERVE_ORDER); *freeme = (char*)value; val->rs.s = (char*)value; val->rs.len = strlen(value); val->flags = PV_VAL_STR; }else if(json_is_string(v)) { const char* value = json_string_value(v); val->rs.s = (char*)value; val->rs.len = strlen(value); val->flags = PV_VAL_STR; }else if(json_is_boolean(v)) { val->ri = json_is_true(v) ? 0 : 1; val->flags = PV_TYPE_INT|PV_VAL_INT; }else if(json_is_real(v)) { char* value = NULL; if(asprintf(&value, "%.15g", json_real_value(v))<0) { ERR("asprintf failed\n"); return -1; } *freeme = value; val->rs.s = value; val->rs.len = strlen(value); val->flags = PV_VAL_STR; }else if(json_is_integer(v)) { long long value = json_integer_value(v); if ((value > INT_MAX) || (value < INT_MIN)) { char* svalue = NULL; if (asprintf(&svalue, "%"JSON_INTEGER_FORMAT, value) < 0) { ERR("asprintf failed\n"); return -1; } *freeme = svalue; val->rs.s = svalue; val->rs.len = strlen(svalue); val->flags = PV_VAL_STR; } else { val->ri = (int)value; val->flags = PV_TYPE_INT|PV_VAL_INT; } }else if(json_is_null(v)) { val->flags = PV_VAL_NULL; }else { ERR("unrecognized json type: %d\n", json_typeof(v)); return -1; } return 0; }
bool cfg_get_bool(w_root_t *root, const char *name, bool defval) { json_t *val = cfg_get_json(root, name); if (val) { if (!json_is_boolean(val)) { w_log(W_LOG_FATAL, "Expected config value %s to be a boolean\n", name); } return json_is_true(val); } return defval; }
void BinWriter::writeConstructor(json_t *jconstructor) { writeMethodBase(jconstructor); if (json_is_true(json_object_get(jconstructor, "defaultconstructor"))) { bytes.writeBoolean(true); } else { bytes.writeBoolean(false); } }
static bool stratum_notify_m7(struct stratum_ctx *sctx, json_t *params) { const char *job_id, *prevblock, *accroot, *merkleroot, *version, *ntime; int height; bool clean; job_id = json_string_value(json_array_get(params, 0)); prevblock = json_string_value(json_array_get(params, 1)); accroot = json_string_value(json_array_get(params, 2)); merkleroot = json_string_value(json_array_get(params, 3)); height = json_integer_value(json_array_get(params, 4)); version = json_string_value(json_array_get(params, 5)); ntime = json_string_value(json_array_get(params, 6)); clean = json_is_true(json_array_get(params, 7)); if (!job_id || !prevblock || !accroot || !merkleroot || !version || !height || !ntime || strlen(prevblock) != 32*2 || strlen(accroot) != 32*2 || strlen(merkleroot) != 32*2 || strlen(ntime) != 8*2 || strlen(version) != 2*2) { applog(LOG_ERR, "Stratum (M7) notify: invalid parameters"); return false; } pthread_mutex_lock(&sctx->work_lock); if (!sctx->job.job_id || strcmp(sctx->job.job_id, job_id)) { sctx->job.xnonce2 = (unsigned char *)realloc(sctx->job.xnonce2, sctx->xnonce2_size); memset(sctx->job.xnonce2, 0, sctx->xnonce2_size); } free(sctx->job.job_id); sctx->job.job_id = strdup(job_id); hex2bin(sctx->job.m7prevblock, prevblock, 32); hex2bin(sctx->job.m7accroot, accroot, 32); hex2bin(sctx->job.m7merkleroot, merkleroot, 32); be64enc(sctx->job.m7height, height); hex2bin(sctx->job.m7version, version, 2); hex2bin(sctx->job.m7ntime, ntime, 8); sctx->job.clean = clean; sctx->job.diff = sctx->next_diff; pthread_mutex_unlock(&sctx->work_lock); return true; }