static int generic_object(struct json_object **dstp, struct json_object *jobj,
		const char *key, enum odict_type type)
{
	struct json_object *dst;

	if (!dstp || !jobj || !key)
		return EINVAL;

	if (!json_object_object_get_ex(jobj, key, &dst)) {
		return ENOENT;
	}
	if (!json_object_is_type(dst, type)) {
		return EPROTO;
	}
	*dstp = dst;
	return 0;
}
int jzon_double(double *dst, struct json_object *obj, const char *key)
{
	struct json_object *value;
	enum odict_type type;

	if (!dst || !obj || !key)
		return EINVAL;

	if (!json_object_object_get_ex(obj, key, &value))
		return ENOENT;
	type = json_object_get_type(value);
	if (type != json_type_double && type != json_type_int)
		return EPROTO;

	*dst = json_object_get_double(value);
	return 0;
}
Exemple #3
0
int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
{
	str json_s;
	str field_s;
	pv_spec_t *dst_pv;
	pv_value_t dst_val;
	char *value;
	struct json_object *j = NULL;
	struct json_object *oj = NULL;
	int ret;

	if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
		LM_ERR("cannot get json string value\n");
		return -1;
	}

	if (fixup_get_svalue(msg, (gparam_p)field, &field_s) != 0) {
		LM_ERR("cannot get field string value\n");
		return -1;
	}

	dst_pv = (pv_spec_t *)dst;


	j = json_tokener_parse(json_s.s);

	if (j==NULL) {
		LM_ERR("empty or invalid JSON\n");
		return -1;
	}

	json_object_object_get_ex(j, field_s.s, &oj);
	if(oj!=NULL) {
		value = (char*)json_object_to_json_string(oj);
		dst_val.rs.s = value;
		dst_val.rs.len = strlen(value);
		dst_val.flags = PV_VAL_STR;
		dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);
		ret = 1;
	} else {
		ret = -1;
	}

	json_object_put(j);
	return ret;
}
Exemple #4
0
static struct json_object *
jp_match_next(struct jp_opcode *ptr,
              struct json_object *root, struct json_object *cur,
              jp_match_cb_t cb, void *priv)
{
	int idx;
	struct json_object *next = NULL;

	if (!ptr)
	{
		if (cb)
			cb(cur, priv);

		return cur;
	}

	switch (ptr->type)
	{
	case T_STRING:
	case T_LABEL:
		if (json_object_object_get_ex(cur, ptr->str, &next))
			return jp_match_next(ptr->sibling, root, next, cb, priv);

		break;

	case T_NUMBER:
		idx = ptr->num;

		if (idx < 0)
			idx += json_object_array_length(cur);

		if (idx >= 0)
			next = json_object_array_get_idx(cur, idx);

		if (next)
			return jp_match_next(ptr->sibling, root, next, cb, priv);

		break;

	default:
		return jp_match_expr(ptr, root, cur, cb, priv);
	}

	return NULL;
}
Exemple #5
0
static char *
get_stream_url(struct movie_entry *e)
{
	char url[1000];
	char name[100];
	json_object *root;
	json_object *obj;
	json_bool jres;
	const char *format_ext = (e->format == SF_MP4) ? "mp4" : "wmv";

	provider->error_number = 0;

	if (e->format == SF_MP4) {
		snprintf(url, 499, "%svideo/media/%d/watch.json?format=%s&protocol=hls&bitrate=%d",
			 api_root, e->id, format_ext, e->bitrate);
	} else {
		snprintf(url, 499, "%svideo/media/%d/watch.json?format=%s&bitrate=%d",
			 api_root, e->id, format_ext, e->bitrate);
	}

	snprintf(name, 99, "stream-%d", e->id);
	logi("fetch %s to %s", url, name);

	root = get_cached(url, name);
	if (provider->error_number != 0)
		return NULL;

	int status = get_int(root, "status_code");
	if (status != 200) {
		sprintf(last_error, "get stream status: %d", status);
		provider->error_number = 1;
		return NULL;
	}

	jres = json_object_object_get_ex(root, "data", &obj);
	if (jres == FALSE) {
		sprintf(last_error, "Cannot get stream data");
		provider->error_number = 1;
		return  NULL;
	}

	char *stream_url = strdup(get_str(obj, "url"));
	json_object_put(root);
	return stream_url;
}
static GError *
_load_hash (struct json_object *obj, const gchar *k, GHashTable *dst)
{
	struct json_object *sub = NULL;

	if (!json_object_object_get_ex(obj, k, &sub))
		return NULL;
	if (!json_object_is_type(sub, json_type_object))
		return NEWERROR(CODE_BAD_REQUEST, "Invalid '%s' field", k);

	json_object_object_foreach(sub,key,val) {
		if (!json_object_is_type(val, json_type_string))
			continue;
		g_hash_table_insert(dst, g_strdup(key), metautils_gba_from_string(
					json_object_get_string(val)));
	}
	return NULL;
}
Exemple #7
0
/**
 * Get value by key.
 * @param  osName Key name.
 * @return         Json array object.
 *
 * @since GDAL 2.3
 */
