-
Notifications
You must be signed in to change notification settings - Fork 17
/
module.jai
227 lines (204 loc) · 6.53 KB
/
module.jai
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// This file contains just the JSON serialization functions. See generic.jai and typed.jai for parse fuctions.
// Generates a JSON string from either a JSON_Value or any custom type.
// "indent_char" does what it says on the tin.
// "ignore" is only used for custom types to determine which properties of your custom type should be serialized.
// The default ignore function ignores all struct members that have the note @JsonIgnore.
// "rename" is used for renaming certain members.
// It gets called with the Type_Info_Struct_Member and must return the new name of the field.
// The default procedure rename members by their @JsonName note. Eg: @JsonName(renamed_member).
json_write_string :: (value: $T, indent_char := "\t", ignore := ignore_by_note, rename := rename_by_note) -> string {
builder: String_Builder;
defer free_buffers(*builder);
json_append_value(*builder, value, indent_char, ignore, rename);
return builder_to_string(*builder);
}
json_write_file :: (filename: string, value: $T, indent_char := "\t", ignore := ignore_by_note, rename := rename_by_note) -> bool {
builder: String_Builder;
defer free_buffers(*builder);
json_append_value(*builder, value, indent_char, ignore, rename);
return write_entire_file(filename, *builder);
}
json_append_value :: (builder: *String_Builder, val: $T, indent_char := "\t", ignore := ignore_by_note, rename := rename_by_note) {
#if T == JSON_Value {
json_write_json_value(builder, val, indent_char);
} else {
info := type_info(T);
json_write_native(builder, *val, info, indent_char, ignore, rename);
}
}
// This function is useful if you have a JSON template string and just want to
// safely insert a value without having to replicate the full json structure in Jai.
// The return value does NOT include quotes around the string.
//
// Example:
// JSON_TEMPLATE :: #string END
// {
// "complicated": {
// "json": {
// "structure": {
// "for_a_stupid_api": {
// "that_needs": [
// {"a_deeply_nested_value": "%1"}
// ]
// }
// }
// }
// }
// }
// END
// escaped_value := json_escape_string(my_unsafe_value);
// defer free(escaped_value);
// json_str := print(JSON_TEMPLATE, escaped_value);
json_escape_string :: (str: string) -> string {
builder: String_Builder;
defer free_buffers(*builder);
json_append_escaped(*builder, str);
escaped := builder_to_string(*builder);
return escaped;
}
Ignore_Proc :: #type (member: *Type_Info_Struct_Member) -> bool;
Rename_Proc :: #type (member: *Type_Info_Struct_Member) -> string;
ignore_by_note :: (member: *Type_Info_Struct_Member) -> bool {
for note: member.notes {
if note == "JsonIgnore" return true;
}
return false;
}
rename_by_note :: (member: *Type_Info_Struct_Member) -> string {
for note: member.notes {
if !begins_with(note, "JsonName(") continue;
if note.count <= 10 || note[note.count-1] != #char ")" {
log_error("Invalid JsonName note format. Expected a name in parenthesis, but the note was \"%\".", note);
continue;
}
return slice(note, 9, note.count-10);
}
return member.name;
}
#scope_module
WHITESPACE_CHARS :: " \t\n\r";
#load "generic.jai";
#load "typed.jai";
json_append_escaped :: (builder: *String_Builder, str: string) {
remaining := str;
next_pos := index_of_illegal_string_char(remaining);
append(builder, "\"");
while (next_pos >= 0) {
append(builder, slice(remaining, 0, next_pos));
if remaining[next_pos] == {
case #char "\\";
append(builder, "\\\\");
case #char "\"";
append(builder, "\\\"");
case #char "\n";
append(builder, "\\n");
case #char "\r";
append(builder, "\\r");
case #char "\t";
append(builder, "\\t");
case;
// ToDo: handle illegal multi-byte characters
// print("Escaping: %\n\n", slice(remaining, next_pos, remaining.count - next_pos));
print_to_builder(builder, "\\u%", formatInt(remaining[next_pos], base=16, minimum_digits=4));
}
remaining = advance(remaining, next_pos + 1);
next_pos = index_of_illegal_string_char(remaining);
}
append(builder, remaining);
append(builder, "\"");
}
index_of_illegal_string_char :: (str: string) -> s64 {
for 0..str.count - 1 {
if str[it] == #char "\\" || str[it] == #char "\"" || str[it] <= 0x1F {
return it;
}
}
return -1;
}
expect_and_slice :: (str: string, expected: string) -> remainder: string, success: bool {
if str.count < expected.count || !equal(slice(str, 0, expected.count), expected) {
log_error("Unexpected token. Expected \"%\" but got: %", expected, str);
return str, false;
}
remainder := advance(str, expected.count);
return remainder, true;
}
parse_string :: (str: string) -> result: string, remainder: string, success: bool {
assert(str[0] == #char "\"", "Invalid string start %", str);
inside := advance(str);
needsUnescape := false;
while inside[0] != #char "\"" {
if inside.count < 2 return "", str, false;
if inside[0] == #char "\\" {
needsUnescape = true;
if inside.count < 2 return "", str, false;
advance(*inside);
}
advance(*inside);
}
length := inside.data - str.data - 1;
result := slice(str, 1, length);
if needsUnescape {
success: bool;
result, success = unescape(result);
if !success return "", str, false;
} else {
result = copy_string(result);
}
remainder := slice(str, length + 2, str.count - length - 2);
return result, remainder, true;
}
unescape :: (str: string) -> result: string, success: bool {
result := alloc_string(str.count);
rc := 0;
for i: 0..str.count-1 {
if str[i] != #char "\\" {
// Check for invalid characters for JSON
if str[i] < 0x20 return "", false;
result[rc] = str[i];
rc += 1;
} else {
if i == str.count - 1 return "", false;
i += 1;
if str[i] == {
case #char "\""; #through;
case #char "/"; #through;
case #char "\\";
result[rc] = str[i];
rc += 1;
case #char "b";
result[rc] = 0x08;
rc += 1;
case #char "f";
result[rc] = 0x0c;
rc += 1;
case #char "n";
result[rc] = #char "\n";
rc += 1;
case #char "r";
result[rc] = #char "\r";
rc += 1;
case #char "t";
result[rc] = #char "\t";
rc += 1;
case #char "u";
if i + 4 >= str.count return "", false;
unicode_char, success := parse_unicode(slice(str, i + 1, 4));
if !success return "", false;
utf8_len := encode_utf8(unicode_char, *(result.data[rc]));
rc += utf8_len;
i += 4;
case;
return "", false;
}
}
}
result.count = rc;
return result, true;
}
#import "Basic";
#import "File";
#import "String";
#import "Hash_Table";
#import,dir "./unicode_utils";
#import "IntroSort";