Adds json_pack / json_unpack variadic functions.
[jansson.git] / test / suites / api / test_unpack.c
1 /*
2  * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
3  * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
4  *
5  * Jansson is free software; you can redistribute it and/or modify
6  * it under the terms of the MIT license. See LICENSE for details.
7  */
8
9 #include <string.h>
10 #include <jansson.h>
11 #include <stdio.h>
12 #include "util.h"
13
14 int main()
15 {
16     json_t *j, *j2;
17     int i1, i2, i3;
18     int rv;
19     //void* v;
20     double f;
21     char *s;
22
23     /*
24      * Simple, valid json_pack cases
25      */
26
27     /* true */
28     rv = json_unpack(json_true(), NULL, "b", &i1);
29     if(rv || !i1)
30         fail("json_unpack boolean failed");
31
32     /* false */
33     rv = json_unpack(json_false(), NULL, "b", &i1);
34     if(rv || i1)
35         fail("json_unpack boolean failed");
36
37     /* null */
38     rv = json_unpack(json_null(), NULL, "n");
39     if(rv)
40         fail("json_unpack null failed");
41
42     /* integer */
43     j = json_integer(1);
44     rv = json_unpack(j, NULL, "i", &i1);
45     if(rv || i1 != 1)
46         fail("json_unpack integer failed");
47     json_decref(j);
48
49     /* real */
50     j = json_real(1.0);
51     rv = json_unpack(j, NULL, "f", &f);
52     if(rv || f != 1.0)
53         fail("json_unpack real failed");
54     json_decref(j);
55
56     /* string */
57     j = json_string("foo");
58     rv = json_unpack(j, NULL, "s", &s);
59     if(rv || strcmp(s, "foo"))
60         fail("json_unpack string failed");
61     json_decref(j);
62
63     /* empty object */
64     j = json_object();
65     rv = json_unpack(j, NULL, "{}");
66     if(rv)
67         fail("json_unpack empty object failed");
68     json_decref(j);
69
70     /* empty list */
71     j = json_array();
72     rv = json_unpack(j, NULL, "[]");
73     if(rv)
74         fail("json_unpack empty list failed");
75     json_decref(j);
76
77     /* non-incref'd object */
78     j = json_object();
79     rv = json_unpack(j, NULL, "o", &j2);
80     if(j2 != j || j->refcount != (ssize_t)1)
81         fail("json_unpack object failed");
82     json_decref(j);
83
84     /* incref'd object */
85     j = json_object();
86     rv = json_unpack(j, NULL, "O", &j2);
87     if(j2 != j || j->refcount != (ssize_t)2)
88         fail("json_unpack object failed");
89     json_decref(j);
90     json_decref(j);
91
92     /* simple object */
93     j = json_pack(NULL, "{s:i}", "foo", 1);
94     rv = json_unpack(j, NULL, "{s:i}", "foo", &i1);
95     if(rv || i1!=1)
96         fail("json_unpack simple object failed");
97     json_decref(j);
98
99     /* simple array */
100     j = json_pack(NULL, "[iii]", 1, 2, 3);
101     rv = json_unpack(j, NULL, "[i,i,i]", &i1, &i2, &i3);
102     if(rv || i1 != 1 || i2 != 2 || i3 != 3)
103         fail("json_unpack simple array failed");
104     json_decref(j);
105
106     return 0;
107 }
108
109 /* vim: ts=4:expandtab:sw=4
110  */