Make json_pack/json_unpack() recursive

Note that we pass va_list pointers around instead of just va_lists, which
would seem more intuitive. This is necessary since the behaviour of va_lists
passed as function parameters is finicky. Quoth stdarg(3):

	If ap is passed to a function that uses va_arg(ap,type) then the value
	of ap is undefined after the return of that function.

The pointer-passing strategy is used by Python's Py_BuildValue() for the same
purpose.
This commit is contained in:
Graeme Smecher
2011-01-14 09:18:42 -08:00
committed by Petri Lehtinen
parent 269e86b725
commit 3a7512d2b0
3 changed files with 578 additions and 448 deletions

View File

@@ -15,13 +15,14 @@ int main()
{
json_t *value;
int i;
json_error_t error;
/*
* Simple, valid json_pack cases
*/
/* true */
value = json_pack(NULL, "b", 1);
value = json_pack(&error, "b", 1);
if(!json_is_true(value))
fail("json_pack boolean failed");
if(value->refcount != (ssize_t)-1)
@@ -29,7 +30,7 @@ int main()
json_decref(value);
/* false */
value = json_pack(NULL, "b", 0);
value = json_pack(&error, "b", 0);
if(!json_is_false(value))
fail("json_pack boolean failed");
if(value->refcount != (ssize_t)-1)
@@ -37,7 +38,7 @@ int main()
json_decref(value);
/* null */
value = json_pack(NULL, "n");
value = json_pack(&error, "n");
if(!json_is_null(value))
fail("json_pack null failed");
if(value->refcount != (ssize_t)-1)
@@ -45,7 +46,7 @@ int main()
json_decref(value);
/* integer */
value = json_pack(NULL, "i", 1);
value = json_pack(&error, "i", 1);
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack integer failed");
if(value->refcount != (ssize_t)1)
@@ -54,7 +55,7 @@ int main()
/* real */
value = json_pack(NULL, "f", 1.0);
value = json_pack(&error, "f", 1.0);
if(!json_is_real(value) || json_real_value(value) != 1.0)
fail("json_pack real failed");
if(value->refcount != (ssize_t)1)
@@ -62,7 +63,7 @@ int main()
json_decref(value);
/* string */
value = json_pack(NULL, "s", "test");
value = json_pack(&error, "s", "test");
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string failed");
if(value->refcount != (ssize_t)1)
@@ -70,7 +71,7 @@ int main()
json_decref(value);
/* empty object */
value = json_pack(NULL, "{}", 1.0);
value = json_pack(&error, "{}", 1.0);
if(!json_is_object(value) || json_object_size(value) != 0)
fail("json_pack empty object failed");
if(value->refcount != (ssize_t)1)
@@ -78,7 +79,7 @@ int main()
json_decref(value);
/* empty list */
value = json_pack(NULL, "[]", 1.0);
value = json_pack(&error, "[]", 1.0);
if(!json_is_array(value) || json_array_size(value) != 0)
fail("json_pack empty list failed");
if(value->refcount != (ssize_t)1)
@@ -86,7 +87,7 @@ int main()
json_decref(value);
/* non-incref'd object */
value = json_pack(NULL, "o", json_integer(1));
value = json_pack(&error, "o", json_integer(1));
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack object failed");
if(value->refcount != (ssize_t)1)
@@ -94,7 +95,7 @@ int main()
json_decref(value);
/* incref'd object */
value = json_pack(NULL, "O", json_integer(1));
value = json_pack(&error, "O", json_integer(1));
if(!json_is_integer(value) || json_integer_value(value) != 1)
fail("json_pack object failed");
if(value->refcount != (ssize_t)2)
@@ -103,17 +104,17 @@ int main()
json_decref(value);
/* simple object */
value = json_pack(NULL, "{s:[]}", "foo");
value = json_pack(&error, "{s:[]}", "foo");
if(!json_is_object(value) || json_object_size(value) != 1)
fail("json_pack object failed");
fail("json_pack array failed");
if(!json_is_array(json_object_get(value, "foo")))
fail("json_pack object failed");
fail("json_pack array failed");
if(json_object_get(value, "foo")->refcount != (ssize_t)1)
fail("json_pack object refcount failed");
json_decref(value);
/* simple array */
value = json_pack(NULL, "[i,i,i]", 0, 1, 2);
value = json_pack(&error, "[i,i,i]", 0, 1, 2);
if(!json_is_array(value) || json_array_size(value) != 3)
fail("json_pack object failed");
for(i=0; i<3; i++)
@@ -125,30 +126,82 @@ int main()
}
json_decref(value);
/* Whitespace; regular string */
value = json_pack(&error, " s ", "test");
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string (with whitespace) failed");
json_decref(value);
/* Whitespace; empty array */
value = json_pack(&error, "[ ]");
if(!json_is_array(value) || json_array_size(value) != 0)
fail("json_pack empty array (with whitespace) failed");
json_decref(value);
/* Whitespace; array */
value = json_pack(&error, "[ i , i, i ] ", 1, 2, 3);
if(!json_is_array(value) || json_array_size(value) != 3)
fail("json_pack array (with whitespace) failed");
json_decref(value);
/*
* Invalid cases
*/
/* mismatched open/close array/object */
if(json_pack(NULL, "[}"))
fail("json_pack failed to catch mismatched '}'");
if(json_pack(NULL, "{]"))
/* mismatched open/close array/object */
if(json_pack(&error, "[}"))
fail("json_pack failed to catch mismatched '}'");
if(error.line != 1 || error.column != 2)
fail("json_pack didn't get the error coordinates right!");
if(json_pack(&error, "{]"))
fail("json_pack failed to catch mismatched ']'");
if(error.line != 1 || error.column != 2)
fail("json_pack didn't get the error coordinates right!");
/* missing close array */
if(json_pack(NULL, "["))
if(json_pack(&error, "["))
fail("json_pack failed to catch missing ']'");
if(error.line != 1 || error.column != 2)
fail("json_pack didn't get the error coordinates right!");
/* missing close object */
if(json_pack(NULL, "{"))
if(json_pack(&error, "{"))
fail("json_pack failed to catch missing '}'");
if(error.line != 1 || error.column != 2)
fail("json_pack didn't get the error coordinates right!");
/* NULL string */
if(json_pack(NULL, "s", NULL))
fail("json_pack failed to catch null string");
if(json_pack(&error, "s", NULL))
fail("json_pack failed to catch null argument string");
if(error.line != 1 || error.column != 1)
fail("json_pack didn't get the error coordinates right!");
/* NULL format */
if(json_pack(&error, NULL))
fail("json_pack failed to catch NULL format string");
if(error.line != 1 || error.column != 1)
fail("json_pack didn't get the error coordinates right!");
/* More complicated checks for row/columns */
if(json_pack(&error, "{ {}: s }", "foo"))
fail("json_pack failed to catch object as key");
if(error.line != 1 || error.column != 3)
fail("json_pack didn't get the error coordinates right!");
if(json_pack(&error, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
fail("json_pack failed to catch missing ]");
if(error.line != 1 || error.column != 13)
fail("json_pack didn't get the error coordinates right!");
if(json_pack(&error, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
fail("json_pack failed to catch missing ]");
if(error.line != 1 || error.column != 21)
fail("json_pack didn't get the error coordinates right!");
return(0);
//fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
}
/* vim: ts=4:expandtab:sw=4

View File

@@ -20,90 +20,139 @@ int main()
double f;
char *s;
json_error_t error;
/*
* Simple, valid json_pack cases
*/
/* true */
rv = json_unpack(json_true(), NULL, "b", &i1);
rv = json_unpack(json_true(), &error, "b", &i1);
if(rv || !i1)
fail("json_unpack boolean failed");
/* false */
rv = json_unpack(json_false(), NULL, "b", &i1);
rv = json_unpack(json_false(), &error, "b", &i1);
if(rv || i1)
fail("json_unpack boolean failed");
/* null */
rv = json_unpack(json_null(), NULL, "n");
rv = json_unpack(json_null(), &error, "n");
if(rv)
fail("json_unpack null failed");
/* integer */
j = json_integer(1);
rv = json_unpack(j, NULL, "i", &i1);
rv = json_unpack(j, &error, "i", &i1);
if(rv || i1 != 1)
fail("json_unpack integer failed");
json_decref(j);
/* real */
j = json_real(1.0);
rv = json_unpack(j, NULL, "f", &f);
rv = json_unpack(j, &error, "f", &f);
if(rv || f != 1.0)
fail("json_unpack real failed");
json_decref(j);
/* string */
j = json_string("foo");
rv = json_unpack(j, NULL, "s", &s);
rv = json_unpack(j, &error, "s", &s);
if(rv || strcmp(s, "foo"))
fail("json_unpack string failed");
json_decref(j);
/* empty object */
j = json_object();
rv = json_unpack(j, NULL, "{}");
rv = json_unpack(j, &error, "{}");
if(rv)
fail("json_unpack empty object failed");
json_decref(j);
/* empty list */
j = json_array();
rv = json_unpack(j, NULL, "[]");
rv = json_unpack(j, &error, "[]");
if(rv)
fail("json_unpack empty list failed");
json_decref(j);
/* non-incref'd object */
j = json_object();
rv = json_unpack(j, NULL, "o", &j2);
rv = json_unpack(j, &error, "o", &j2);
if(j2 != j || j->refcount != (ssize_t)1)
fail("json_unpack object failed");
json_decref(j);
/* incref'd object */
j = json_object();
rv = json_unpack(j, NULL, "O", &j2);
rv = json_unpack(j, &error, "O", &j2);
if(j2 != j || j->refcount != (ssize_t)2)
fail("json_unpack object failed");
json_decref(j);
json_decref(j);
/* simple object */
j = json_pack(NULL, "{s:i}", "foo", 1);
rv = json_unpack(j, NULL, "{s:i}", "foo", &i1);
j = json_pack(&error, "{s:i}", "foo", 1);
rv = json_unpack(j, &error, "{s:i}", "foo", &i1);
if(rv || i1!=1)
fail("json_unpack simple object failed");
json_decref(j);
/* simple array */
j = json_pack(NULL, "[iii]", 1, 2, 3);
rv = json_unpack(j, NULL, "[i,i,i]", &i1, &i2, &i3);
j = json_pack(&error, "[iii]", 1, 2, 3);
rv = json_unpack(j, &error, "[i,i,i]", &i1, &i2, &i3);
if(rv || i1 != 1 || i2 != 2 || i3 != 3)
fail("json_unpack simple array failed");
json_decref(j);
/*
* Invalid cases
*/
/* mismatched open/close array/object */
j = json_pack(&error, "[]");
rv = json_unpack(j, &error, "[}");
if(!rv)
fail("json_unpack failed to catch mismatched ']'");
json_decref(j);
j = json_pack(&error, "{}");
rv = json_unpack(j, &error, "{]");
if(!rv)
fail("json_unpack failed to catch mismatched '}'");
json_decref(j);
/* missing close array */
j = json_pack(&error, "[]");
rv = json_unpack(j, &error, "[");
if(rv >= 0)
fail("json_unpack failed to catch missing ']'");
json_decref(j);
/* missing close object */
j = json_pack(&error, "{}");
rv = json_unpack(j, &error, "{");
if(rv >= 0)
fail("json_unpack failed to catch missing '}'");
json_decref(j);
/* NULL format string */
j = json_pack(&error, "[]");
rv =json_unpack(j, &error, NULL);
if(rv >= 0)
fail("json_unpack failed to catch null format string");
json_decref(j);
/* NULL string pointer */
j = json_string("foobie");
rv =json_unpack(j, &error, "s", NULL);
if(rv >= 0)
fail("json_unpack failed to catch null string pointer");
json_decref(j);
return 0;
//fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
}
/* vim: ts=4:expandtab:sw=4