Files
cworker/jparser_test.c
2023-08-13 19:39:49 +02:00

62 lines
1.1 KiB
C

/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <massert.h>
#include <jlexer.h>
#include <jparser.h>
int main(void) {
int fd = open("test.json", O_RDONLY);
MASSERT(fd > 0);
rcache_t cache;
rcache_init(&cache, fd);
jlexer_t lexer;
jlexer_init(&lexer, &cache);
jparser_t parser;
jparser_init(&parser, &lexer);
if (jparser_parse(&parser) < 0) {
dprintf(STDERR_FILENO, "cannot parse json\n");
return 1;
}
int64_t id = 0;
if (jparser_bind(&parser, JVALTYPE_NUM, "id", (void *)&id) < 0) {
dprintf(STDERR_FILENO, "cannot bind variable\n");
return 1;
}
char* name = "";
if (jparser_bind(&parser, JVALTYPE_STR, "name", (void *)&name) < 0) {
dprintf(STDERR_FILENO, "cannot bind variable\n");
}
printf("id = %d\n", id);
printf("name = %s\n", name);
jparser_destroy(&parser);
jlexer_destroy(&lexer);
rcache_destroy(&cache);
return 0;
}