CPLJSONArray CPLJSONObject::GetArray(const std::string &osName) const
{
    std::string objectName;
    CPLJSONObject object = GetObjectByPath( osName, objectName );
    if( object.IsValid() )
    {
        json_object *poVal = nullptr;
        if( json_object_object_get_ex( TO_JSONOBJ(object.GetInternalHandle()),
                                       objectName.c_str(), &poVal ) )
        {
            if( poVal && json_object_get_type( poVal ) == json_type_array )
            {
                return CPLJSONArray( objectName, poVal );
            }
        }
    }
    return CPLJSONArray( "", nullptr );
}
void CategoryStats::deserialize(json_object *o) {
  if(!o) return;

  /* Reset all */
  memset(categories, 0, ntop->get_flashstart()->getNumCategories()*sizeof(u_int64_t));

  for(int id=0; id<ntop->get_flashstart()->getNumCategories(); id++) {
    char *name = ntop->get_flashstart()->getCategoryName(id);

    if(name != NULL) {
      json_object *bytes;

      if(json_object_object_get_ex(o, name, &bytes)) {
	categories[id] = json_object_get_int64(bytes);
      }
    }
  }
}
Exemple #9
0
static
int processor_setup_minute(processor_state_t *self, json_object *request)
{
    // we know that started_at data is valid since we already checked that
    // when determining which processor to call
    int minute = 0;
    json_object *started_at_obj = NULL;
    if (json_object_object_get_ex(request, "started_at", &started_at_obj)) {
        const char *started_at = json_object_get_string(started_at_obj);
        char hours[3] = {started_at[11], started_at[12], '\0'};
        char minutes[3] = {started_at[14], started_at[15], '\0'};
        minute = 60 * atoi(hours) + atoi(minutes);
    }
    json_object *minute_obj = json_object_new_int(minute);
    json_object_object_add(request, "minute", minute_obj);
    // printf("[D] minute: %d\n", minute);
    return minute;
}
Exemple #10
0
/* search a key (what) in object "where", and return a pointer to its
 * associated object. If nullable is false, exit if key is not found */
