-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexample.c
More file actions
57 lines (45 loc) · 1.29 KB
/
example.c
File metadata and controls
57 lines (45 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "json.h"
const char *read_file(const char *path) {
FILE *file = fopen(path, "r");
if (file == NULL) {
fprintf(stderr, "Expected file \"%s\" not found", path);
return NULL;
}
fseek(file, 0, SEEK_END);
long len = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = malloc(len + 1);
if (buffer == NULL) {
fprintf(stderr, "Unable to allocate memory for file");
fclose(file);
return NULL;
}
fread(buffer, 1, len, file);
buffer[len] = '\0';
return (const char *)buffer;
}
int main(void) {
const char *json = read_file("../sample/reddit.json");
if (json == NULL) {
return -1;
}
clock_t start, end;
start = clock();
result(json_element) element_result = json_parse(json);
end = clock();
printf("Time taken %fs\n", (double)(end - start) / (double)CLOCKS_PER_SEC);
free((void *)json);
if (result_is_err(json_element)(&element_result)) {
typed(json_error) error = result_unwrap_err(json_element)(&element_result);
fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error));
return -1;
}
typed(json_element) element = result_unwrap(json_element)(&element_result);
// json_print(&element, 2);
json_free(&element);
return 0;
}