-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
183 lines (142 loc) · 5.06 KB
/
example.c
File metadata and controls
183 lines (142 loc) · 5.06 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "stdio.h"
#include "time.h"
// Uncomment for call stack trace
// #define JSON_PARSER_DEBUG 1
// Uncomment to supress error messages
// #define JSON_PARSER_SILENT 1
#include "JSONitator.h"
char *file_read(const char *file_path)
{
FILE *f = fopen(file_path, "rb");
if (f == NULL)
return NULL;
fseek(f, 0, SEEK_END);
uint64_t fsize = ftell(f);
rewind(f);
char *file_buff = malloc(sizeof(char) * fsize);
uint64_t read_size = fread(file_buff, sizeof(char), fsize, f);
if (fsize != read_size)
{
printf("Failed to read entirety of file.\n");
return NULL;
}
fclose(f);
return file_buff;
}
int main(void)
{
const char *FILE_PATH = "in.json";
char *file_buff = file_read(FILE_PATH);
if (file_buff == NULL)
{
printf("Failed to read file.\n");
return 1;
}
//==========================================================================
// Parse JSON file
//==========================================================================
JSON *json = json_parse(file_buff);
// json_parse can fail
if (json == NULL)
{
printf("json is null.\n");
return 1;
}
free(file_buff);
//==========================================================================
// Stringify JSON struct
//==========================================================================
char *stringified = json_stringify(json);
// json_stringified can fail
if (stringified == NULL)
{
printf("Failed to stringify json struct.\n");
return 1;
}
FILE *fstream = fopen("out.json", "w");
if (fstream == NULL)
{
printf("Failed to open out.json file.\n");
return 1;
}
size_t len = strlen(stringified);
fwrite(stringified, sizeof(char), len, fstream);
fclose(fstream);
free(stringified);
//==========================================================================
// Access into JSON objects
//==========================================================================
// Equivalent to JS: json["behaviour"]
JSON *shallow_val = json_object_get(json, "behaviour");
// Equivalent to JS: json["_"]["user_name"]["default"]
// Works for both object and array access (index must be stringified)
JSON *nested_val = json_get_deep(json, 3, (const char *[3]){"_", "user_name", "default"});
// access functions might fail, handle the error case
if (nested_val == NULL)
{
printf("Failed to access: json[\"_\"][\"user_name\"][\"default\"]\n");
return 1;
}
// Example with array access:
JSON *nested_val2 = json_get_deep(json, 4, (const char *[4]){"moderation", "blacklisted_chatters", "value", "0"});
if (nested_val2 == NULL)
{
printf("Failed to access: json[\"moderation\"][\"blacklisted_chatters\"][\"value\"][0]\n");
return 1;
}
printf("Deep value type: \"%s\"\n", json_type_to_str(nested_val->type));
// Here deep_val->type == VAL_STRING
// to extract the data, use typed json_value functions
char *default_username = json_value_string(nested_val);
char *blacklisted = json_value_string(nested_val2);
printf("json[\"_\"][\"user_name\"][\"default\"] == \"%s\"\n", default_username);
printf("json[\"moderation\"][\"blacklisted_chatters\"][\"value\"][0] == \"%s\"\n", blacklisted);
JSON *nested_obj = json_get_deep(json, 2, (const char *[2]){"llm", "temperature"});
if (nested_obj == NULL)
{
printf("Failed to access: json[\"llm\"][\"temperature\"]\n");
return 1;
}
// Print a JSON object
printf("json[\"llm\"][\"temperature\"] == ");
json_print(nested_obj);
//==========================================================================
// Create JSON objects
//==========================================================================
// Creates an object like:
/*
{
"field1": "This is a JSON string.",
"field2": 53
}
*/
char *fields[2] = {"field1", "field2"};
JSON *values[2] = {
json_make_string("This is a JSON string."),
json_make_number(53),
};
JSON *json_object = json_make_object(2, fields, values);
// Add an entry "field3": null
err_t err = json_object_append(json_object, "field3", json_make_null());
if (err != 0)
{
printf("Failed to append entry to json_object\n");
return 1;
}
// Delete this new entry
err = json_object_delete(json_object, "field3");
if (err != 0)
{
printf("Failed to delete entry in json_object\n");
return 1;
}
//==========================================================================
// Free JSON objects
//==========================================================================
// Clear the memory allocated during parsing
// Only call on the direct output of json_parse or standalone JSON structs
json_free(json);
// In this case json_object is standalone, all child values will be freed by json_free
json_free(json_object);
return 0;
}