Пример #1
0
// Given an array of string values, if that array does not contain
// a ".watchmanconfig" entry, prepend it
static void prepend_watchmanconfig_to_array(json_t *ref) {
  const char *val;

  if (json_array_size(ref) == 0) {
    // json_array_insert_new at index can fail when the array is empty,
    // so just append in this case.
    json_array_append_new(ref, json_string_nocheck(".watchmanconfig"));
    return;
  }

  val = json_string_value(json_array_get(ref, 0));
  if (!strcmp(val, ".watchmanconfig")) {
    return;
  }
  json_array_insert_new(ref, 0, json_string_nocheck(".watchmanconfig"));
}
Пример #2
0
static void test_insert(void)
{
    json_t *array, *five, *seven, *eleven, *value;
    int i;

    array = json_array();
    five = json_integer(5);
    seven = json_integer(7);
    eleven = json_integer(11);

    if(!array)
        fail("unable to create array");
    if(!five || !seven || !eleven)
        fail("unable to create integer");


    if(!json_array_insert(array, 1, five))
        fail("able to insert value out of bounds");


    if(json_array_insert(array, 0, five))
        fail("unable to insert value in an empty array");

    if(json_array_get(array, 0) != five)
        fail("json_array_insert works incorrectly");

    if(json_array_size(array) != 1)
        fail("array size is invalid after insertion");


    if(json_array_insert(array, 1, seven))
        fail("unable to insert value at the end of an array");

    if(json_array_get(array, 0) != five)
        fail("json_array_insert works incorrectly");

    if(json_array_get(array, 1) != seven)
        fail("json_array_insert works incorrectly");

    if(json_array_size(array) != 2)
        fail("array size is invalid after insertion");


    if(json_array_insert(array, 1, eleven))
        fail("unable to insert value in the middle of an array");

    if(json_array_get(array, 0) != five)
        fail("json_array_insert works incorrectly");

    if(json_array_get(array, 1) != eleven)
        fail("json_array_insert works incorrectly");

    if(json_array_get(array, 2) != seven)
        fail("json_array_insert works incorrectly");

    if(json_array_size(array) != 3)
        fail("array size is invalid after insertion");


    if(json_array_insert_new(array, 2, json_integer(123)))
        fail("unable to insert value in the middle of an array");

    value = json_array_get(array, 2);
    if(!json_is_integer(value) || json_integer_value(value) != 123)
        fail("json_array_insert_new works incorrectly");

    if(json_array_size(array) != 4)
        fail("array size is invalid after insertion");


    for(i = 0; i < 20; i++) {
        if(json_array_insert(array, 0, seven))
            fail("unable to insert value at the begining of an array");
    }

    for(i = 0; i < 20; i++) {
        if(json_array_get(array, i) != seven)
            fail("json_aray_insert works incorrectly");
    }

    if(json_array_size(array) != 24)
        fail("array size is invalid after loop insertion");

    json_decref(five);
    json_decref(seven);
    json_decref(eleven);
    json_decref(array);
}
Пример #3
0
int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
{
	return json_array_insert_new((json_t *)array, index, (json_t *)value);
}
Пример #4
0
void MltRuntime::add_runtime_entry(const JsonPath& path,
		json_t* script_serialed, int give) throw(Exception)
{
	if ( path.path.size() == 0) {
		throw_error_v(ErrorRuntimeUuidPathInvalid, "root path not allowed for add entry");
	}

	JsonPath parentPath(path);
	parentPath.path.pop_back();

	json_t* parent_je = json_serialize;
	JsonPath::PathCompIter it = parentPath.path.begin();

	for (; it != parentPath.path.end(); it++)
	{
		json_t* je = json_object_get(parent_je, it->name.c_str());
		if (!je) {
			throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
		}
		if ( it->type == JsonPathComponent::PathObject ) {
			if ( !json_is_object(je) ) {
				throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
			}
			parent_je = je;
		}
		else if (it->type == JsonPathComponent::PathArray ) {
			if (!json_is_array(je)) {
				throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
			}
			int sz = json_array_size(je);
			if (sz == 0) {
				throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
			}

			int idx = it->arr_idx;
			if (idx < 0) idx = sz + idx;
			if (idx < 0 || idx >= sz ) {
				throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
			}

			parent_je = json_array_get(je, idx);
		}
	}

	if (!json_is_object(parent_je)) {
		throw_error_v(ErrorRuntimeUuidPathInvalid, "parent path invalid for add entry");
	}

	const JsonPathComponent& lastPath = *(path.path.rbegin());
	if (lastPath.type == JsonPathComponent::PathArray) {
		json_t* cur = json_object_get(parent_je, lastPath.name.c_str());
		if ( !cur ) {
			cur = json_array();
			json_object_set_new(parent_je, lastPath.name.c_str(), cur);
		}

		if (!json_is_array(cur)) {
			throw_error_v(ErrorRuntimeUuidPathInvalid, " path invalid for add entry");
		}
		else {
			int idx = lastPath.arr_idx;
			int sz = json_array_size(cur);

			if (idx < 0) {
				idx = sz + idx + 1;
			}

			if (idx > sz) idx = sz;
			if (idx < 0) idx = 0;

			if ( idx == sz ) {
				JsonPath curPath(parentPath);
				curPath.push_back(lastPath.name.c_str(), idx);
				parse_struct(script_serialed, curPath, uuid_pathmap);
				if (give)
					json_array_append_new(cur, script_serialed);
				else
					json_array_append(cur, script_serialed);
			}
			else {
				JsonPath curPath(parentPath);
				curPath.push_back(lastPath.name.c_str(), idx);
				parse_struct(script_serialed, curPath, uuid_pathmap);
				if (give)
					json_array_insert_new(cur, idx, script_serialed);
				else
					json_array_insert(cur, idx, script_serialed);
			}
		}
	}
	else if (lastPath.type == JsonPathComponent::PathObject) {
		json_t* cur = json_object_get(parent_je, lastPath.name.c_str());
		if (cur) {
			throw_error_v(ErrorRuntimeUuidPathInvalid, " path invalid for add entry");
		}

		JsonPath curPath(parentPath);
		curPath.push_back(lastPath.name.c_str());
		parse_struct(script_serialed, curPath, uuid_pathmap);
		if (give) {
			json_object_set_new(parent_je, lastPath.name.c_str(), script_serialed);
		}
		else {
			json_object_set(parent_je, lastPath.name.c_str(), script_serialed);
		}
	}

	json_version++;
	return;
}
Пример #5
0
int la_codec_array_insert_new(la_codec_value_t *array, size_t index, la_codec_value_t *value)
{
    return json_array_insert_new((json_t*) array, index, (json_t*) value);
}