-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonapi.c
104 lines (78 loc) · 2.98 KB
/
jsonapi.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "jsonapi.h"
void show_json_error()
{
const char *json_error = cJSON_GetErrorPtr();
if (json_error != NULL)
{
fprintf(stderr, "JSON API error: %s\n", json_error);
}
}
void extract_json_value(cJSON *parent_object, const cJSON **target_object, char *key)
{
*target_object = cJSON_GetObjectItemCaseSensitive(parent_object, key);
}
char *extract_string_from_json(cJSON *parent_object, const cJSON **target_object, char *key)
{
extract_json_value(parent_object, target_object, key);
if (cJSON_IsString(*target_object) && (*target_object)->valuestring != NULL)
{
return (*target_object)->valuestring;
}
return NULL;
};
const cJSON *extract_object_from_json(cJSON *parent_object, const cJSON **target_object, char *key)
{
extract_json_value(parent_object, target_object, key);
if (cJSON_IsObject(*target_object) && (*target_object)->child != NULL)
{
return (*target_object)->child;
}
return NULL;
}
void extract_field_list_from_json(const cJSON *fields_object, database_table_t *table)
{
cJSON *current_field_object = NULL;
cJSON_ArrayForEach(current_field_object, fields_object)
{
const cJSON *field_name = NULL;
const cJSON *data_type = NULL;
database_field_t *current_field = add_table_field(table, extract_string_from_json(current_field_object, &field_name, "name"),
extract_string_from_json(current_field_object, &data_type, "data_type"));
}
}
void extract_table_list_from_json(const cJSON *tables_object, database_instance_t *instance)
{
cJSON *current_table_object = NULL;
cJSON_ArrayForEach(current_table_object, tables_object)
{
const cJSON *table_name = NULL;
const cJSON *fields = NULL;
database_table_t *current_table = add_table(instance, extract_string_from_json(current_table_object, &table_name, "name"));
extract_object_from_json(current_table_object, &fields, "fields");
extract_field_list_from_json(fields, current_table);
}
}
database_instance_t *parse_initialization_request(const char *json_string)
{
database_config_t config;
const cJSON *database_path = NULL;
const cJSON *database_driver = NULL;
const cJSON *tables = NULL;
cJSON *initialization_request = cJSON_Parse(json_string);
if (initialization_request == NULL)
{
const char *json_error = cJSON_GetErrorPtr();
if (json_error != NULL)
{
show_json_error();
return NULL;
}
}
config.path = extract_string_from_json(initialization_request, &database_path, "path");
config.driver = extract_string_from_json(initialization_request, &database_driver, "path");
database_instance_t *instance = create_sqlite_instance(config);
instance->initialize_database(instance);
extract_object_from_json(initialization_request, &tables, "tables");
extract_table_list_from_json(tables, instance);
return instance;
}