03_parsing_database.c 3.1 KB

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