static void cmd_debug_recrawl(struct watchman_client *client, json_t *args) { w_root_t *root; json_t *resp; /* resolve the root */ if (json_array_size(args) != 2) { send_error_response(client, "wrong number of arguments for 'debug-recrawl'"); return; } root = resolve_root_or_err(client, args, 1, false); if (!root) { return; } resp = make_response(); w_root_lock(root); w_root_schedule_recrawl(root, "debug-recrawl"); w_root_unlock(root); set_prop(resp, "recrawl", json_true()); send_and_dispose_response(client, resp); w_root_delref(root); }
/* debug-ageout */ static void cmd_debug_ageout(struct watchman_client *client, json_t *args) { w_root_t *root; json_t *resp; int min_age; /* resolve the root */ if (json_array_size(args) != 3) { send_error_response(client, "wrong number of arguments for 'debug-ageout'"); return; } root = resolve_root_or_err(client, args, 1, false); if (!root) { return; } min_age = json_integer_value(json_array_get(args, 2)); resp = make_response(); w_root_lock(root); w_root_perform_age_out(root, min_age); w_root_unlock(root); set_prop(resp, "ageout", json_true()); send_and_dispose_response(client, resp); w_root_delref(root); }
//----------------------------------------------------------------// json_t* _luaToJSON ( lua_State* L, int idx ) { switch ( lua_type ( L, idx )) { case LUA_TBOOLEAN: { bool value = lua_toboolean ( L, idx ) == 0 ? false : true; return value ? json_true () : json_false (); } case LUA_TTABLE: { return lua_objlen ( L, idx ) ? _luaToJSONArray ( L, idx ) : _luaToJSONObject ( L, idx ); } case LUA_TSTRING: { cc8* str = lua_tostring ( L, idx ); return json_string ( str ); } case LUA_TNUMBER: { double real = lua_tonumber ( L, idx ); return json_real ( real ); } case LUA_TLIGHTUSERDATA: { return json_null (); } }; return 0; }
void w_log_to_clients(int level, const char *buf) { json_t *json = NULL; w_ht_iter_t iter; if (!clients) { return; } pthread_mutex_lock(&w_client_lock); if (w_ht_first(clients, &iter)) do { struct watchman_client *client = w_ht_val_ptr(iter.value); if (client->log_level != W_LOG_OFF && client->log_level >= level) { json = make_response(); if (json) { set_mixed_string_prop(json, "log", buf); set_prop(json, "unilateral", json_true()); if (!enqueue_response(client, json, true)) { json_decref(json); } } } } while (w_ht_next(clients, &iter)); pthread_mutex_unlock(&w_client_lock); }
/* debug-ageout */ static void cmd_debug_ageout( struct watchman_client* client, const json_ref& args) { /* resolve the root */ if (json_array_size(args) != 3) { send_error_response(client, "wrong number of arguments for 'debug-ageout'"); return; } auto root = resolve_root_or_err(client, args, 1, false); if (!root) { return; } std::chrono::seconds min_age(json_integer_value(json_array_get(args, 2))); auto resp = make_response(); root->performAgeOut(min_age); resp.set("ageout", json_true()); send_and_dispose_response(client, std::move(resp)); }
/** * connects the device */ json_t * b_device_connect (json_t * device, void ** device_ptr) { char * param; json_t * j_param; if (device_ptr != NULL) { // Allocating *device_ptr for further use *device_ptr = (json_t *)json_pack("{s{sisi}s{sisi}s{sisi}s{s{sssfsos[sss]}s{sssfsos[sss]}}}", "switches", "sw1", 0, "sw2", 1, "dimmers", "di1", 42, "di2", 5, "dimmers_values", "di1", 42, "di2", 5, "heaters", "he1", "mode", "auto", "command", 18.0, "on", json_true(), "availableModes", "auto", "manual", "off", "he2", "mode", "manual", "command", 20.0, "on", json_false(), "availableModes", "auto", "manual", "off"); } if (o_strstr(json_string_value(json_object_get(json_object_get(device, "options"), "device_specified")), "batman") == NULL) { param = msprintf("%s says I'm batman with the alert_url %s", json_string_value(json_object_get(device, "name")), json_string_value(json_object_get(json_object_get(device, "options"), "alert_url"))); j_param = json_pack("{sis{ss}}", "result", WEBSERVICE_RESULT_OK, "options", "device_specified", param); o_free(param); } else { j_param = json_pack("{si}", "result", WEBSERVICE_RESULT_OK); } return j_param; }
json_t* nonstring(char* arg) { json_t* temp; char* endptr; if (!strcmp(arg, "null") || !strcmp(arg, "n")) {return json_null();} if (!strcmp(arg, "true") || !strcmp(arg, "t")) {return json_true();} if (!strcmp(arg, "false") || !strcmp(arg, "f")) {return json_false();} if (!strcmp(arg, "array") || !strcmp(arg, "[]")) {return json_array();} if (!strcmp(arg, "object") || !strcmp(arg, "{}")) {return json_object();} errno = 0; temp = json_integer(strtol(arg, &endptr, 10)); if (!errno && *endptr=='\0') {return temp;} errno = 0; temp = json_real(strtod(arg, &endptr)); if (!errno && *endptr=='\0') {return temp;} arg_err("parse error: illegal nonstring on arg %i, \"%s\""); return json_null(); }
json_t *playlist_to_json_set_collaborative(sp_playlist *playlist, json_t *object) { bool collaborative = sp_playlist_is_collaborative(playlist); json_object_set_new(object, "collaborative", collaborative ? json_true() : json_false()); return object; }
int cometd_dump_message(void * output, cometd_message * message, json_dump_callback_t callback) { json_t * root = json_object(); json_object_set_new(root, CHANNEL_FIELD, json_string_nocheck(message->channel)); if (message->id) json_object_set_new(root, ID_FIELD, json_string_nocheck(message->id)); if (message->clientId) json_object_set_new(root, CLIENT_ID_FIELD, json_string_nocheck(message->clientId)); json_object_set_new(root, SUCCESSFUL_FIELD, message->successful ? json_true() : json_false()); json_object_set_new(root, VERSION_FIELD, json_string_nocheck(message->version)); if (message->supportedConnectionTypes) json_object_set(root, SUPPORTED_CONNECTION_TYPES_FIELD, message->supportedConnectionTypes); if (message->connectionType) json_object_set_new(root, CONNECTION_TYPE_FIELD, json_string_nocheck(message->connectionType)); if (message->data) json_object_set(root, DATA_FIELD, message->data); if (message->ext) json_object_set(root, EXT_FIELD, message->ext); if (message->advice.present) { if (message->advice.sourceAdvice) json_object_set_new(root, ADVICE_FIELD, message->advice.sourceAdvice); else { json_t * advice = json_object(); if (message->advice.reconnect) json_object_set_new(advice, RECONNECT_FIELD, json_string_nocheck(message->advice.reconnect)); json_object_set_new(advice, INTERVAL_FIELD, json_integer(message->advice.interval)); json_object_set(root, DATA_FIELD, message->data); json_object_set_new(root, ADVICE_FIELD, advice); } } int result = json_dump_callback(root, callback, output, 0); json_decref(root); return result; }
static json_t *make_new(struct watchman_rule_match *match) { if (match->is_new) { return json_true(); } return NULL; }
// Called to initialize your node structure. void init(DSLink *link) { json_t *messageValue = dslink_json_get_config(link, "message"); if (messageValue) { log_info("Message = %s\n", json_string_value(messageValue)); } DSNode *superRoot = link->responder->super_root; DSNode *stringValueNode = dslink_node_create(superRoot, "string", "node"); dslink_node_set_meta(link, stringValueNode, "$type", json_string("string")); dslink_node_set_meta(link, stringValueNode, "$writable", json_string("write")); dslink_node_update_value_new(link, stringValueNode, json_string("Hello World!")); dslink_node_add_child(link, stringValueNode); responder_init_replicator(link, superRoot); responder_init_rng(link, superRoot); responder_init_invoke(link, superRoot); responder_init_serialization(link, superRoot); // add link data json_t * linkData = json_object(); json_object_set_nocheck(linkData, "test", json_true()); link->link_data = linkData; log_info("Initialized!\n"); }
json_t *bunser(const char *buf, const char *end, int *needed, json_error_t *jerr) { json_int_t ival; switch (buf[0]) { case BSER_INT8: case BSER_INT16: case BSER_INT32: case BSER_INT64: if (!bunser_int(buf, end - buf, needed, &ival)) { snprintf(jerr->text, sizeof(jerr->text), "invalid integer encoding"); return NULL; } return json_integer(ival); case BSER_STRING: { const char *start; json_int_t len; if (!bunser_string(buf, end - buf, needed, &start, &len)) { snprintf(jerr->text, sizeof(jerr->text), "invalid string encoding"); return NULL; } return json_string_binary(start, len); } case BSER_REAL: *needed = sizeof(double) + 1; return json_real(*(double*)(buf+1)); case BSER_TRUE: *needed = 1; return json_true(); case BSER_FALSE: *needed = 1; return json_false(); case BSER_NULL: *needed = 1; return json_null(); case BSER_ARRAY: return bunser_array(buf, end, needed, jerr); case BSER_TEMPLATE: return bunser_template(buf, end, needed, jerr); case BSER_OBJECT: return bunser_object(buf, end, needed, jerr); default: snprintf(jerr->text, sizeof(jerr->text), "invalid bser encoding type %02x", (int)buf[0]); return NULL; } return NULL; }
void JSON::setBoolean(const char *key, bool value) { if (!_json) { return; } json_object_set_new(_json, key, value ? json_true() : json_false()); }
json_t *janus_source_query_session(janus_plugin_session *handle) { if (g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) { return NULL; } janus_source_session *session = (janus_source_session *)handle->plugin_handle; if (!session) { JANUS_LOG(LOG_ERR, "No session associated with this handle...\n"); return NULL; } /* In the source plugin, every session is the same: we just provide some configure info */ json_t *info = json_object(); json_object_set_new(info, "audio_active", session->audio_active ? json_true() : json_false()); json_object_set_new(info, "video_active", session->video_active ? json_true() : json_false()); json_object_set_new(info, "bitrate", json_integer(session->bitrate)); json_object_set_new(info, "slowlink_count", json_integer(session->slowlink_count)); json_object_set_new(info, "destroyed", json_integer(session->destroyed)); return info; }
json_t *C_monitoring__remove(json_t *arg) { json_t *id = json_object_get(arg, "id"); if (!id) return NULL; item_t *item = find_item(id); if (!item) return NULL; item_free(item); return json_true(); }
static void cmd_shutdown(struct watchman_client *client, json_t *args) { json_t *resp = make_response(); unused_parameter(args); w_log(W_LOG_ERR, "shutdown-server was requested, exiting!\n"); w_request_shutdown(); set_prop(resp, "shutdown-server", json_true()); send_and_dispose_response(client, resp); }
PFILE_INFO_NODE ResponseParser::parseListRoot(char* json) { json_error_t error; json_t* root = json_loads(json,0, &error); if(!root) { fprintf(stderr, "error: on line %d: %s\n", error.line, error.text); //return 1; } json_t* items = json_object_get(root,"items"); if(!json_is_array(items)) { fprintf(stderr, "error: items is not an array\n"); //return 1; } PFILE_INFO_NODE start,current,end; start = current = end = NULL; for(int i = 0; i < json_array_size(items); i++) { json_t* data = json_array_get(items, i); json_t* parents = json_object_get(data, "parents"); PFILE_INFO_NODE node = NULL; if (parents != NULL) { json_t* parent = json_array_get(parents,0); json_t* is_root_json = json_object_get(parent,"isRoot"); if ( is_root_json == json_true()) { PFILE_INFO info = parseItem(data); if (info != NULL) { PFILE_INFO_NODE node = new FILE_INFO_NODE(); node->info = info; node->next = NULL; if (start == NULL) { start = end = current = node; } else { current->next = node; end = node; current = current->next; } } } } } return start; }
void JSON::setArrayBoolean(int index, bool value) { if (!isArray()) { return; } expandArray(index + 1); json_array_set(_json, index, value ? json_true() : json_false()); }
json_t *ConstructorInfoWriter::write() { json_t *json = json_object(); json_object_set(json, "type", json_string("CONSTRUCTOR")); json_object_set(json, "defaultconstructor", defaultConstructor ? json_true() : json_false()); MethodBaseWriter::write(json); return json; }
static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) { json_t *json; switch(lex->token) { case TOKEN_STRING: { json = json_string_nocheck(lex->value.string); break; } case TOKEN_INTEGER: { json = json_integer(lex->value.integer); break; } case TOKEN_REAL: { json = json_real(lex->value.real); break; } case TOKEN_TRUE: json = json_true(); break; case TOKEN_FALSE: json = json_false(); break; case TOKEN_NULL: json = json_null(); break; case '{': json = parse_object(lex, flags, error); break; case '[': json = parse_array(lex, flags, error); break; case TOKEN_INVALID: error_set(error, lex, "invalid token"); return NULL; default: error_set(error, lex, "unexpected token"); return NULL; } if(!json) return NULL; return json; }
//native Handle:json_true(); static cell_t Native_json_true(IPluginContext *pContext, const cell_t *params) { json_t *result = json_true(); Handle_t hndlResult = g_pHandleSys->CreateHandle(htJanssonObject, result, pContext->GetIdentity(), myself->GetIdentity(), NULL); if(hndlResult == BAD_HANDLE) { pContext->ThrowNativeError("Could not create <True> handle."); } return hndlResult; }
extern void error ( const errorSelection selection, const char *const format, ...) { va_list ap; va_start (ap, format); if (Option.json) { char *reason; vasprintf (&reason, format, ap); json_t *response = json_object (); json_object_set_new (response, "error", json_string (reason)); if (selected (selection, WARNING)) json_object_set_new (response, "warning", json_true ()); if (selected (selection, FATAL)) json_object_set_new (response, "fatal", json_true ()); if (selected (selection, PERROR)) { json_object_set_new (response, "errno", json_integer (errno)); json_object_set_new (response, "perror", json_string (strerror (errno))); } json_dumpf (response, stdout, 0); fprintf (stdout, "\n"); json_decref (response); free (reason); } else { fprintf (errout, "%s: %s", getExecutableName (), selected (selection, WARNING) ? "Warning: " : ""); vfprintf (errout, format, ap); if (selected (selection, PERROR)) #ifdef HAVE_STRERROR fprintf (errout, " : %s", strerror (errno)); #else perror (" "); #endif fputs ("\n", errout); } va_end (ap); if (! Option.json && selected (selection, FATAL)) exit (1); }
/*! * \brief Get the \ref json_mem block for a pointer allocated via * ast_json_malloc(). * * This function properly handles Jansson singletons (null, true, false), and * \c NULL. * * \param p Pointer, usually to a \c json_t or \ref ast_json. * \return \ref json_mem object with extra allocation info. */ static inline struct json_mem *to_json_mem(void *p) { struct json_mem *mem; /* Avoid ref'ing the singleton values */ if (p == NULL || p == json_null() || p == json_true() || p == json_false()) { return NULL; } mem = (struct json_mem *)((char *) (p) - sizeof(*mem)); ast_assert(mem->magic == JSON_MAGIC); return mem; }
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
/*==================================================================== * 函数名 : vGenSYSTIMESYNCMsg * 功能 : 主动上报物理服务器和NTP服务器时间同步状态 * 算法实现 : * 参数说明 : vpd 要上报的设备 * 返回值说明: 成功 生成的消息 * 失败 NULL * ---------------------------------------------------------------------- * 修改记录: * 日 期 版本 修改人 走读人 修改记录 * 2015/1/26 v1.0 YLI 创建 * ====================================================================*/ json_t * vGenSYSTIMESYNCMsg(VPD vpd) { json_t *root; char saLocalTime[256]; root = json_object(); //devid if (json_object_set(root,"devid", json_string(vpd.saDevId)) == FAILUER) { json_decref(root); vLogErr("devid set error!!!"); return NULL; } //devtype if (json_object_set(root,"devtype", json_string(vpd.saDevType)) == FAILUER) { json_decref(root); vLogErr("devtype set error!!!"); return NULL; } //eventid if (json_object_set(root,"eventid", json_string("EV_SYSTIME_SYNC")) == FAILUER) { json_decref(root); vLogErr("eventid set error!!!"); return NULL; } //rpttime memset(saLocalTime,0x00,sizeof saLocalTime); GetLocalTime(saLocalTime); if (json_object_set(root,"rpttime", json_string(saLocalTime)) == FAILUER) { json_decref(root); vLogErr("rpttime set error!!!"); return NULL; } //syncstate if (json_object_set(root,"syncstate", json_true()) == FAILUER) { json_decref(root); vLogErr("syncstate set error!!!"); return NULL; } return root; }
/** Send response data to client. Parameters: int success: Status about data json_t *data: Data for response int client_sockfd: connection */ void response_data(int success, json_t *data, int client_sockfd) { json_t *root = json_object(); if (success == 1) json_object_set(root, "success", json_true()); else json_object_set(root, "success", json_false()); json_object_set(root, "data", data); char *decode = json_dumps(root, 1); write(client_sockfd, decode, 500); json_decref(root); }
int32 rf_evt_pack_json(json_t *result, const int32 *evt_types, rf_evt_svc_t *service) { int32 i = 0; json_t *types_array = NULL; json_t *listener = NULL; json_t *status = NULL; json_object_add(result, RMM_JSON_ODATA_CONTEXT, json_string(service->links.odata_context)); json_object_add(result, RMM_JSON_ODATA_ID, json_string(service->links.odata_id)); add_json_string(result, RMM_JSON_ODATA_TYPE, service->fixed.odata_type); add_json_string(result, RMM_JSON_RF_ID, service->fixed.id); add_json_string(result, RMM_JSON_RF_NAME, service->fixed.name); status = json_object(); if (status != NULL) { add_json_string(status, RMM_JSON_RF_STATUS_STATE, service->fixed.status.state); add_json_string(status, RMM_JSON_RF_STATUS_HEALTH, service->fixed.status.health); } json_object_add(result, RMM_JSON_RF_STATUS, status); if (service->fixed.service_enabled) json_object_add(result, RMM_JSON_RF_SERVICE_ENABLED, json_true()); else json_object_add(result, RMM_JSON_RF_SERVICE_ENABLED, json_false()); add_json_integer(result, RMM_JSON_RF_DELIVERY_RETRY, service->fixed.retry); add_json_integer(result, RMM_JSON_RF_DELIVERY_RETRY_INTERVAL, service->fixed.retry_interval); types_array = json_array(); if (types_array == NULL) return -1; for (i = 0; i < MAX_EVT_ACTION_NUM; i++) { if (evt_types[i] == 0) continue; json_array_add(types_array, json_string((int8 *)rf_evt_msg[i].type_name)); } json_object_add(result, RMM_JSON_RF_SUPPORTED_EVT_TYPES, types_array); listener = json_object(); if (listener == NULL) return -1; add_json_string(listener, RMM_JSON_ODATA_ID, service->links.subscriptions); json_object_add(result, RMM_JSON_RF_SUBSCRIPTION, listener); return 0; }
void w_cancel_subscriptions_for_root(w_root_t *root) { w_ht_iter_t iter; pthread_mutex_lock(&w_client_lock); if (w_ht_first(clients, &iter)) { do { struct watchman_user_client *client = w_ht_val_ptr(iter.value); w_ht_iter_t citer; if (w_ht_first(client->subscriptions, &citer)) { do { struct watchman_client_subscription *sub = w_ht_val_ptr(citer.value); if (sub->root == root) { json_t *response = make_response(); w_log(W_LOG_ERR, "Cancel subscription %.*s for client:stm=%p due to " "root cancellation\n", sub->name->len, sub->name->buf, client->client.stm); set_prop(response, "root", w_string_to_json(root->root_path)); set_prop(response, "subscription", w_string_to_json(sub->name)); set_prop(response, "unilateral", json_true()); set_prop(response, "canceled", json_true()); if (!enqueue_response(&client->client, response, true)) { w_log(W_LOG_DBG, "failed to queue sub cancellation\n"); json_decref(response); } w_ht_iter_del(client->subscriptions, &citer); } } while (w_ht_next(client->subscriptions, &citer)); } } while (w_ht_next(clients, &iter)); } pthread_mutex_unlock(&w_client_lock); }
json_t* bser2json(bser_t* bser) { if (bser_is_integer(bser)) { json_int_t v = bser_integer_value(bser); return json_integer(v); } else if (bser_is_real(bser)) { double v = bser_real_value(bser); return json_real(v); } else if (bser_is_true(bser)) { return json_true(); } else if (bser_is_false(bser)) { return json_false(); } else if (bser_is_null(bser)) { return json_null(); } else if (bser_is_string(bser)) { size_t length; const char* str = bser_string_value(bser, &length); char* dup = strndup(str, length); json_t* string = json_string(dup); free(dup); return string; } else if (bser_is_array(bser)) { size_t length = bser_array_size(bser); json_t* array = json_array(); for (int i = 0; i < length; ++i) { json_array_append_new(array, bser2json(bser_array_get(bser, i))); } return array; } else if (bser_is_object(bser)) { size_t length = bser_object_size(bser); json_t* object = json_object(); for (int i = 0; i < length; ++i) { size_t key_length; bser_t* key = bser_object_key_at(bser, i); assert(bser_is_string(key)); bser_t* value = bser_object_value_at(bser, i); if (!bser_is_no_field(value)) { const char* key_chars = bser_string_value(key, &key_length); assert(key_chars != NULL && *key_chars != '\0'); char* key_dup = strndup(key_chars, key_length); json_object_set_new(object, key_dup, bser2json(value)); free(key_dup); } } return object; } else { return NULL; } }
static json_t *json_serialize_pspec (const GValue *value) { /* Only types in json-glib but G_TYPE_BOXED */ switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value))) { case G_TYPE_STRING: if (!g_value_get_string (value)) break; else return json_string (g_value_get_string (value)); case G_TYPE_BOOLEAN: if (g_value_get_boolean (value)) return json_true (); else return json_false (); case G_TYPE_INT: return json_integer (g_value_get_int (value)); case G_TYPE_UINT: return json_integer (g_value_get_uint (value)); case G_TYPE_LONG: return json_integer (g_value_get_long (value)); case G_TYPE_ULONG: return json_integer (g_value_get_ulong (value)); case G_TYPE_INT64: return json_integer (g_value_get_int64 (value)); case G_TYPE_FLOAT: return json_real (g_value_get_float (value)); case G_TYPE_DOUBLE: return json_real (g_value_get_double (value)); case G_TYPE_CHAR: return json_integer (g_value_get_schar (value)); case G_TYPE_UCHAR: return json_integer (g_value_get_uchar (value)); case G_TYPE_ENUM: return json_integer (g_value_get_enum (value)); case G_TYPE_FLAGS: return json_integer (g_value_get_flags (value)); case G_TYPE_NONE: break; case G_TYPE_OBJECT: { GObject *object = g_value_get_object (value); if (object) return json_gobject_serialize (object); } break; defalut: g_warning("Unsuppoted type `%s'",g_type_name (G_VALUE_TYPE (value))); } return json_null(); }