static inline struct json_object*
get_in_object(struct json_object *where,
	      const char *what,
	      int nullable)
{
	struct json_object *to;
	json_bool ret;
	ret = json_object_object_get_ex(where, what, &to);
	if (!nullable && !ret) {
		log_critical(PFX "Error while parsing config\n" PFL);
		exit(EXIT_INV_CONFIG);
	}
	if (!nullable && strcmp(json_object_to_json_string(to), "null") == 0) {
		log_critical(PFX "Cannot find key %s", what);
		exit(EXIT_INV_CONFIG);
	}
	return to;
}
const char *get_field_string_json(json_object *e, const char *path)
{
	const char *str;
	json_object *node;

	if (path) {
		if (!json_object_object_get_ex(e, path, &node))
			return NULL;
	} else
		node = e;

	if (json_object_get_type(node) == json_type_string) {
		str = json_object_get_string(node);

		return str;
	}

	return NULL;
}
Exemple #12
0
char *pocket_auth(const char *key, const char *token)
{ 
    struct buffer s;
    json_object *response;
    char* access_token;

    buffer_init(&s);

    request(URL_AUTHORIZE, create_auth_petition(key, token), &s, 0);

    response = json_tokener_parse(s.ptr);
    json_object_object_get_ex(response, "access_token", &response);
    access_token = (char *)json_object_get_string(response);

    free(response);
    buffer_cleanup(&s);

    return access_token;
}
Exemple #13
0
char *pocket_get_token(const char *key) 
{
    struct buffer s;
    json_object *response;
    char *token;

    buffer_init(&s);

    request(URL_REQUEST, create_request_petition(key), &s, 0);

    response = json_tokener_parse(s.ptr);
    json_object_object_get_ex(response, "code", &response);
    token = (char *)json_object_get_string(response);

    free(response);
    buffer_cleanup(&s);

    return token;
}
static const digest_handler *LUKS2_digest_handler(struct crypt_device *cd, int digest)
{
	struct luks2_hdr *hdr;
	json_object *jobj1, *jobj2;

	if (digest < 0)
		return NULL;

	if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
		return NULL;

	if (!(jobj1 = LUKS2_get_digest_jobj(hdr, digest)))
		return NULL;

	if (!json_object_object_get_ex(jobj1, "type", &jobj2))
		return NULL;

	return LUKS2_digest_handler_type(cd, json_object_get_string(jobj2));
}
Exemple #15
0
static void pretty_print_input(json_object *i) {
	json_object *id, *name, *size, *caps;
	json_object_object_get_ex(i, "identifier", &id);
	json_object_object_get_ex(i, "name", &name);
	json_object_object_get_ex(i, "size", &size);
	json_object_object_get_ex(i, "capabilities", &caps);

	printf( "Input device %s\n  Type: ", json_object_get_string(name));

	struct {
		const char *a;
		const char *b;
	} cap_names[] = {
		{ "keyboard", "Keyboard" },
		{ "pointer", "Mouse" },
		{ "touch", "Touch" },
		{ "tablet_tool", "Tablet tool" },
		{ "tablet_pad", "Tablet pad" },
		{ "gesture", "Gesture" },
		{ "switch", "Switch" },
	};

	size_t len = json_object_array_length(caps);
	if (len == 0) {
		printf("Unknown");
	}

	json_object *cap;
	for (size_t i = 0; i < len; ++i) {
		cap = json_object_array_get_idx(caps, i);
		const char *cap_s = json_object_get_string(cap);
		const char *_name = NULL;
		for (size_t j = 0; j < sizeof(cap_names) / sizeof(cap_names[0]); ++i) {
			if (strcmp(cap_names[i].a, cap_s) == 0) {
				_name = cap_names[i].b;
				break;
			}
		}
		printf("%s%s", _name ? _name : cap_s, len > 1 && i != len - 1 ? ", " : "");
	}
	printf("\n  Sway ID: %s\n", json_object_get_string(id));
	if (size) {
		json_object *width, *height;
		json_object_object_get_ex(size, "width", &width);
		json_object_object_get_ex(size, "height", &height);
		printf("  Size: %lfmm x %lfmm\n",
				json_object_get_double(width), json_object_get_double(height));
	}
	printf("\n");
}
Exemple #16
0
static void * recv_start(void*arg)
{
    struct sockaddr_in addr;
    char buffer[100];
    char *buf  = (char*)malloc(1000);
    if(!buf)
    {
        printf("malloc fialed!\n");
        return;
    }
    socklen_t addrlen;
    struct json_object * jsonobj;
    
    while(1)
    {
        struct json_object *json_opr;
        recvfrom(c2java_socket,&buffer,1000,0,(struct sockaddr*)(&addr),&addrlen);
        jsonobj = json_tokener_parse((const char*)&buffer);
        json_object_object_get_ex(jsonobj,"opr",&json_opr);
        char *opr = json_object_get_string(json_opr);
        if(is_error(jsonobj))
        {
            printf("json failed!\n");
        }
        if(!strcmp(opr,"start"))
        {
            display_endpoint(buf);
            sendto(c2java_socket,buf,strlen(buf),0,(struct sockaddr*)(&addr),sizeof(struct sockaddr));
        }
        else if(!strcmp(opr,"put"))
        {
            
        }
        

        json_object_put(jsonobj);

        display_endpoint(buf);
        printf("name:%s\n",buf);
        *buf = '\0';
    }
}
/**
 * Given a JSON object representing an OpenStack service, find the first endpoint of the given version.
 * If an endpoint of the given version is found, return it.
 * Otherwise, if no endpoint of the given version is found, return NULL.
 */
