73 lines
1.5 KiB
C
73 lines
1.5 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>
|
|
#include <logger.h>
|
|
|
|
|
|
int main(void) {
|
|
|
|
int fd = open("test.json", O_RDONLY);
|
|
|
|
MASSERT(fd > 0);
|
|
|
|
rcache_t cache;
|
|
jlexer_t lexer;
|
|
jparser_t parser;
|
|
|
|
rcache_init(&cache, fd);
|
|
jlexer_init(&lexer, &cache);
|
|
jparser_init(&parser, &lexer);
|
|
|
|
if (jparser_parse(&parser) < 0) {
|
|
log_error("cannot parse json\n");
|
|
return 1;
|
|
}
|
|
|
|
int64_t id = 0;
|
|
if (jparser_bind(&parser, JVALTYPE_INTEG, "id", (void *)&id) < 0) {
|
|
log_error("cannot bind id variable\n");
|
|
return 1;
|
|
}
|
|
|
|
double size = 0;
|
|
if (jparser_bind(&parser, JVALTYPE_FLOAT, "size", (void *)&size) < 0) {
|
|
log_error("cannot bind size variable\n");
|
|
return 1;
|
|
}
|
|
|
|
char* name = "";
|
|
if (jparser_bind(&parser, JVALTYPE_STR, "name", (void *)&name) < 0) {
|
|
log_error("cannot bind name variable\n");
|
|
}
|
|
|
|
bool exists = false;
|
|
if (jparser_bind(&parser, JVALTYPE_BOOL, "exists", (void *)&exists) < 0) {
|
|
log_error("cannot bind exists variable\n");
|
|
}
|
|
|
|
|
|
printf("id = %ld\n", id);
|
|
printf("size = %f\n", size);
|
|
printf("name = %s\n", name);
|
|
printf("exists = %d\n", exists);
|
|
|
|
jparser_destroy(&parser);
|
|
jlexer_destroy(&lexer);
|
|
rcache_destroy(&cache);
|
|
free(name);
|
|
|
|
return 0;
|
|
}
|