Beispiel #1
0
/* 判断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;
	}
}
Beispiel #2
0
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
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);
}
Beispiel #6
0
// 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;
}
Beispiel #8
0
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);
}
Beispiel #9
0
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;
}
Beispiel #10
0
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();
}
Beispiel #11
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;
}
Beispiel #12
0
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();
}
Beispiel #13
0
static kbool_t IsJsonType(struct JsonBuf *jsonbuf, KJSONTYPE type)
{
	switch(type) {
		case KJSON_OBJECT:   return json_is_object(jsonbuf->jsonobj);
		case KJSON_ARRAY:    return json_is_array(jsonbuf->jsonobj);
		case KJSON_STRING:   return json_is_string(jsonbuf->jsonobj);
		case KJSON_INT:      return json_is_integer(jsonbuf->jsonobj);
		case KJSON_DOUBLE:   return json_is_real(jsonbuf->jsonobj);
		case KJSON_BOOLEAN:  return json_is_boolean(jsonbuf->jsonobj);
		case KJSON_NULL:     return json_is_null(jsonbuf->jsonobj);
		case KJSON_INT64:    return json_is_integer(jsonbuf->jsonobj);
		case KJSON_LONG:     return json_is_integer(jsonbuf->jsonobj);
	}
	return false;
}
Beispiel #14
0
bool JSON::getArrayBoolean(int index)
{
    if (!isArray())
    {
        return false;
    }

    json_t *jobject = json_array_get(_json, index);

    if (!jobject || !json_is_boolean(jobject))
    {
        return false;
    }

    return json_is_true(jobject);
}
Beispiel #15
0
bool JSON::getBoolean(const char *key)
{
    if (!_json)
    {
        return false;
    }

    json_t *jbool = json_object_get(_json, key);

    if (!jbool || !json_is_boolean(jbool))
    {
        return false;
    }

    return json_is_true(jbool);
}
Beispiel #16
0
// 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;

  *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;
    }
    prepend_watchmanconfig_to_array(ref);
    json_incref(ref);
    *enforcing = true;
    return ref;
  }

  // Synthesize our conservative default value.
  // .watchmanconfig MUST be first
  return json_pack("[ssss]", ".watchmanconfig", ".hg", ".git", ".svn");
}
int read_cfg_bool(json_t *root, const char *key, bool *val, bool required, bool default_val)
{
    json_t *node = json_object_get(root, key);
    if (!node) {
        if (required) {
            return -__LINE__;
        } else {
            *val = default_val;
            return 0;
        }
    }
    if (!json_is_boolean(node))
        return -__LINE__;
    *val = json_boolean_value(node);

    return 0;
}
Beispiel #18
0
static qeo_retcode_t object_add_member(const qeo_factory_t *factory, qeocore_type_t *type, const char *member_name, json_t *member)
{
    qeo_retcode_t       result        = QEO_EFAIL;
    qeocore_type_t      *qeoType      = NULL;
    qeocore_member_id_t qeo_member_id = QEOCORE_MEMBER_ID_DEFAULT;
    json_t              *member_key   = json_object_get(member, KEY_KEY); // optional  => BOOLEAN
    json_t              *member_id    = json_object_get(member, KEY_ID);  // optional  => INTxx

    assert(factory != NULL);

    if (((NULL != member_key) && !json_is_boolean(member_key)) ||
        ((NULL != member_id) && !json_is_integer(member_id)) ||
        (NULL == member_name)) {
        // syntax error
        return QEO_EINVAL;
    }

    qeo_log_d("Processing %s", member_name);
    qeoType = build_member_type(factory, member);
    if (NULL == qeoType) {
        qeo_log_e("Could not build member_type");
        return result;
    }

    bool is_key = (member_key && json_is_true(member_key));
    do {
        result = qeocore_type_struct_add(type,                            // container
                qeoType,                         // new member to add
                member_name,                     // name of member
                &qeo_member_id,                  // member id
                is_key ? QEOCORE_FLAG_KEY : 0);  // flag
        if (QEO_OK != result) {
            qeo_log_e("qeocore_type_struct_add failed for member %s", member_name);
            break;
        }

        qeocore_type_free(qeoType);

        // Modify the json member to add/update the qeo member id
        json_object_set_new(member, KEY_ID, json_integer(qeo_member_id));
    } while (0);


    return result;
}
Beispiel #19
0
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;
	}
}
Beispiel #20
0
static cxConstChars jsonToConstChars(json_t *v)
{
    CX_RETURN(v == NULL, NULL);
    cxConstChars str = NULL;
    v = jsonParseRegisterValue(v);
    if(json_is_string(v)){
        str = json_string_value(v);
    }else if(json_is_integer(v)){
        str = cxConstStr("%"JSON_INTEGER_FORMAT,json_integer_value(v));
    }else if(json_is_real(v)){
        str = cxConstStr("%f",json_real_value(v));
    }else if(json_is_boolean(v)){
        str = cxConstStr("%s",json_is_true(v)?"true":"false");
    }else if(json_is_object(v)){
        str = jsonStrConvert(v);
    }
    return str;
}
Beispiel #21
0
QMap<QString, QVariant> mapFromJSON(json_t *json, json_error_t *error)
{
    QMap<QString, QVariant> dict;
    void *member;
    const char *key;
    json_t *value;

    for (member = json_object_iter(json); member; member = json_object_iter_next(json, member)) {
        key = json_object_iter_key(member);
        value = json_object_iter_value(member);

        QString k = QString::fromUtf8(key);
        QVariant v;

        // json_is_object(const json_t *json)
        // json_is_array(const json_t *json)
        // json_is_string(const json_t *json)
        // json_is_integer(const json_t *json)
        // json_is_real(const json_t *json)
        // json_is_true(const json_t *json)
        // json_is_false(const json_t *json)
        // json_is_null(const json_t *json)
        if (json_is_object(value)) {
            v = mapFromJSON(json, 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()) {
            dict[k] = v;
        }
    }
    return dict;
}
Beispiel #22
0
static inline int device_set_boolean(setter_boolean fn, struct Device *device,
                                     json_t *value, const char *context,
                                     struct MbDeviceJsonError *error)
{
    int ret;

    if (!json_is_boolean(value)) {
        json_error_set_mismatched_type(
                error, context, value->type, JSON_BOOLEAN);
        return -1;
    }

    ret = fn(device, json_boolean_value(value));
    if (ret < 0) {
        json_error_set_standard_error(error, ret);
        return -1;
    }

    return 0;
}
Beispiel #23
0
static void cmd_debug_set_subscriptions_paused(
    struct watchman_client* clientbase,
    const json_ref& args) {
  auto client = (struct watchman_user_client*)clientbase;

  const auto& paused = args.at(1);
  auto& paused_map = paused.object();
  for (auto& it : paused_map) {
    auto sub_iter = client->subscriptions.find(it.first);
    if (sub_iter == client->subscriptions.end()) {
      send_error_response(
          client,
          "this client does not have a subscription named '%s'",
          it.first.c_str());
      return;
    }
    if (!json_is_boolean(it.second)) {
      send_error_response(
          client,
          "new value for subscription '%s' not a boolean",
          it.first.c_str());
      return;
    }
  }

  auto states = json_object();

  for (auto& it : paused_map) {
    auto sub_iter = client->subscriptions.find(it.first);
    bool old_paused = sub_iter->second->debug_paused;
    bool new_paused = json_is_true(it.second);
    sub_iter->second->debug_paused = new_paused;
    states.set(
        it.first,
        json_object({{"old", json_boolean(old_paused)}, {"new", it.second}}));
  }

  auto resp = make_response();
  resp.set("paused", std::move(states));
  send_and_dispose_response(clientbase, std::move(resp));
}
Beispiel #24
0
void Config::load(const char *path)
{
    json_error_t error;
    json_t* json = json_load_file(path, 0, &error);
        
    this->useAuth = false;
    this->authHash = "";
    this->authSalt = "";

    if(!json) {
		/* didn't find config file, setup default password */
		setAuth(true, OBS_REMOTE_DEFAULT_PASSWORD);
		save(path);
        return;        
    }

    json_t* jUseAuth = json_object_get(json, "useAuth");
    json_t* jAuthHash = json_object_get(json, "authHash");
    json_t* jAuthSalt = json_object_get(json, "authSalt");

    if(jUseAuth == NULL || !json_is_boolean(jUseAuth))
    {
        return;
    }

    if(json_typeof(jUseAuth) == JSON_FALSE)
    {
        return;
    }

    if(jAuthHash == NULL || !json_is_string(jAuthHash) ||
       jAuthSalt == NULL || !json_is_string(jAuthSalt))
    {
        /* couldn't get auth parameters */
        return;
    }

    this->useAuth = true;
    this->authHash = json_string_value(jAuthHash);
    this->authSalt = json_string_value(jAuthSalt);
}
/**
 * Parse a boolean
 *
 * If the key does not exist in the src object, returns success but does fill in *dest.
 *
 * @param src JSON object to pull a value from
 * @param key key to pull
 * @param dest (output) pointer to an allocated integer
 * @return TR_CFG_SUCCESS or an error code
 */
