signalwire/libks

remove assert on ks_json_get_object_ functions

Closed this issue · 0 comments

functions like ks_json_get_object_cstr assert internally that makes it useless since you cannot make sure the item you access is valid before you access it.

The only valid way to use this function is to always check the key exists and has the right type ahead. but if you check ahead there's no reason to assert later.

The full implementation below:

KS_DECLARE(const char * const) ks_json_get_object_cstr(const ks_json_t * const object, const char * const key)
{
	const ks_json_t *item = ks_json_get_object_item(object, key);
	if (item) {
		ks_assert(ks_json_type_is_string(item));
		return item->valuestring;
	}
	return NULL;
}

We created ks_json_get_object_cstr_def as a safer way of the above function. I would like to keep the function name ks_json_get_object_cstr as a THIN wrapper to cJSON and remove the asserts internally, and the ks_json_get_object_cstr_def version could be removed or build as a macro on top of ks_json_get_object_cstr if it still useful.

There are other functions like this e.g.

  • ks_json_get_object_item
  • ks_json_get_object_bool
  • ks_json_get_object_number_int
  • ks_json_get_object_number_double
  • ks_json_get_array_number_double
  • ks_json_get_array_uuid
  • ks_json_get_array_number_int
  • ks_json_get_array_cstr
  • ks_json_get_array_bool
  • ks_json_get_array_item