static struct json_object *
service_find_endpoint_by_version(keystone_context_t *context, struct json_object *service, unsigned int desired_api_version)
{
	struct json_object *endpoints, *endpoint;

	if (json_object_object_get_ex(service, "endpoints", &endpoints)) {
		if (0 == desired_api_version) {
			/* No desired API version currently set, so use the first endpoint found */
			endpoint = json_array_get(context, endpoints, 0);
		} else {
			/* Looking for a certain version of the Swift RESTful API */
			endpoint = json_array_find(context, endpoints, filter_endpoint_by_version, (void *) &desired_api_version);
		}
	} else {
		context->keystone_error("response.access.serviceCatalog[n] lacks an 'endpoints' key", KSERR_PARSE);
		endpoint = NULL; /* Lacking the expected key */
	}

	return endpoint;
}
Exemple #18
0
bool handle_ipc_event(struct bar *bar) {
	struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
	if (!resp) {
		return false;
	}
	switch (resp->type) {
	case IPC_EVENT_WORKSPACE:
		ipc_update_workspaces(bar);
		break;
	case IPC_EVENT_MODE: {
		json_object *result = json_tokener_parse(resp->payload);
		if (!result) {
			free_ipc_response(resp);
			sway_log(L_ERROR, "failed to parse payload as json");
			return false;
		}
		json_object *json_change;
		if (json_object_object_get_ex(result, "change", &json_change)) {
			const char *change = json_object_get_string(json_change);

			free(bar->config->mode);
			if (strcmp(change, "default") == 0) {
				bar->config->mode = NULL;
			} else {
				bar->config->mode = strdup(change);
			}
		} else {
			sway_log(L_ERROR, "failed to parse response");
		}

		json_object_put(result);
		break;
	}
	default:
		free_ipc_response(resp);
		return false;
	}

	free_ipc_response(resp);
	return true;
}
GError *
m2v2_json_load_single_xbean (struct json_object *j, gpointer *pbean)
{
	if (!json_object_is_type (j, json_type_object))
		return NEWERROR(CODE_BAD_REQUEST, "Invalid object type");
	struct json_object *jtype = NULL;
	if (!json_object_object_get_ex (j, "type", &jtype))
		return NEWERROR(CODE_BAD_REQUEST, "Missing 'type' field");
	if (!json_object_is_type (jtype, json_type_string))
		return NEWERROR(CODE_BAD_REQUEST, "Invalid 'type' field");

	const char *stype = json_object_get_string (jtype);
	if (!g_ascii_strcasecmp(stype, "alias"))
		return m2v2_json_load_single_alias (j, pbean);
	if (!g_ascii_strcasecmp(stype, "header"))
		return m2v2_json_load_single_header (j, pbean);
	if (!g_ascii_strcasecmp(stype, "chunk"))
		return m2v2_json_load_single_chunk (j, pbean);

	return NEWERROR(CODE_BAD_REQUEST, "Unexpected 'type' field");
}
/**
 * @ingroup VuoMathExpression
 * Decodes the JSON object to create a new value.
 *
 * @eg{
 *   {
 *     "expression" : "y = x + 4"
 *   }
 * }
 */
VuoMathExpression VuoMathExpression_makeFromJson(json_object *js)
{
	VuoMathExpression me;
	json_object *o = NULL;

	if (json_object_object_get_ex(js, "expression", &o))
	{
		me.expression = VuoText_makeFromJson(o);

		VuoMathExpressionError error = NULL;
		me.parser = VuoMathExpressionParser_makeFromSingleExpression(me.expression, &error);
		free(error);
	}
	else
	{
		me.expression = VuoText_make("");
		me.parser = NULL;
	}

	return me;
}
Exemple #21
0
static json_object *ReadMetaFile(const char *metafile) {
  char *buf, *buf2;
  size_t size;
  json_object *obj, *result;

  if (!g_file_get_contents(metafile, &buf, &size, NULL)) {
    return NULL;
  }
  buf2 = strndup(buf, size);
  obj = json_tokener_parse(buf2);
  g_free(buf);
  g_free(buf2);

  if (is_error(obj)) {
    return NULL;
  }
  if (!json_object_object_get_ex(obj, "result", &result)) {
    return NULL;
  }
  return obj;
}
/** Get monitor command
  @param monitor Monitor to save command
  @param json_monitor JSON monitor to extract command
  */