static TR_CFG_RC tr_cfg_parse_boolean(json_t *src, const char *key, int *dest)
{
  json_t *jtmp;

  /* Validate parameters */
  if ((src == NULL) || (key == NULL) || (dest == NULL))
    return TR_CFG_BAD_PARAMS;

  /* See if we have a value for this key; do nothing if not */
  jtmp = json_object_get(src, key);
  if (jtmp) {
    if (json_is_boolean(jtmp)) {
      *dest = json_is_true(jtmp);
    } else {
      tr_debug("tr_cfg_parse_unsigned: Parsing error, %s is not a boolean.", key);
      return TR_CFG_NOPARSE;
    }
  }

  return TR_CFG_SUCCESS;
}
Beispiel #26
0
json_t* OBSAPIMessageHandler::HandleSetSourceRender(OBSAPIMessageHandler *handler, json_t* message)
{
    StringList sceneNames;
    json_t* source = json_object_get(message, "source");
    if(source == NULL || json_typeof(source) != JSON_STRING)
    {
        return GetErrorResponse("No source specified");
    }

    json_t* sourceRender = json_object_get(message, "render");
    if(source == NULL || !json_is_boolean(sourceRender))
    {
        return GetErrorResponse("No render specified");
    }

    json_t* ret = GetOkResponse();

    String sourceName = json_string_value(source);
    OBSSetSourceRender(sourceName.Array(), json_typeof(sourceRender) == JSON_TRUE);

    return ret;
}
Beispiel #27
0
/**
 * Convert JSON object to PyObject
 * @param JSONObject
 * @return PyObject
 */
