예제 #1
0
//native bool:json_object_iter_set(Handle:hObj, Handle:hIter, Handle:hValue);
static cell_t Native_json_object_iter_set(IPluginContext *pContext, const cell_t *params) {
	HandleError err;
	HandleSecurity sec;
	sec.pOwner = NULL;
	sec.pIdentity = myself->GetIdentity();

	// Param 1
	json_t *object;
	Handle_t hndlObject = static_cast<Handle_t>(params[1]);
	if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None)
    {
        pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlObject, err);
        return BAD_HANDLE;
    }

    // Param 2
    void *iter;
	Handle_t hndlIterator = static_cast<Handle_t>(params[2]);
	if ((err=g_pHandleSys->ReadHandle(hndlIterator, htJanssonIterator, &sec, (void **)&iter)) != HandleError_None)
    {
        pContext->ThrowNativeError("Invalid <JSON Iterator> handle %x (error %d)", hndlIterator, err);
        return BAD_HANDLE;
    }

 	// Param 3
	json_t *value;
	Handle_t hndlValue = static_cast<Handle_t>(params[3]);
	if ((err=g_pHandleSys->ReadHandle(hndlValue, htJanssonObject, &sec, (void **)&value)) != HandleError_None)
    {
        pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlValue, err);
        return BAD_HANDLE;
    }

    return (json_object_iter_set(object, iter, value) == 0);
}
예제 #2
0
static void test_iterators()
{
    int i;
    json_t *object, *foo, *bar, *baz;
    const char *iter_keys[3];
    int have_key[3] = { 0, 0, 0 };
    json_t *iter_values[3];
    void *iter;

    if(json_object_iter(NULL))
        fail("able to iterate over NULL");

    if(json_object_iter_next(NULL, NULL))
        fail("able to increment an iterator on a NULL object");

    object = json_object();
    foo = json_string("foo");
    bar = json_string("bar");
    baz = json_string("baz");
    if(!object || !foo || !bar || !bar)
        fail("unable to create values");

    if(json_object_iter_next(object, NULL))
        fail("able to increment a NULL iterator");

    if(json_object_set(object, "a", foo) ||
       json_object_set(object, "b", bar) ||
       json_object_set(object, "c", baz))
        fail("unable to populate object");

    iter = json_object_iter(object);
    if(!iter)
        fail("unable to get iterator");
    iter_keys[0] = json_object_iter_key(iter);
    iter_values[0] = json_object_iter_value(iter);

    iter = json_object_iter_next(object, iter);
    if(!iter)
        fail("unable to increment iterator");
    iter_keys[1] = json_object_iter_key(iter);
    iter_values[1] = json_object_iter_value(iter);
	printf("values=%s\n", json_string_value(iter_values[1]));

    iter = json_object_iter_next(object, iter);
    if(!iter)
        fail("unable to increment iterator");
    iter_keys[2] = json_object_iter_key(iter);
    iter_values[2] = json_object_iter_value(iter);

    if(json_object_iter_next(object, iter) != NULL)
        fail("able to iterate over the end");

    /* Check that keys have correct values */
    for (i = 0; i < 3; i++) {
        if (strcmp(iter_keys[i], "a") == 0) {
            if (iter_values[i] != foo)
                fail("wrong value for iter key a");
            else
                have_key[0] = 1;
        } else if (strcmp(iter_keys[i], "b") == 0) {
            if (iter_values[i] != bar)
                fail("wrong value for iter key b");
            else
                have_key[1] = 1;
        } else if (strcmp(iter_keys[i], "c") == 0) {
            if (iter_values[i] != baz)
                fail("wrong value for iter key c");
            else
                have_key[2] = 1;
        }
    }

    /* Check that we got all keys */
    for(i = 0; i < 3; i++) {
        if(!have_key[i])
            fail("a key wasn't iterated over");
    }

    if(json_object_iter_at(object, "foo"))
        fail("json_object_iter_at() succeeds for non-existent key");

    iter = json_object_iter_at(object, "b");
    if(!iter)
        fail("json_object_iter_at() fails for an existing key");

    if(strcmp(json_object_iter_key(iter), "b"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != bar)
        fail("iterating failed: wrong value");

    if(json_object_iter_set(object, iter, baz))
        fail("unable to set value at iterator");

    if(strcmp(json_object_iter_key(iter), "b"))
        fail("json_object_iter_key() fails after json_object_iter_set()");
    if(json_object_iter_value(iter) != baz)
        fail("json_object_iter_value() fails after json_object_iter_set()");
    if(json_object_get(object, "b") != baz)
        fail("json_object_get() fails after json_object_iter_set()");

    json_decref(object);
    json_decref(foo);
    json_decref(bar);
    json_decref(baz);
}
예제 #3
0
/* 获取每个元素的值, 返回元素的结构体 */
struct policymsg iter_get_value(json_t *object)
{
	struct policymsg p;
    int i;
    json_t *iter_values[3];
    const char *iter_keys[3];
    void *iter;

	printf("判断是不是object类型\n");
	my_json_type(object);
	//printf("........\n");
#if 0
    if(json_object_iter(NULL))
	{
        printf("iterate NULL\n");
	}

    if(json_object_iter_next(NULL, NULL))
	{
        printf("iterator a NULL object\n");
	}
#endif 

    iter = json_object_iter(object);
	i = 0;
	while(1)
	{
		iter_keys[i] = json_object_iter_key(iter);
		iter_values[i] = json_object_iter_value(iter);
		//printf("values[%d]=%s\n", i,json_string_value(iter_values[i]));

		if((iter = json_object_iter_next(object, iter)) == NULL)
		{
			 //printf("iterate end\n");
			 break;
		}
		i++;
	}

	i=4;
	iter = json_object_iter_at(object, "b");
    if(iter)
	{
		iter_keys[i] = json_object_iter_key(iter);
		iter_values[i] = json_object_iter_value(iter);
		printf("values[%d]=%s\n", i,json_string_value(iter_values[i]));
        //printf("json_object_iter_at() printfs for an existing key\n");
	}

#if 0
    if(json_object_iter_set(object, iter, baz))
        printf("unable to set value at iterator");

    if(strcmp(json_object_iter_key(iter), "b"))
        printf("json_object_iter_key() printfs after json_object_iter_set()");
    if(json_object_iter_value(iter) != baz)
        printf("json_object_iter_value() printfs after json_object_iter_set()");
    if(json_object_get(object, "b") != baz)
        printf("json_object_get() printfs after json_object_iter_set()");
#endif

    json_decref(object);
	return p;
}
예제 #4
0
static void test_iterators()
{
    json_t *object, *foo, *bar, *baz;
    void *iter;

    if(json_object_iter(NULL))
        fail("able to iterate over NULL");

    if(json_object_iter_next(NULL, NULL))
        fail("able to increment an iterator on a NULL object");

    object = json_object();
    foo = json_string("foo");
    bar = json_string("bar");
    baz = json_string("baz");
    if(!object || !foo || !bar || !bar)
        fail("unable to create values");

    if(json_object_iter_next(object, NULL))
        fail("able to increment a NULL iterator");

    if(json_object_set(object, "a", foo) ||
            json_object_set(object, "b", bar) ||
            json_object_set(object, "c", baz))
        fail("unable to populate object");

    iter = json_object_iter(object);
    if(!iter)
        fail("unable to get iterator");
    if(strcmp(json_object_iter_key(iter), "a"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != foo)
        fail("iterating failed: wrong value");

    iter = json_object_iter_next(object, iter);
    if(!iter)
        fail("unable to increment iterator");
    if(strcmp(json_object_iter_key(iter), "b"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != bar)
        fail("iterating failed: wrong value");

    iter = json_object_iter_next(object, iter);
    if(!iter)
        fail("unable to increment iterator");
    if(strcmp(json_object_iter_key(iter), "c"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != baz)
        fail("iterating failed: wrong value");

    if(json_object_iter_next(object, iter) != NULL)
        fail("able to iterate over the end");

    if(json_object_iter_at(object, "foo"))
        fail("json_object_iter_at() succeeds for non-existent key");

    iter = json_object_iter_at(object, "b");
    if(!iter)
        fail("json_object_iter_at() fails for an existing key");

    if(strcmp(json_object_iter_key(iter), "b"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != bar)
        fail("iterating failed: wrong value");

    iter = json_object_iter_next(object, iter);
    if(!iter)
        fail("unable to increment iterator");
    if(strcmp(json_object_iter_key(iter), "c"))
        fail("iterating failed: wrong key");
    if(json_object_iter_value(iter) != baz)
        fail("iterating failed: wrong value");

    if(json_object_iter_set(object, iter, bar))
        fail("unable to set value at iterator");

    if(strcmp(json_object_iter_key(iter), "c"))
        fail("json_object_iter_key() fails after json_object_iter_set()");
    if(json_object_iter_value(iter) != bar)
        fail("json_object_iter_value() fails after json_object_iter_set()");
    if(json_object_get(object, "c") != bar)
        fail("json_object_get() fails after json_object_iter_set()");

    json_decref(object);
    json_decref(foo);
    json_decref(bar);
    json_decref(baz);
}
예제 #5
0
파일: codec-json.c 프로젝트: Memeo/lounge
int la_codec_object_iter_set(la_codec_value_t *object, void *iter, la_codec_value_t *value)
{
    return json_object_iter_set((json_t *) object, iter, (json_t *) value);
}