static const char *
extract_monitor_cmd(enum monitor_cmd_type *type,
		    /* @todo const */ struct json_object *json_monitor) {
	static const char *cmd_keys[] = {
#define _X(menum, cmd, type, fn) cmd,
			MONITOR_CMDS_X
#undef _X
	};

	for (size_t i = 0; i < RD_ARRAYSIZE(cmd_keys); ++i) {
		struct json_object *json_cmd_arg = NULL;
		const bool get_rc = json_object_object_get_ex(
				json_monitor, cmd_keys[i], &json_cmd_arg);
		if (get_rc) {
			*type = i;
			return json_object_get_string(json_cmd_arg);
		}
	}

	return NULL;
}
Exemple #23
0
telebot_error_e telebot_parser_get_file_path(struct json_object *obj,
        char **path) {

    if (obj == NULL)
        return TELEBOT_ERROR_INVALID_PARAMETER;

    if (path == NULL)
        return TELEBOT_ERROR_INVALID_PARAMETER;

    struct json_object *file_path;
    if (json_object_object_get_ex (obj, "file_path", &file_path)) {
        *path = strdup(json_object_get_string(file_path));
        json_object_put (file_path);
    }
    else {
        *path = NULL;
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    return TELEBOT_ERROR_NONE;
}
static void install(struct afb_req request)
{
	struct json_object *obj, *added;
	char *query;
	const char *filename;
	struct afb_arg arg;

	/* get the argument */
	arg = afb_req_get(request, "widget");
	filename = arg.path;
	if (filename == NULL) {
		afb_req_fail(request, "bad-request", "missing 'widget' file");
		return;
	}

	/* makes the query */
	if (0 >= asprintf(&query, "\"%s\"", filename)) {
		afb_req_fail(request, "server-error", "out of memory");
		return;
	}

	obj = jbus_call_sj_sync(jbus, _install_, query);
	if (afb_interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
			obj ? json_object_to_json_string(obj) : "NULL");
	free(query);

	/* check status */
	if (obj == NULL) {
		afb_req_fail(request, "failed", "framework daemon failure");
		return;
	}

	/* embed if needed */
	if (json_object_object_get_ex(obj, _added_, &added))
		obj = added;
	obj = json_object_get(obj);
	obj = embed(_id_, obj);
	afb_req_success(request, obj, NULL);
}
Exemple #25
0
static void bodhi_read_value(json_object *json, const char *item_name,
                             void *value, int flags)
{
    json_object *j = NULL;
    if (!json_object_object_get_ex(json, item_name, &j))
    {
        error_msg("'%s' section is not available", item_name);
        return;
    }

    switch (flags) {
    case BODHI_READ_INT:
        *(int *) value = json_object_get_int(j);
        break;
    case BODHI_READ_STR:
        *(char **) value = (char *) strtrimch(xstrdup(json_object_to_json_string(j)), '"');
        break;
    case BODHI_READ_JSON_OBJ:
        *(json_object **) value = (json_object *) j;
        break;
    };
}
int jzon_strdup(char **dst, struct json_object *obj, const char *key)
{
	struct json_object *value;
	enum odict_type type;

	if (!dst || !obj || !key)
		return EINVAL;

	if (!json_object_object_get_ex(obj, key, &value))
		return ENOENT;
	type = json_object_get_type(value);
	if (type == json_type_string) {
		return str_dup(dst, json_object_get_string(value));
	}
	else if (type == json_type_null) {
		*dst = NULL;
		return 0;
	}
	else {
		return EPROTO;
	}
}
Exemple #27
0
static void handle_message(struct worker* client, const char* data){
	struct json_object* json = json_tokener_parse(data);
	if ( !json ){
		logmsg("Failed to parse JSON\n");
		return;
	}

	struct json_object* type;
	if ( !json_object_object_get_ex(json, "type", &type) ){
		logmsg("message missing type\n");
		return;
	}

	const char* type_str = json_object_get_string(type);
	if ( strcmp(type_str, "update") == 0 ){
		handle_update(json);
	} else {
		logmsg("unhandled message type %s\n", type_str);
	}

	json_object_put(json);
}
Exemple #28
0
mraa_result_t
mraa_init_json_platform_get_index(json_object* jobj, const char* io, const char* key, int index, int* pos, int upper)
{
    json_object* jobj_temp = NULL;
    if (json_object_object_get_ex(jobj, key, &jobj_temp)) {
        if (!json_object_is_type(jobj_temp, json_type_int)) {
            syslog(LOG_ERR, "init_json_platform: %s index at position: %d not an int", io, index);
            return MRAA_ERROR_INVALID_RESOURCE;
        }

        *pos = (int) json_object_get_int(jobj_temp);
        if (*pos < 0 || *pos > upper) {
            syslog(LOG_ERR,
                   "init_json_platform: %s %s at position: %d, gave: %d which was out of range", io,
                   key, index, *pos);
            return MRAA_ERROR_INVALID_RESOURCE;
        }
        return MRAA_SUCCESS;
    }
    syslog(LOG_ERR, "init_json_platform: An %s was not found for the %s", key, io);
    return MRAA_ERROR_NO_DATA_AVAILABLE;
}
Exemple #29
0
void NdpiStats::deserialize(NetworkInterface *iface, json_object *o) {
  if(!o) return;

  /* Reset all */
  for(int i=0; i<MAX_NDPI_PROTOS; i++) if(counters[i] != NULL) free(counters[i]);
  memset(counters, 0, sizeof(counters));

  for(int proto_id=0; proto_id<MAX_NDPI_PROTOS; proto_id++) {
    char *name = iface->get_ndpi_proto_name(proto_id);

    if(name != NULL) {
      json_object *obj;

      if(json_object_object_get_ex(o, name, &obj)) {
	json_object *bytes, *packets;

	if((counters[proto_id] = (ProtoCounter*)malloc(sizeof(ProtoCounter))) != NULL) {
	  if(json_object_object_get_ex(obj, "bytes", &bytes)) {
	    json_object *sent, *rcvd;

	    if(json_object_object_get_ex(bytes, "sent", &sent))
	      counters[proto_id]->bytes.sent = json_object_get_int64(sent);

	    if(json_object_object_get_ex(bytes, "rcvd", &rcvd))
	      counters[proto_id]->bytes.rcvd = json_object_get_int64(rcvd);
	  }

	  if(json_object_object_get_ex(obj, "packets", &packets)) {
	    json_object *sent, *rcvd;

	    if(json_object_object_get_ex(bytes, "sent", &sent))
	      counters[proto_id]->packets.sent = json_object_get_int64(sent);

	    if(json_object_object_get_ex(bytes, "rcvd", &rcvd))
	      counters[proto_id]->packets.rcvd = json_object_get_int64(rcvd);
	  }
	}
      }
    }
  }
}
Exemple #30
0
    void bot::thread()
    {
        std::string message;

        while(!this->quit)
        {
            message = this->janus_servers[this->server]->read();
            if(message.size())
            {
                json_object *json_command = NULL, *temp = NULL;
                json_command = json_tokener_parse(message.c_str());

                std::string method;

                try
                {
                    if(!json_object_object_get_ex(json_command, "method", &temp))  throw "'method' not found in command: " + message;
                }
                catch(std::string e)
                {
                    output(e);
                    json_object_put(json_command);
                    json_object_put(temp);
                    return;
                }
                method = json_object_get_string(temp);

                json_object_put(json_command);
                json_object_put(temp);

                if(this->server_method[method] != NULL) (this->server_method[method])(this, message);
                else output("Unknown method " + method);
            }

            this->update_pos();
            usleep(200000);
        }
    }