forked from rluba/jaison
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.jai
More file actions
282 lines (253 loc) · 8.51 KB
/
module.jai
File metadata and controls
282 lines (253 loc) · 8.51 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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, float_handling := Special_Float_Handling.SUPPORT_NULL) -> string {
builder: String_Builder;
defer free_buffers(*builder);
json_append_value(*builder, value, indent_char, ignore, rename, float_handling);
return builder_to_string(*builder);
}
json_write_file :: (filename: string, value: $T, indent_char := "\t", ignore := ignore_by_note, rename := rename_by_note, float_handling := Special_Float_Handling.SUPPORT_NULL) -> bool {
builder: String_Builder;
defer free_buffers(*builder);
json_append_value(*builder, value, indent_char, ignore, rename, float_handling);
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, float_handling := Special_Float_Handling.SUPPORT_NULL) {
#if T == JSON_Value {
json_write_json_value(builder, val, indent_char, float_handling=float_handling);
} else {
info := type_info(T);
json_write_native(builder, *val, info, indent_char, ignore, rename, float_handling=float_handling);
}
}
// 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;
}
// How shall NaN and Infinity be serialized / deserialized?
Special_Float_Handling :: enum_flags {
SUPPORT_NULL :: 0x01;
SUPPORT_STRINGS :: 0x02;
SUPPORT_ALL :: SUPPORT_NULL | SUPPORT_STRINGS;
}
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);
if !inside return "", "", false;
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;
success := true;
for i: 0..str.count-1 {
if str[i] != #char "\\" {
// Check for invalid characters for JSON
if str[i] < 0x20 {
success = false;
break;
}
result[rc] = str[i];
rc += 1;
} else {
if i == str.count - 1 {
success = false;
break;
}
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 {
success = false;
break;
}
unicode_char, unicode_success := parse_unicode(slice(str, i + 1, 4));
if !unicode_success {
success = false;
break;
}
utf8_len := encode_utf8(unicode_char, *(result.data[rc]));
rc += utf8_len;
i += 4;
case;
success = false;
break;
}
}
}
if !success {
free(result);
return "", false;
}
result.count = rc;
return result, true;
}
append_special_float :: (builder: *String_Builder, float_handling: Special_Float_Handling, is_nan: bool, is_negative: bool) {
if !(float_handling & .SUPPORT_STRINGS) {
append(builder, "null");
} else if is_nan {
append(builder, "\"NaN\"");
} else if is_negative {
append(builder, "\"-Infinity\"");
} else {
append(builder, "\"Infinity\"");
}
}
get_nan_or_infinity :: (value: u32) -> is_nan: bool, is_infinity: bool, is_negative: bool {
is_special := (value & 0x7F80_0000) == 0x7F80_0000;
mant := (value & 0x007F_FFFF);
return (is_special && mant), is_special && !mant, !!(value & 0x8000_0000);
}
get_nan_or_infinity :: (value: u64) -> is_nan: bool, is_infinity: bool, is_negative: bool {
is_special := (value & 0x7FF0_0000_0000_0000) == 0x7FF0_0000_0000_0000;
mant := (value & 0xF_FFFF_FFFF_FFF);
return (is_special && mant), is_special && !mant, !!(value & 0x8000_0000_0000_0000);
}
NAN :: 0h7FFF_FFFF_FFFF_FFFF;
INFINITY :: 0h7FF0_0000_0000_0000;
NEGATIVE_INFINITY :: 0h8FF0_0000_0000_0000;
#import "Basic";
#import "File";
#import "String";
#import "Hash_Table";
#import,dir "./unicode_utils";
#import "IntroSort";