static PyObject* convert(json_t *json) {
    PyObject* object = NULL;
    json_t* val;
    int result = 0;

    if (json_is_object(json)) {
        object = PyDict_New();

        char* key;
        json_object_foreach(json, key, val) {
            if (json_is_object(val) || json_is_array(val)) {
                result = PyDict_SetItemString(object, key, convert(val));
            }
            else if(json_is_string(val)) {
                const char *str = json_string_value(val);
                result = PyDict_SetItemString(object, key,
                    PyString_DecodeEscape(str, str ? strlen(str) : 0, NULL, 0, NULL));
            }
            else if(json_is_boolean(val)) {
                result = PyDict_SetItemString(object, key,
                    json_is_true(val) ? Py_True : Py_False);
            }
            else if(json_is_integer(val)) {
                result = PyDict_SetItemString(object, key,
                    PyInt_FromLong(json_integer_value(val)));
            }
            else if(json_is_real(val)) {
                result = PyDict_SetItemString(object, key,
                    PyFloat_FromDouble(json_real_value(val)));
            }
            else {
                result = PyDict_SetItemString(object, key, Py_None);
            }
            if (result == -1)
                goto failure;
        }
    }
    else if (json_is_array(json)) {
GratingParams parse_grating_info(const json_t * const root) {
    GratingParams result;

    json_t *data_json;

    data_json = json_object_get(root, "reset_phase_position");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_boolean(data_json));
    result.reset_phase_position = json_is_true( data_json );

    data_json = json_object_get(root, "phase_position");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.phase_position = json_real_value( data_json );

    data_json = json_object_get(root, "phase_velocity");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.phase_velocity = json_real_value( data_json );

    data_json = json_object_get(root, "wavelength");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.wavelength = json_real_value( data_json );

    data_json = json_object_get(root, "contrast");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.contrast = json_real_value( data_json );

    data_json = json_object_get(root, "orientation");
    flyvr_assert(data_json != NULL);
    flyvr_assert(json_is_real(data_json));
    result.orientation = json_real_value( data_json );

    return result;
}
static bool load_content_from_config_file(json_t *config_json, const char *config_file) {
  bool return_value = false;

  jid = get_str_value(config_json, "jid");
  werr2(jid == NULL, goto _finish, "There is no jid entry in %s", config_file);

  password = get_str_value(config_json, "password");
  werr2(password == NULL, goto _finish, "There is no password entry in %s", config_file);

  owner = get_str_value(config_json, "owner");
  werr2(owner == NULL, goto _finish, "There is no owner entry in %s", config_file);

  ssid = get_str_value(config_json, "ssid");
  psk  = get_str_value(config_json, "psk");

  /* Set privacy based on privacy value from wyliodrin.json (if exists) */
  json_t *privacy_json = json_object_get(config_json, "privacy");
  if (privacy_json != NULL && json_is_boolean(privacy_json) && json_is_true(privacy_json)) {
    privacy = true;
  }

  const char *pong_timeout_str = get_str_value(config_json, "pong_timeout");
  if (pong_timeout_str != NULL) {
    char *pEnd;
    pong_timeout = strtol(pong_timeout_str, &pEnd, 10);
    if (pong_timeout == 0) {
      pong_timeout = DEFAULT_PONG_TIMEOUT;
    }
  }

  /* Success */
  return_value = true;

  _finish: ;
    return return_value;
}
Beispiel #30
0
/* Call the simple functions not covered by other tests of the public API */
int main()
{
    json_t *value;

    value = json_integer(1);
    if(json_typeof(value) != JSON_INTEGER)
        fail("json_typeof failed");

    if(json_is_object(value))
        fail("json_is_object failed");

    if(json_is_array(value))
        fail("json_is_array failed");

    if(json_is_string(value))
        fail("json_is_string failed");

    if(!json_is_integer(value))
        fail("json_is_integer failed");

    if(json_is_real(value))
        fail("json_is_real failed");

    if(!json_is_number(value))
        fail("json_is_number failed");

    if(json_is_true(value))
        fail("json_is_true failed");

    if(json_is_false(value))
        fail("json_is_false failed");

    if(json_is_boolean(value))
        fail("json_is_boolean failed");

    if(json_is_null(value))
        fail("json_is_null failed");

    json_decref(value);


    value = json_string("foo");
    if(!value)
        fail("json_string failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set(value, "bar"))
        fail("json_string_set failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    value = json_string(NULL);
    if(value)
        fail("json_string(NULL) failed");

    /* invalid UTF-8  */
    value = json_string("a\xefz");
    if(value)
        fail("json_string(<invalid utf-8>) failed");

    value = json_string_nocheck("foo");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "bar"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    /* invalid UTF-8 */
    value = json_string_nocheck("qu\xff");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "qu\xff"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
        fail("invalid string value");

    json_decref(value);


    value = json_integer(123);
    if(!value)
        fail("json_integer failed");
    if(json_integer_value(value) != 123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.0)
        fail("invalid number value");

    if(json_integer_set(value, 321))
        fail("json_integer_set failed");
    if(json_integer_value(value) != 321)
        fail("invalid integer value");
    if(json_number_value(value) != 321.0)
        fail("invalid number value");

    json_decref(value);

    value = json_real(123.123);
    if(!value)
        fail("json_real failed");
    if(json_real_value(value) != 123.123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.123)
        fail("invalid number value");

    if(json_real_set(value, 321.321))
        fail("json_real_set failed");
    if(json_real_value(value) != 321.321)
        fail("invalid real value");
    if(json_number_value(value) != 321.321)
        fail("invalid number value");

    json_decref(value);

    value = json_true();
    if(!value)
        fail("json_true failed");
    json_decref(value);

    value = json_false();
    if(!value)
        fail("json_false failed");
    json_decref(value);

    value = json_null();
    if(!value)
        fail("json_null failed");
    json_decref(value);

    /* Test reference counting on singletons (true, false, null) */
    value = json_true();
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");

    value = json_false();
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");

    value = json_null();
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");

    return 0;
}