Expand the pack/unpack API, implement unpack flags
Expand the pack/unpack API: json_(un)pack is the simple version with no error output, json_(un)pack_ex has error output and flags, json_v(un)pack_ex is a va_list version of json_(un)pack_ex. Implement unpacking flags: - JSON_UNPACK_ONLY turns off extra validation, i.e. array and object unpacking doesn't check that all items are unpacked. This is really just convenience for not adding '*' after each object and array. - JSON_VALIDATE_ONLY turns off unpacking, i.e. no varargs are expected and nothing is unpacked.
This commit is contained in:
parent
2770dca2c0
commit
579c291882
@ -10,6 +10,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> /* for size_t */
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <jansson_config.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -185,8 +187,20 @@ int json_string_set_nocheck(json_t *string, const char *value);
|
||||
int json_integer_set(json_t *integer, json_int_t value);
|
||||
int json_real_set(json_t *real, double value);
|
||||
|
||||
json_t *json_pack(json_error_t *error, const char *fmt, ...);
|
||||
int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...);
|
||||
|
||||
/* pack, unpack */
|
||||
|
||||
json_t *json_pack(const char *fmt, ...);
|
||||
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
|
||||
json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
|
||||
|
||||
#define JSON_VALIDATE_ONLY 0x1
|
||||
#define JSON_UNPACK_ONLY 0x2
|
||||
|
||||
int json_unpack(json_t *root, const char *fmt, ...);
|
||||
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
|
||||
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
|
||||
|
||||
|
||||
/* equality */
|
||||
|
||||
|
@ -6,10 +6,6 @@
|
||||
* it under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <jansson.h>
|
||||
#include "jansson_private.h"
|
||||
|
||||
@ -17,6 +13,7 @@ typedef struct {
|
||||
const char *fmt;
|
||||
char token;
|
||||
json_error_t *error;
|
||||
size_t flags;
|
||||
int line;
|
||||
int column;
|
||||
} scanner_t;
|
||||
@ -181,11 +178,20 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
|
||||
{
|
||||
int ret = -1;
|
||||
int wildcard = 0;
|
||||
hashtable_t key_set;
|
||||
|
||||
if(hashtable_init(&key_set, jsonp_hash_key, jsonp_key_equal, NULL, NULL)) {
|
||||
set_error(s, "Out of memory");
|
||||
return -1;
|
||||
/* Use a set (emulated by a hashtable) to check that all object
|
||||
keys are accessed. Checking that the correct number of keys
|
||||
were accessed is not enough, as the same key can be unpacked
|
||||
multiple times.
|
||||
*/
|
||||
hashtable_t *key_set;
|
||||
|
||||
if(!(s->flags & JSON_UNPACK_ONLY)) {
|
||||
key_set = hashtable_create(jsonp_hash_key, jsonp_key_equal, NULL, NULL);
|
||||
if(!key_set) {
|
||||
set_error(s, "Out of memory");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!json_is_object(root)) {
|
||||
@ -199,7 +205,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
|
||||
json_t *value;
|
||||
|
||||
if(wildcard) {
|
||||
set_error(s, "Expected '}', got '%c'", s->token);
|
||||
set_error(s, "Expected '}' after '*', got '%c'", s->token);
|
||||
goto error;
|
||||
}
|
||||
|
||||
@ -231,19 +237,27 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap)
|
||||
if(unpack(s, value, ap))
|
||||
goto error;
|
||||
|
||||
hashtable_set(&key_set, (void *)key, NULL);
|
||||
if(!(s->flags & JSON_UNPACK_ONLY))
|
||||
hashtable_set(key_set, (void *)key, NULL);
|
||||
|
||||
next_token(s);
|
||||
}
|
||||
|
||||
if(!wildcard && key_set.size != json_object_size(root)) {
|
||||
long diff = (long)json_object_size(root) - (long)key_set.size;
|
||||
if(s->flags & JSON_UNPACK_ONLY)
|
||||
wildcard = 1;
|
||||
|
||||
if(!wildcard && key_set->size != json_object_size(root)) {
|
||||
long diff = (long)json_object_size(root) - (long)key_set->size;
|
||||
set_error(s, "%li object items left unpacked", diff);
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
error:
|
||||
hashtable_close(&key_set);
|
||||
if(!(s->flags & JSON_UNPACK_ONLY))
|
||||
hashtable_destroy(key_set);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -262,7 +276,7 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
|
||||
json_t *value;
|
||||
|
||||
if(wildcard) {
|
||||
set_error(s, "Expected ']', got '%c'", s->token);
|
||||
set_error(s, "Expected ']' after '*', got '%c'", s->token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -290,6 +304,9 @@ static int unpack_array(scanner_t *s, json_t *root, va_list *ap)
|
||||
i++;
|
||||
}
|
||||
|
||||
if(s->flags & JSON_UNPACK_ONLY)
|
||||
wildcard = 1;
|
||||
|
||||
if(!wildcard && i != json_array_size(root)) {
|
||||
long diff = (long)json_array_size(root) - (long)i;
|
||||
set_error(s, "%li array items left upacked", diff);
|
||||
@ -310,9 +327,6 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
return unpack_array(s, root, ap);
|
||||
|
||||
case 's':
|
||||
{
|
||||
const char **str;
|
||||
|
||||
if(!json_is_string(root))
|
||||
{
|
||||
set_error(s, "Type mismatch! Object (%i) wasn't a string.",
|
||||
@ -320,15 +334,18 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
return -1;
|
||||
}
|
||||
|
||||
str = va_arg(*ap, const char **);
|
||||
if(!str) {
|
||||
set_error(s, "Passed a NULL string pointer!");
|
||||
return -1;
|
||||
}
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY)) {
|
||||
const char **str;
|
||||
|
||||
*str = json_string_value(root);
|
||||
str = va_arg(*ap, const char **);
|
||||
if(!str) {
|
||||
set_error(s, "Passed a NULL string pointer!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*str = json_string_value(root);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case 'i':
|
||||
if(!json_is_integer(root))
|
||||
@ -337,7 +354,10 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
json_typeof(root));
|
||||
return -1;
|
||||
}
|
||||
*va_arg(*ap, int*) = json_integer_value(root);
|
||||
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY))
|
||||
*va_arg(*ap, int*) = json_integer_value(root);
|
||||
|
||||
return 0;
|
||||
|
||||
case 'b':
|
||||
@ -347,7 +367,10 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
json_typeof(root));
|
||||
return -1;
|
||||
}
|
||||
*va_arg(*ap, int*) = json_is_true(root);
|
||||
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY))
|
||||
*va_arg(*ap, int*) = json_is_true(root);
|
||||
|
||||
return 0;
|
||||
|
||||
case 'f':
|
||||
@ -357,19 +380,25 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
json_typeof(root));
|
||||
return -1;
|
||||
}
|
||||
*va_arg(*ap, double*) = json_number_value(root);
|
||||
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY))
|
||||
*va_arg(*ap, double*) = json_number_value(root);
|
||||
|
||||
return 0;
|
||||
|
||||
case 'O':
|
||||
json_incref(root);
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY))
|
||||
json_incref(root);
|
||||
/* Fall through */
|
||||
|
||||
case 'o':
|
||||
*va_arg(*ap, json_t**) = root;
|
||||
if(!(s->flags & JSON_VALIDATE_ONLY))
|
||||
*va_arg(*ap, json_t**) = root;
|
||||
|
||||
return 0;
|
||||
|
||||
case 'n':
|
||||
/* Don't assign, just validate */
|
||||
/* Never assign, just validate */
|
||||
if(!json_is_null(root))
|
||||
{
|
||||
set_error(s, "Type mismatch! Object (%i) wasn't null.",
|
||||
@ -384,11 +413,11 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
|
||||
}
|
||||
}
|
||||
|
||||
json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
||||
json_t *json_vpack_ex(json_error_t *error, size_t flags,
|
||||
const char *fmt, va_list ap)
|
||||
{
|
||||
scanner_t s;
|
||||
json_t *value;
|
||||
va_list ap;
|
||||
|
||||
jsonp_error_init(error, "");
|
||||
|
||||
@ -398,18 +427,17 @@ json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
||||
}
|
||||
|
||||
s.error = error;
|
||||
s.flags = flags;
|
||||
s.fmt = fmt;
|
||||
s.line = 1;
|
||||
s.column = 0;
|
||||
|
||||
next_token(&s);
|
||||
|
||||
va_start(ap, fmt);
|
||||
value = pack(&s, &ap);
|
||||
va_end(ap);
|
||||
|
||||
next_token(&s);
|
||||
if(s.token) {
|
||||
json_decref(value);
|
||||
set_error(&s, "Garbage after format string");
|
||||
return NULL;
|
||||
}
|
||||
@ -417,11 +445,34 @@ json_t *json_pack(json_error_t *error, const char *fmt, ...)
|
||||
return value;
|
||||
}
|
||||
|
||||
int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
||||
json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)
|
||||
{
|
||||
json_t *value;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
value = json_vpack_ex(error, flags, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
json_t *json_pack(const char *fmt, ...)
|
||||
{
|
||||
json_t *value;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
value = json_vpack_ex(NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags,
|
||||
const char *fmt, va_list ap)
|
||||
{
|
||||
scanner_t s;
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
jsonp_error_init(error, "");
|
||||
|
||||
@ -431,17 +482,14 @@ int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
||||
}
|
||||
|
||||
s.error = error;
|
||||
s.flags = flags;
|
||||
s.fmt = fmt;
|
||||
s.line = 1;
|
||||
s.column = 0;
|
||||
|
||||
next_token(&s);
|
||||
|
||||
va_start(ap, fmt);
|
||||
result = unpack(&s, root, &ap);
|
||||
va_end(ap);
|
||||
|
||||
if(result)
|
||||
if(unpack(&s, root, &ap))
|
||||
return -1;
|
||||
|
||||
next_token(&s);
|
||||
@ -452,3 +500,27 @@ int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = json_vunpack_ex(root, error, flags, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int json_unpack(json_t *root, const char *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = json_vunpack_ex(root, NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -54,7 +54,11 @@ json_equal
|
||||
json_copy
|
||||
json_deep_copy
|
||||
json_pack
|
||||
json_pack_ex
|
||||
json_vpack_ex
|
||||
json_unpack
|
||||
json_unpack_ex
|
||||
json_vunpack_ex
|
||||
EOF
|
||||
|
||||
# The list of functions are not exported in the library because they
|
||||
|
@ -22,7 +22,7 @@ int main()
|
||||
*/
|
||||
|
||||
/* true */
|
||||
value = json_pack(&error, "b", 1);
|
||||
value = json_pack_ex(&error, 0, "b", 1);
|
||||
if(!json_is_true(value))
|
||||
fail("json_pack boolean failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
@ -30,7 +30,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* false */
|
||||
value = json_pack(&error, "b", 0);
|
||||
value = json_pack_ex(&error, 0, "b", 0);
|
||||
if(!json_is_false(value))
|
||||
fail("json_pack boolean failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
@ -38,7 +38,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* null */
|
||||
value = json_pack(&error, "n");
|
||||
value = json_pack_ex(&error, 0, "n");
|
||||
if(!json_is_null(value))
|
||||
fail("json_pack null failed");
|
||||
if(value->refcount != (ssize_t)-1)
|
||||
@ -46,7 +46,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* integer */
|
||||
value = json_pack(&error, "i", 1);
|
||||
value = json_pack_ex(&error, 0, "i", 1);
|
||||
if(!json_is_integer(value) || json_integer_value(value) != 1)
|
||||
fail("json_pack integer failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
@ -55,7 +55,7 @@ int main()
|
||||
|
||||
|
||||
/* real */
|
||||
value = json_pack(&error, "f", 1.0);
|
||||
value = json_pack_ex(&error, 0, "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)
|
||||
@ -63,7 +63,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* string */
|
||||
value = json_pack(&error, "s", "test");
|
||||
value = json_pack_ex(&error, 0, "s", "test");
|
||||
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
|
||||
fail("json_pack string failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
@ -71,7 +71,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* empty object */
|
||||
value = json_pack(&error, "{}", 1.0);
|
||||
value = json_pack_ex(&error, 0, "{}", 1.0);
|
||||
if(!json_is_object(value) || json_object_size(value) != 0)
|
||||
fail("json_pack empty object failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
@ -79,7 +79,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* empty list */
|
||||
value = json_pack(&error, "[]", 1.0);
|
||||
value = json_pack_ex(&error, 0, "[]", 1.0);
|
||||
if(!json_is_array(value) || json_array_size(value) != 0)
|
||||
fail("json_pack empty list failed");
|
||||
if(value->refcount != (ssize_t)1)
|
||||
@ -87,7 +87,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* non-incref'd object */
|
||||
value = json_pack(&error, "o", json_integer(1));
|
||||
value = json_pack_ex(&error, 0, "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)
|
||||
@ -95,7 +95,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* incref'd object */
|
||||
value = json_pack(&error, "O", json_integer(1));
|
||||
value = json_pack_ex(&error, 0, "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)
|
||||
@ -104,7 +104,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* simple object */
|
||||
value = json_pack(&error, "{s:[]}", "foo");
|
||||
value = json_pack_ex(&error, 0, "{s:[]}", "foo");
|
||||
if(!json_is_object(value) || json_object_size(value) != 1)
|
||||
fail("json_pack array failed");
|
||||
if(!json_is_array(json_object_get(value, "foo")))
|
||||
@ -114,7 +114,7 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* simple array */
|
||||
value = json_pack(&error, "[i,i,i]", 0, 1, 2);
|
||||
value = json_pack_ex(&error, 0, "[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++)
|
||||
@ -127,19 +127,19 @@ int main()
|
||||
json_decref(value);
|
||||
|
||||
/* Whitespace; regular string */
|
||||
value = json_pack(&error, " s ", "test");
|
||||
value = json_pack_ex(&error, 0, " 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, "[ ]");
|
||||
value = json_pack_ex(&error, 0, "[ ]");
|
||||
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);
|
||||
value = json_pack_ex(&error, 0, "[ 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);
|
||||
@ -149,52 +149,52 @@ int main()
|
||||
*/
|
||||
|
||||
/* mismatched open/close array/object */
|
||||
if(json_pack(&error, "[}"))
|
||||
if(json_pack_ex(&error, 0, "[}"))
|
||||
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, "{]"))
|
||||
if(json_pack_ex(&error, 0, "{]"))
|
||||
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(&error, "["))
|
||||
if(json_pack_ex(&error, 0, "["))
|
||||
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(&error, "{"))
|
||||
if(json_pack_ex(&error, 0, "{"))
|
||||
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(&error, "s", NULL))
|
||||
if(json_pack_ex(&error, 0, "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))
|
||||
if(json_pack_ex(&error, 0, 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"))
|
||||
if(json_pack_ex(&error, 0, "{ {}: 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))
|
||||
if(json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
|
||||
fail("json_pack failed to catch missing ]");
|
||||
if(error.line != 1 || error.column != 19)
|
||||
fail("json_pack didn't get the error coordinates right!");
|
||||
|
||||
if(json_pack(&error, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
|
||||
if(json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
|
||||
fail("json_pack failed to catch extra }");
|
||||
if(error.line != 1 || error.column != 21)
|
||||
fail("json_pack didn't get the error coordinates right!");
|
||||
|
@ -26,77 +26,77 @@ int main()
|
||||
*/
|
||||
|
||||
/* true */
|
||||
rv = json_unpack(json_true(), &error, "b", &i1);
|
||||
rv = json_unpack_ex(json_true(), &error, 0, "b", &i1);
|
||||
if(rv || !i1)
|
||||
fail("json_unpack boolean failed");
|
||||
|
||||
/* false */
|
||||
rv = json_unpack(json_false(), &error, "b", &i1);
|
||||
rv = json_unpack_ex(json_false(), &error, 0, "b", &i1);
|
||||
if(rv || i1)
|
||||
fail("json_unpack boolean failed");
|
||||
|
||||
/* null */
|
||||
if(json_unpack(json_null(), &error, "n"))
|
||||
if(json_unpack_ex(json_null(), &error, 0, "n"))
|
||||
fail("json_unpack null failed");
|
||||
|
||||
/* integer */
|
||||
j = json_integer(42);
|
||||
rv = json_unpack(j, &error, "i", &i1);
|
||||
rv = json_unpack_ex(j, &error, 0, "i", &i1);
|
||||
if(rv || i1 != 42)
|
||||
fail("json_unpack integer failed");
|
||||
json_decref(j);
|
||||
|
||||
/* real */
|
||||
j = json_real(1.7);
|
||||
rv = json_unpack(j, &error, "f", &f);
|
||||
rv = json_unpack_ex(j, &error, 0, "f", &f);
|
||||
if(rv || f != 1.7)
|
||||
fail("json_unpack real failed");
|
||||
json_decref(j);
|
||||
|
||||
/* string */
|
||||
j = json_string("foo");
|
||||
rv = json_unpack(j, &error, "s", &s);
|
||||
rv = json_unpack_ex(j, &error, 0, "s", &s);
|
||||
if(rv || strcmp(s, "foo"))
|
||||
fail("json_unpack string failed");
|
||||
json_decref(j);
|
||||
|
||||
/* empty object */
|
||||
j = json_object();
|
||||
if(json_unpack(j, &error, "{}"))
|
||||
if(json_unpack_ex(j, &error, 0, "{}"))
|
||||
fail("json_unpack empty object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* empty list */
|
||||
j = json_array();
|
||||
if(json_unpack(j, &error, "[]"))
|
||||
if(json_unpack_ex(j, &error, 0, "[]"))
|
||||
fail("json_unpack empty list failed");
|
||||
json_decref(j);
|
||||
|
||||
/* non-incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, &error, "o", &j2);
|
||||
rv = json_unpack_ex(j, &error, 0, "o", &j2);
|
||||
if(j2 != j || j->refcount != 1)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* incref'd object */
|
||||
j = json_object();
|
||||
rv = json_unpack(j, &error, "O", &j2);
|
||||
rv = json_unpack_ex(j, &error, 0, "O", &j2);
|
||||
if(j2 != j || j->refcount != 2)
|
||||
fail("json_unpack object failed");
|
||||
json_decref(j);
|
||||
json_decref(j);
|
||||
|
||||
/* simple object */
|
||||
j = json_pack(&error, "{s:i}", "foo", 42);
|
||||
rv = json_unpack(j, &error, "{s:i}", "foo", &i1);
|
||||
j = json_pack("{s:i}", "foo", 42);
|
||||
rv = json_unpack_ex(j, &error, 0, "{s:i}", "foo", &i1);
|
||||
if(rv || i1 != 42)
|
||||
fail("json_unpack simple object failed");
|
||||
json_decref(j);
|
||||
|
||||
/* simple array */
|
||||
j = json_pack(&error, "[iii]", 1, 2, 3);
|
||||
rv = json_unpack(j, &error, "[i,i,i]", &i1, &i2, &i3);
|
||||
j = json_pack("[iii]", 1, 2, 3);
|
||||
rv = json_unpack_ex(j, &error, 0, "[i,i,i]", &i1, &i2, &i3);
|
||||
if(rv || i1 != 1 || i2 != 2 || i3 != 3)
|
||||
fail("json_unpack simple array failed");
|
||||
json_decref(j);
|
||||
@ -106,37 +106,37 @@ int main()
|
||||
*/
|
||||
|
||||
/* mismatched open/close array/object */
|
||||
j = json_pack(&error, "[]");
|
||||
if(!json_unpack(j, &error, "[}"))
|
||||
j = json_pack("[]");
|
||||
if(!json_unpack_ex(j, &error, 0, "[}"))
|
||||
fail("json_unpack failed to catch mismatched ']'");
|
||||
json_decref(j);
|
||||
|
||||
j = json_pack(&error, "{}");
|
||||
if(!json_unpack(j, &error, "{]"))
|
||||
j = json_pack("{}");
|
||||
if(!json_unpack_ex(j, &error, 0, "{]"))
|
||||
fail("json_unpack failed to catch mismatched '}'");
|
||||
json_decref(j);
|
||||
|
||||
/* missing close array */
|
||||
j = json_pack(&error, "[]");
|
||||
if(!json_unpack(j, &error, "["))
|
||||
j = json_pack("[]");
|
||||
if(!json_unpack_ex(j, &error, 0, "["))
|
||||
fail("json_unpack failed to catch missing ']'");
|
||||
json_decref(j);
|
||||
|
||||
/* missing close object */
|
||||
j = json_pack(&error, "{}");
|
||||
if(!json_unpack(j, &error, "{"))
|
||||
j = json_pack("{}");
|
||||
if(!json_unpack_ex(j, &error, 0, "{"))
|
||||
fail("json_unpack failed to catch missing '}'");
|
||||
json_decref(j);
|
||||
|
||||
/* NULL format string */
|
||||
j = json_pack(&error, "[]");
|
||||
if(!json_unpack(j, &error, NULL))
|
||||
j = json_pack("[]");
|
||||
if(!json_unpack_ex(j, &error, 0, NULL))
|
||||
fail("json_unpack failed to catch null format string");
|
||||
json_decref(j);
|
||||
|
||||
/* NULL string pointer */
|
||||
j = json_string("foobie");
|
||||
if(!json_unpack(j, &error, "s", NULL))
|
||||
if(!json_unpack_ex(j, &error, 0, "s", NULL))
|
||||
fail("json_unpack failed to catch null string pointer");
|
||||
json_decref(j);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user