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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::io;
use std::io::Read;
use crate::symbol::SYMBOL;

#[cfg(test)]
mod tests;

pub mod boolean;
pub mod string;
pub mod null;
pub mod float;
pub mod integer;
pub mod object;

pub struct RawUnprocessedJSONArray;
impl RawUnprocessedJSONArray {
    pub fn split_into_vector_of_strings(_json_string: String) -> Result<Vec<String>, String> {
        let mut list : Vec<String> = vec![];

        // cursor
        let mut is_end_of_json_string = false;
        let mut cursor = io::Cursor::new(_json_string.to_string());
        let mut bytes_read : i128 = 0;
        let total_bytes : i128 = _json_string.len() as i128;


        // read the start of the array

        let mut read_until_start_of_array = true;
        while read_until_start_of_array {

            if is_end_of_json_string {
                let message = format!("not proper end of the json array: {}", _json_string.to_string());
                return Err(message);
            }

            let byte = 0;
            let mut char_buffer = vec![byte];
            let length = char_buffer.len();
            let boxed_read = cursor.read_exact(&mut char_buffer);
            if boxed_read.is_err() {
                let message = boxed_read.err().unwrap().to_string();
                return Err(message);
            }
            boxed_read.unwrap();
            bytes_read = bytes_read + length as i128;
            is_end_of_json_string = total_bytes == bytes_read;
            if is_end_of_json_string {
                let message = format!("not proper start of the json array: {}", _json_string.to_string());
                return Err(message);
            }
            let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

            if !char.is_whitespace() && char != '['{
                let message = format!("input string does not start with opening square bracket: {} in {}", char, _json_string);
                return Err(message);
            }

            if char == '[' {
                read_until_start_of_array = false;
            }
        }



        let mut is_end_of_array = false;
        let mut token;
        while !is_end_of_array {

            let byte = 0;
            let mut char_buffer = vec![byte];
            let length = char_buffer.len();
            let boxed_read = cursor.read_exact(&mut char_buffer);
            if boxed_read.is_err() {
                let message = boxed_read.err().unwrap().to_string();
                return Err(message);
            }
            boxed_read.unwrap();
            bytes_read = bytes_read + length as i128;
            let mut char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

            if char == ']' {
                is_end_of_array = true;
            }

            if char != ' ' && char != ']' {
                let is_string = char == '\"';
                if is_string {
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);

                    // read till non escaped '"'
                    let mut not_end_of_string_property_value = true;
                    while not_end_of_string_property_value {
                        let byte = 0;
                        char_buffer = vec![byte];
                        let boxed_read = cursor.read_exact(&mut char_buffer);
                        if boxed_read.is_err() {
                            let message = boxed_read.err().unwrap().to_string();
                            return Err(message);
                        }
                        boxed_read.unwrap();
                        let length = char_buffer.len();
                        bytes_read = bytes_read + length as i128;
                        let _char = String::from_utf8(char_buffer).unwrap();
                        let last_char_in_buffer = token.chars().last().unwrap().to_string();
                        not_end_of_string_property_value = _char != "\"" && last_char_in_buffer != "\\";
                        token = [token, _char.to_string()].join(SYMBOL.empty_string);
                    }
                    list.push(token.to_string());

                    // if char is whitespace read until non whitespace and check it is comma, if not return error
                    let is_whitespace = char == ' ';
                    if is_whitespace {
                        let mut read_till_end_of_whitespace = true;
                        while read_till_end_of_whitespace {
                            let byte = 0;
                            let mut char_buffer = vec![byte];
                            let length = char_buffer.len();
                            let boxed_read = cursor.read_exact(&mut char_buffer);
                            if boxed_read.is_err() {
                                let message = boxed_read.err().unwrap().to_string();
                                return Err(message);
                            }
                            boxed_read.unwrap();
                            bytes_read = bytes_read + length as i128;
                            char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

                            if char == ',' {
                                read_till_end_of_whitespace = false
                            } else {
                                if char == ']' {
                                    read_till_end_of_whitespace = false;
                                    is_end_of_array = true;
                                } else {
                                    let message = format!("Missing comma between array items or closing square bracket at the end of array: {}", _json_string);
                                    return Err(message);
                                }
                            }
                        }
                    }
                }

                let is_null = char == 'n';
                if is_null {
                    // read 'ull'
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    let byte = 0;
                    let mut char_buffer = vec![byte, byte, byte];
                    let length = char_buffer.len();
                    let boxed_read = cursor.read_exact(&mut char_buffer);
                    if boxed_read.is_err() {
                        let message = boxed_read.err().unwrap().to_string();
                        return Err(message);
                    }
                    boxed_read.unwrap();
                    bytes_read = bytes_read + length as i128;
                    let remaining_bool = String::from_utf8(char_buffer).unwrap();
                    if remaining_bool != "ull" {
                        let message = format!("Unable to parse null: {} in {}", remaining_bool, _json_string);
                        return Err(message)
                    }
                    token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
                    list.push(token.to_string());
                }

                let is_boolean_true = char == 't';
                if is_boolean_true {
                    // read 'rue'
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    let byte = 0;
                    let mut char_buffer = vec![byte, byte, byte];
                    let length = char_buffer.len();
                    let boxed_read = cursor.read_exact(&mut char_buffer);
                    if boxed_read.is_err() {
                        let message = boxed_read.err().unwrap().to_string();
                        return Err(message);
                    }
                    boxed_read.unwrap();
                    bytes_read = bytes_read + length as i128;
                    let remaining_bool = String::from_utf8(char_buffer).unwrap();
                    if remaining_bool != "rue" {
                        let message = format!("Unable to parse true: {} in {}", remaining_bool, _json_string);
                        return Err(message)
                    }
                    token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
                    list.push(token.to_string());
                }

                let is_boolean_false = char == 'f';
                if is_boolean_false {
                    // read 'alse'
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    let byte = 0;
                    let mut char_buffer = vec![byte, byte, byte, byte];
                    let length = char_buffer.len();
                    let boxed_read = cursor.read_exact(&mut char_buffer);
                    if boxed_read.is_err() {
                        let message = boxed_read.err().unwrap().to_string();
                        return Err(message);
                    }
                    boxed_read.unwrap();
                    bytes_read = bytes_read + length as i128;
                    let remaining_bool = String::from_utf8(char_buffer).unwrap();
                    if remaining_bool != "alse" {
                        let message = format!("Unable to parse false: {} in {}", remaining_bool, _json_string);
                        return Err(message)
                    }
                    token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
                    list.push(token.to_string());
                }

                let is_array = char == '[';
                if is_array {
                    // read the array (including nested objects and arrays)
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    let mut number_of_open_square_brackets = 1;
                    let mut number_of_closed_square_brackets = 0;

                    let mut read_nested_array = true;
                    while read_nested_array {

                        let byte = 0;
                        let mut char_buffer = vec![byte];
                        let length = char_buffer.len();
                        let boxed_read = cursor.read_exact(&mut char_buffer);
                        if boxed_read.is_err() {
                            let message = boxed_read.err().unwrap().to_string();
                            return Err(message);
                        }
                        boxed_read.unwrap();
                        bytes_read = bytes_read + length as i128;
                        let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

                        let is_open_square_bracket = char == '[';
                        if is_open_square_bracket {
                            number_of_open_square_brackets = number_of_open_square_brackets + 1;
                        }


                        let is_close_square_bracket = char == ']';
                        if is_close_square_bracket {
                            number_of_closed_square_brackets = number_of_closed_square_brackets + 1;
                        }

                        token = [token.to_string(), char.to_string()].join(SYMBOL.empty_string);

                        if number_of_open_square_brackets == number_of_closed_square_brackets {
                            list.push(token.to_string());
                            read_nested_array = false;
                        }
                    }
                }


                let is_nested_object = char == '{';
                if is_nested_object {
                    // read the object (including nested objects and arrays)
                    token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    let mut number_of_open_curly_braces = 1;
                    let mut number_of_closed_curly_braces = 0;

                    let mut read_nested_object = true;
                    while read_nested_object {

                        let byte = 0;
                        let mut char_buffer = vec![byte];
                        let length = char_buffer.len();
                        let boxed_read = cursor.read_exact(&mut char_buffer);
                        if boxed_read.is_err() {
                            let message = boxed_read.err().unwrap().to_string();
                            return Err(message);
                        }
                        boxed_read.unwrap();
                        bytes_read = bytes_read + length as i128;
                        let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

                        let is_open_curly_brace = char == '{';
                        if is_open_curly_brace {
                            number_of_open_curly_braces = number_of_open_curly_braces + 1;
                        }


                        let is_close_curly_brace = char == '}';
                        if is_close_curly_brace {
                            number_of_closed_curly_braces = number_of_closed_curly_braces + 1;
                        }

                        token = [token.to_string(), char.to_string()].join(SYMBOL.empty_string);

                        if number_of_open_curly_braces == number_of_closed_curly_braces {
                            list.push(token.to_string());
                            read_nested_object = false;
                        }
                    }
                }

                let mut is_comma_separator = char == ',';
                let is_numeric = char.is_numeric();
                let is_minus = char == '-';

                let is_number =
                    !is_string &&
                        !is_null &&
                        !is_boolean_true &&
                        !is_boolean_false &&
                        !is_array &&
                        !is_nested_object &&
                        !is_comma_separator &&
                        (is_numeric || is_minus);
                if is_number {
                    token = "".to_string();
                    // read until char is not number and decimal point, minus, exponent
                    if char != ',' {
                        token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
                    }

                    let mut _is_point_symbol_already_used = false;
                    let mut _is_exponent_symbol_already_used = false;
                    let mut _is_minus_symbol_already_used = false;
                    if char == '-' {
                        _is_minus_symbol_already_used = true;
                    }

                    let mut read_number = true;
                    while read_number {

                        let byte = 0;
                        let mut char_buffer = vec![byte];
                        let length = char_buffer.len();
                        let boxed_read = cursor.read_exact(&mut char_buffer);
                        if boxed_read.is_err() {
                            let message = boxed_read.err().unwrap().to_string();
                            return Err(message);
                        }
                        boxed_read.unwrap();
                        bytes_read = bytes_read + length as i128;
                        char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

                        let is_numeric = char.is_numeric();

                        let is_point_symbol = char == '.';
                        if is_point_symbol && _is_point_symbol_already_used {
                            _is_point_symbol_already_used = true;
                            let message = format!("unable to parse number: {} in {}", token, _json_string);
                            return Err(message)
                        }
                        if is_point_symbol {
                            _is_point_symbol_already_used = true;
                        }

                        let is_exponent_symbol = char == 'e';
                        if is_exponent_symbol && _is_exponent_symbol_already_used {
                            _is_exponent_symbol_already_used = true;
                            let message = format!("unable to parse number: {} in {}", token, _json_string);
                            return Err(message)
                        }
                        if is_exponent_symbol {
                            _is_exponent_symbol_already_used = true;
                        }

                        let is_minus_symbol = char == '-';
                        if is_minus_symbol && _is_minus_symbol_already_used {
                            _is_minus_symbol_already_used = true;
                            let message = format!("unable to parse number: {} in {}", token, _json_string);
                            return Err(message)
                        }

                        let char_is_part_of_number = is_numeric || is_point_symbol || is_exponent_symbol || is_minus_symbol;
                        // if char is whitespace read until non whitespace and check it is comma, if not return error
                        let is_whitespace = char == ' ';
                        if is_whitespace {
                            let mut read_till_end_of_whitespace = true;
                            while read_till_end_of_whitespace {
                                let byte = 0;
                                let mut char_buffer = vec![byte];
                                let length = char_buffer.len();
                                let boxed_read = cursor.read_exact(&mut char_buffer);
                                if boxed_read.is_err() {
                                    let message = boxed_read.err().unwrap().to_string();
                                    return Err(message);
                                }
                                boxed_read.unwrap();
                                bytes_read = bytes_read + length as i128;
                                char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

                                if char == ',' {
                                    read_till_end_of_whitespace = false
                                } else {
                                    if char == ']' {
                                        read_till_end_of_whitespace = false;
                                        is_end_of_array = true;
                                    } else {
                                        let message = format!("Missing comma between array items or closing square bracket at the end of array: {}", _json_string);
                                        return Err(message);
                                    }
                                }
                            }
                        }

                        if char_is_part_of_number {
                            token = [token, char.to_string()].join(SYMBOL.empty_string);
                        } else {
                            read_number = false;
                            // if char is not array element separator or end of the array
                            if char != ',' && char != ']' {
                                let message = format!("unable to parse number: {} in {}", char, _json_string);
                                return Err(message)
                            }

                        }
                    }
                    list.push(token.to_string());
                }

                is_comma_separator = char == ',';
                let is_ascii_control = char.is_ascii_control();
                let is_carriage_return = char == '\r';
                let is_newline = char == '\n';
                let is_not_supported_type =
                    !is_string &&
                        !is_null &&
                        !is_boolean_true &&
                        !is_boolean_false &&
                        !is_array &&
                        !is_nested_object &&
                        !is_comma_separator &&
                        !is_carriage_return &&
                        !is_newline &&
                        !is_ascii_control &&
                        !is_numeric;
                if is_not_supported_type {
                    let message = format!("unknown type: {} in {}", char, _json_string);
                    return Err(message);
                }

            }


            if !is_end_of_array {
                is_end_of_array = char == ']';
            }

        }


        is_end_of_json_string = total_bytes == bytes_read;
        let mut read_after_end_of_array = !is_end_of_json_string;
        while read_after_end_of_array {

            let byte = 0;
            let mut char_buffer = vec![byte];
            let length = char_buffer.len();
            let boxed_read = cursor.read_exact(&mut char_buffer);
            if boxed_read.is_err() {
                let message = boxed_read.err().unwrap().to_string();
                return Err(message);
            }
            boxed_read.unwrap();
            bytes_read = bytes_read + length as i128;
            let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();

            if !char.is_whitespace(){
                let message = format!("after array there are some characters: {} in {}", char, _json_string);
                return Err(message);
            }

            if bytes_read == total_bytes {
                read_after_end_of_array = false;
            }
        }

        Ok(list)
    }
}