|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdbool.h> |
| 3 | +#define NOB_IMPLEMENTATION |
| 4 | +#define NOB_STRIP_PREFIX |
| 5 | +#include "../thirdparty/nob.h" |
| 6 | +#define JIMP_IMPLEMENTATION |
| 7 | +#include "../jimp.h" |
| 8 | + |
| 9 | +typedef struct { |
| 10 | + const char *name; |
| 11 | + double age; |
| 12 | + const char *location; |
| 13 | + double body_count; |
| 14 | +} Person; |
| 15 | + |
| 16 | +typedef struct { |
| 17 | + Person *items; |
| 18 | + size_t count; |
| 19 | + size_t capacity; |
| 20 | +} People; |
| 21 | + |
| 22 | +bool parse_person(Jimp *jimp, Person *p) |
| 23 | +{ |
| 24 | + if (!jimp_object_begin(jimp)) return false; |
| 25 | + while (jimp_object_member(jimp)) { |
| 26 | + if (strcmp(jimp->member, "name") == 0) { |
| 27 | + if (!jimp_string(jimp, &p->name)) return false; |
| 28 | + } else if (strcmp(jimp->member, "age") == 0) { |
| 29 | + if (!jimp_number(jimp, &p->age)) return false; |
| 30 | + } else if (strcmp(jimp->member, "location") == 0) { |
| 31 | + if (!jimp_string(jimp, &p->location)) return false; |
| 32 | + } else if (strcmp(jimp->member, "body_count") == 0) { |
| 33 | + if (!jimp_number(jimp, &p->body_count)) return false; |
| 34 | + } else { |
| 35 | + jimp_unknown_member(jimp); |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + return jimp_object_end(jimp); |
| 40 | +} |
| 41 | + |
| 42 | +bool parse_people(Jimp *jimp, People *ps) |
| 43 | +{ |
| 44 | + if (!jimp_array_begin(jimp)) return false; |
| 45 | + while (jimp_array_item(jimp)) { |
| 46 | + Person p = {0}; |
| 47 | + if (!parse_person(jimp, &p)) return false; |
| 48 | + da_append(ps, p); |
| 49 | + } |
| 50 | + if (!jimp_array_end(jimp)) return false; |
| 51 | + |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +void print_person(const Person *p) |
| 56 | +{ |
| 57 | + printf("name = %s\n", p->name); |
| 58 | + printf("age = %lf\n", p->age); |
| 59 | + printf("location = %s\n", p->location); |
| 60 | + printf("body_count = %lf\n", p->body_count); |
| 61 | +} |
| 62 | + |
| 63 | +typedef struct { |
| 64 | + long *items; |
| 65 | + size_t count; |
| 66 | + size_t capacity; |
| 67 | +} Numbers; |
| 68 | + |
| 69 | +int main() |
| 70 | +{ |
| 71 | + // const char *file_path = "profile.json"; |
| 72 | + // const char *file_path = "numbers.json"; |
| 73 | + // const char *file_path = "profiles.json"; |
| 74 | + // const char *file_path = "empty.json"; |
| 75 | + // const char *file_path = "one.json"; |
| 76 | + const char *file_path = "database.json"; |
| 77 | + String_Builder sb = {0}; |
| 78 | + if (!read_entire_file(file_path, &sb)) return 1; |
| 79 | + Jimp jimp = { |
| 80 | + .file_path = file_path, |
| 81 | + .start = sb.items, |
| 82 | + .end = sb.items + sb.count, |
| 83 | + .point = sb.items, |
| 84 | + }; |
| 85 | + |
| 86 | + People ps = {0}; |
| 87 | + Numbers xs = {0}; |
| 88 | + if (!jimp_object_begin(&jimp)) return 1; |
| 89 | + while (jimp_object_member(&jimp)) { |
| 90 | + if (strcmp(jimp.member, "profile") == 0) { |
| 91 | + if (!parse_people(&jimp, &ps)) return 1; |
| 92 | + } else if (strcmp(jimp.member, "number") == 0) { |
| 93 | + if (!jimp_array_begin(&jimp)) return 1; |
| 94 | + while (jimp_array_item(&jimp)) { |
| 95 | + double x = 0; |
| 96 | + if (!jimp_number(&jimp, &x)) return 1; |
| 97 | + da_append(&xs, x); |
| 98 | + } |
| 99 | + if (!jimp_array_end(&jimp)) return 1; |
| 100 | + } else { |
| 101 | + jimp_unknown_member(&jimp); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + } |
| 105 | + if (!jimp_object_end(&jimp)) return 1; |
| 106 | + |
| 107 | + da_foreach(Person, p, &ps) { |
| 108 | + print_person(p); |
| 109 | + printf("\n"); |
| 110 | + } |
| 111 | + printf("------------------------------\n"); |
| 112 | + da_foreach(long, x, &xs) { |
| 113 | + printf("%ld ", *x); |
| 114 | + } |
| 115 | + printf("\n"); |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
0 commit comments