test-json.c revision dde8bb32b12c855509777ce52ff59a835155ac78
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <math.h>
#include "util.h"
#include "json.h"
for (;;) {
union json_value v = {};
int t, tt;
if (t == JSON_END || t < 0)
break;
else if (t == JSON_STRING) {
const char *nn;
} else if (t == JSON_REAL) {
double d;
} else if (t == JSON_INTEGER) {
intmax_t i;
} else if (t == JSON_BOOLEAN) {
bool b;
}
}
}
typedef void (*Test)(JsonVariant *);
int r;
r = json_parse(data, &v);
assert_se(r == 0);
if (test)
test(v);
}
static void test_1(JsonVariant *v) {
JsonVariant *p, *q;
unsigned i;
/* 3 keys + 3 values */
/* has k */
p = json_variant_value(v, "k");
/* k equals v */
/* has foo */
p = json_variant_value(v, "foo");
/* check foo[0] = 1, foo[1] = 2, foo[2] = 3 */
for (i = 0; i < 3; ++i) {
q = json_variant_element(p, i);
}
/* has bar */
p = json_variant_value(v, "bar");
/* zap is null */
q = json_variant_value(p, "zap");
}
static void test_2(JsonVariant *v) {
JsonVariant *p, *q;
/* 2 keys + 2 values */
/* has mutant */
p = json_variant_value(v, "mutant");
/* mutant[0] == 1 */
q = json_variant_element(p, 0);
/* mutant[1] == null */
q = json_variant_element(p, 1);
/* mutant[2] == "1" */
q = json_variant_element(p, 2);
/* mutant[3] == JSON_VARIANT_OBJECT */
q = json_variant_element(p, 3);
/* has 1 */
p = json_variant_value(q, "1");
/* "1"[0] == 1 */
q = json_variant_element(p, 0);
/* "1"[1] == "1" */
q = json_variant_element(p, 1);
/* has blah */
p = json_variant_value(v, "blah");
}
test_one("{\"foo\" : \"bar\"}", JSON_OBJECT_OPEN, JSON_STRING, "foo", JSON_COLON, JSON_STRING, "bar", JSON_OBJECT_CLOSE, JSON_END);
test_one("{\"foo\" : [true, false]}", JSON_OBJECT_OPEN, JSON_STRING, "foo", JSON_COLON, JSON_ARRAY_OPEN, JSON_BOOLEAN, true, JSON_COMMA, JSON_BOOLEAN, false, JSON_ARRAY_CLOSE, JSON_OBJECT_CLOSE, JSON_END);
test_one("[1, 2]", JSON_ARRAY_OPEN, JSON_INTEGER, (intmax_t) 1, JSON_COMMA, JSON_INTEGER, (intmax_t) 2, JSON_ARRAY_CLOSE, JSON_END);
return 0;
}