diff options
author | Vincent Bernat <bernat@luffy.cx> | 2014-02-15 17:23:53 +0100 |
---|---|---|
committer | Vincent Bernat <bernat@luffy.cx> | 2014-02-15 17:40:22 +0100 |
commit | 7a0b9af6626e0b11231b8fb068a49dab2d5d2bd7 (patch) | |
tree | 35da49273c669e368e3c8097862dab43cce7a9a3 /test | |
parent | ea7a77236c54e3275a2c275aa058f310b4c6cc48 (diff) | |
download | jansson-7a0b9af6626e0b11231b8fb068a49dab2d5d2bd7.zip jansson-7a0b9af6626e0b11231b8fb068a49dab2d5d2bd7.tar.gz jansson-7a0b9af6626e0b11231b8fb068a49dab2d5d2bd7.tar.bz2 |
Allow to mix JSON_STRICT with optional keys
On unpack, one may want to mix `JSON_STRICT` and optional keys by using
a format like `{s:i,s?o!}`. Unfortunately, this fails the stric test
with `-1 object item(s) left unpacked` error when the second key is not
specified.
To fix that, we iter on each key and we check if we have successfully
unpacked them. This is less efficient than the previous method but it
brings correctness.
Diffstat (limited to 'test')
-rw-r--r-- | test/suites/api/test_unpack.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/test/suites/api/test_unpack.c b/test/suites/api/test_unpack.c index fac1be3..6b76106 100644 --- a/test/suites/api/test_unpack.c +++ b/test/suites/api/test_unpack.c @@ -378,4 +378,23 @@ static void run_tests() if(i1 != 42) fail("json_unpack failed to unpack"); json_decref(j); + + /* Combine ? and ! */ + j = json_pack("{si}", "foo", 42); + i1 = i2 = 0; + if(json_unpack(j, "{sis?i!}", "foo", &i1, "bar", &i2)) + fail("json_unpack failed for optional values with strict mode"); + if(i1 != 42) + fail("json_unpack failed to unpack"); + if(i2 != 0) + fail("json_unpack failed to unpack"); + json_decref(j); + + /* But don't compensate a missing key with an optional one. */ + j = json_pack("{sisi}", "foo", 42, "baz", 43); + i1 = i2 = i3 = 0; + if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2)) + fail("json_unpack failed for optional values with strict mode and compensation"); + check_error("1 object item(s) left unpacked", "<validation>", 1, 8, 8); + json_decref(j); } |