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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#[derive(Debug)]
pub enum Json {
    OBJECT { name: String, value: Box<Json> },
    JSON(Vec<Json>),
    ARRAY(Vec<Json>),
    STRING(String),
    NUMBER(f64),
    BOOL(bool),
    NULL,
}

impl Json {
    /// Construct a new `Json::JSON`
    /// ## Example
    /// ```
    /// use json_minimal::*;
    ///
    /// let mut json = Json::new();
    /// ```
    pub fn new() -> Json {
        Json::JSON(Vec::new())
    }

    /// Add any `Json` variant to a `Json` variant of type `Json::JSON`, `Json::ARRAY`
    /// or a `Json::OBJECT` (holding a `Json::JSON`,`Json::ARRAY`,`Json::OBJECT` (holding a `Json::JSON`,`Json::`...)).
    /// ## Panics!
    /// Will panic if the conditions stated above are not met OR if an attempt is made to add a `Json::JSON` to a `Json::JSON`
    /// without wrapping it in a `Json::OBJECT` first.
    /// ## Example
    /// ```
    ///     use json_minimal::*;
    ///     
    ///     let mut json = Json::new();
    ///
    ///     json
    ///         .add(
    ///             Json::OBJECT {
    ///                 name: String::from("Greeting"),
    ///
    ///                 value: Box::new(
    ///                     Json::STRING( String::from("Hello, world!") )
    ///                 )
    ///             }
    ///         )
    ///     ;
    /// ```
    /// See the <a href="https://github.com/36den/json_minimal-rs/">tutorial</a> on github for more.
    pub fn add(&mut self, value: Json) -> &mut Json {
        match self {
            Json::JSON(values) => match value {
                Json::OBJECT { name, value } => {
                    values.push(Json::OBJECT { name, value });
                }
                Json::JSON(_) => {
                    panic!("A `Json::JSON` may not be added to a `Json::JSON` if it is not within a `Json::OBJECT`.");
                }
                Json::ARRAY(vals) => {
                    values.push(Json::ARRAY(vals));
                }
                Json::STRING(val) => {
                    values.push(Json::STRING(val));
                }
                Json::NUMBER(val) => {
                    values.push(Json::NUMBER(val));
                }
                Json::BOOL(val) => {
                    values.push(Json::BOOL(val));
                }
                Json::NULL => {
                    values.push(Json::NULL);
                }
            },
            Json::OBJECT {
                name: _,
                value: obj_val,
            } => match obj_val.unbox_mut() {
                Json::JSON(values) => match value {
                    Json::OBJECT { name, value } => {
                        values.push(Json::OBJECT { name, value });
                    }
                    Json::JSON(_) => {
                        panic!("A `Json::JSON` may not be added to a `Json::JSON` if it is not within a `Json::OBJECT`.");
                    }
                    Json::ARRAY(vals) => {
                        values.push(Json::ARRAY(vals));
                    }
                    Json::STRING(val) => {
                        values.push(Json::STRING(val));
                    }
                    Json::NUMBER(val) => {
                        values.push(Json::NUMBER(val));
                    }
                    Json::BOOL(val) => {
                        values.push(Json::BOOL(val));
                    }
                    Json::NULL => {
                        values.push(Json::NULL);
                    }
                },
                Json::ARRAY(values) => match value {
                    Json::OBJECT { name, value } => {
                        values.push(Json::OBJECT { name, value });
                    }
                    Json::JSON(vals) => {
                        values.push(Json::JSON(vals));
                    }
                    Json::ARRAY(vals) => {
                        values.push(Json::ARRAY(vals));
                    }
                    Json::STRING(val) => {
                        values.push(Json::STRING(val));
                    }
                    Json::NUMBER(val) => {
                        values.push(Json::NUMBER(val));
                    }
                    Json::BOOL(val) => {
                        values.push(Json::BOOL(val));
                    }
                    Json::NULL => {
                        values.push(Json::NULL);
                    }
                },
                json => {
                    panic!("The function `add(`&mut self`,`name: String`,`value: Json`)` may only be called on a `Json::JSON`, `Json::ARRAY` or `Json::OBJECT` holding a `Json::JSON` or `Json::ARRAY`. It was called on: {:?}",json);
                }
            },
            Json::ARRAY(values) => match value {
                Json::OBJECT { name, value } => {
                    values.push(Json::OBJECT { name, value });
                }
                Json::JSON(vals) => {
                    values.push(Json::JSON(vals));
                }
                Json::ARRAY(vals) => {
                    values.push(Json::ARRAY(vals));
                }
                Json::STRING(val) => {
                    values.push(Json::STRING(val));
                }
                Json::NUMBER(val) => {
                    values.push(Json::NUMBER(val));
                }
                Json::BOOL(val) => {
                    values.push(Json::BOOL(val));
                }
                Json::NULL => {
                    values.push(Json::NULL);
                }
            },
            json => {
                panic!("The function `add(`&mut self`,`name: String`,`value: Json`)` may only be called on a `Json::JSON`, `Json::ARRAY` or `Json::OBJECT` holding a `Json::JSON` or `Json::ARRAY`. It was called on: {:?}",json);
            }
        }

        self
    }

    /// Get the `Json` with the requested name if it exists.
    /// ## Panics
    /// This function will panic if called on a `Json` variant other than `Json::JSON` or `Json::OBJECT`,
    /// as only these two variants may hold `Json::OBJECT` (which has a `name` field).
    /// ## Example
    /// ```
    /// use json_minimal::*;
    ///
    /// let mut json = Json::new();
    ///
    /// json
    ///     .add(
    ///         Json::OBJECT {
    ///             name: String::from("Greeting"),
    ///
    ///             value: Box::new(
    ///                 Json::STRING( String::from("Hello, world!") )
    ///             )
    ///         }
    ///     )
    /// ;
    ///
    /// match json.get("Greeting") {
    ///     Some(json) => {
    ///         match json {
    ///             Json::OBJECT { name, value } => {
    ///                 match value.unbox() { // See `unbox()` below
    ///                     Json::STRING(val) => {
    ///                         assert_eq!("Hello, world!",val);
    ///                     },
    ///                     _ => {
    ///                         panic!("I expected this to be a `Json::STRING`!!!");
    ///                     }
    ///                 }   
    ///             },
    ///             _ => {
    ///                 panic!("This shouldn't happen!!!");
    ///             }
    ///         }
    ///     },
    ///     None => {
    ///         panic!("Not found!!!");
    ///     }
    /// }
    /// ```
    pub fn get(&self, search: &str) -> Option<&Json> {
        match self {
            Json::JSON(values) => {
                for n in 0..values.len() {
                    match &values[n] {
                        Json::OBJECT { name, value: _ } => {
                            if name == search {
                                return Some(&values[n]);
                            }
                        }
                        _ => {}
                    }
                }

                return None;
            }
            Json::OBJECT { name: _, value } => match value.unbox() {
                Json::JSON(values) => {
                    for n in 0..values.len() {
                        match &values[n] {
                            Json::OBJECT { name, value: _ } => {
                                if name == search {
                                    return Some(&values[n]);
                                }
                            }
                            _ => {}
                        }
                    }

                    return None;
                }
                json => {
                    panic!("The function `get(`&self`,`search: &str`)` may only be called on a `Json::JSON` or a `Json::OBJECT` holding a `Json::JSON`. I was called on: {:?}",json);
                }
            },
            json => {
                panic!("The function `get(`&self`,`search: &str`)` may only be called on a `Json::JSON`. I was called on: {:?}",json);
            }
        }
    }

    /// Same as `get` above, but the references are mutable. Use `unbox_mut()` (see below) with this one.
    /// ## Panics
    /// This function will panic if called on a `Json` variant other than `Json::JSON` or `Json::OBJECT`,
    /// as only these two variants may hold `Json::OBJECT` which has a `name` field.
    pub fn get_mut(&mut self, search: &str) -> Option<&mut Json> {
        match self {
            Json::JSON(values) => {
                for n in 0..values.len() {
                    match &values[n] {
                        Json::OBJECT { name, value: _ } => {
                            if name == search {
                                return Some(&mut values[n]);
                            }
                        }
                        _ => {}
                    }
                }
            }
            Json::OBJECT { name: _, value } => match value.unbox_mut() {
                Json::JSON(values) => {
                    for n in 0..values.len() {
                        match &values[n] {
                            Json::OBJECT { name, value: _ } => {
                                if name == search {
                                    return Some(&mut values[n]);
                                }
                            }
                            _ => {}
                        }
                    }
                }
                json => {
                    panic!("The function `get_mut(`&self`,`search: &str`)` may only be called on a `Json::JSON` or a `Json::OBJECT` holding a `Json::JSON`. I was called on: {:?}",json);
                }
            },
            json => {
                panic!("The function `get_mut(`&self`,`search: &str`)` may only be called on a `Json::JSON` or a `Json::OBJECT` holding a `Json::JSON`. I was called on: {:?}",json);
            }
        }

        None
    }

    /// Enables matching the contents of a `Box`.
    pub fn unbox(&self) -> &Json {
        self
    }

    /// Idem.
    pub fn unbox_mut(&mut self) -> &mut Json {
        self
    }

    /// Returns a `String` of the form: `{"Json":"Value",...}` but can also be called on 'standalone objects'
    /// which could result in `"Object":{"Stuff":...}` or `"Json":true`.
    pub fn print(&self) -> String {
        let mut result = String::new();

        match self {
            Json::OBJECT { name, value } => {
                result.push_str(&format!("\"{}\":{}", name, value.print()));
            }
            Json::JSON(values) => {
                result.push('{');

                for n in 0..values.len() {
                    result.push_str(&values[n].print());
                    result.push(',');
                }

                result.pop();

                result.push('}');
            }
            Json::ARRAY(values) => {
                result.push('[');

                for n in 0..values.len() {
                    result.push_str(&values[n].print());
                    result.push(',');
                }

                result.pop();

                result.push(']');
            }
            Json::STRING(val) => {
                result.push_str(&format!("\"{}\"", val));
            }
            Json::NUMBER(val) => {
                result.push_str(&format!("{}", val));
            }
            Json::BOOL(val) => {
                if *val {
                    result.push_str("true");
                } else {
                    result.push_str("false")
                }
            }
            Json::NULL => {
                result.push_str("null");
            }
        }

        result
    }

    /// Parses the given bytes if a json structure is found. It even works with `\"Hello\":\"World\"`
    /// (doesn't have to be like `{...}`), i.e. it can return any of the variants in the `Json` enum.
    /// The error is returned in the form `(last position, what went wrong)`. Unfortunately the error
    /// description are minimal (basically "Error parsing ...type...").
    /// ## Example
    /// ```
    /// use json_minimal::*;
    ///
    /// match Json::parse(b"{\"Greeting\":\"Hello, world!\"}") {
    ///     Ok(json) => {
    ///         
    ///         match json.get("Greeting") {
    ///             Some(json) => {
    ///                 match json {
    ///                     Json::OBJECT { name, value } => {
    ///                         match value.unbox() {
    ///                             Json::STRING(val) => {
    ///                                 assert_eq!(val,"Hello, world!");
    ///                             },
    ///                             json => {
    ///                                 panic!("Expected Json::STRING but found {:?}!!!",json);
    ///                             }
    ///                         }
    ///                     }
    ///                     json => {
    ///                         panic!("Expected Json::OBJECT but found {:?}!!!",json);
    ///                     }
    ///                 }
    ///             },
    ///             None => {
    ///                 panic!("Greeting was not found!!!");
    ///             }
    ///         }
    ///     },
    ///     Err( (pos,msg) ) => {
    ///         panic!("`{}` at position `{}`!!!",msg,pos);
    ///     }
    /// }
    /// ```
    /// See the <a href="https://github.com/36den/json_minimal-rs/">tutorial</a> on github for more.
    pub fn parse(input: &[u8]) -> Result<Json, (usize, &'static str)> {
        let mut incr: usize = 0;

        match input[incr] as char {
            '{' => Self::parse_json(input, &mut incr),
            '\"' => Self::parse_string(input, &mut incr),
            '[' => Self::parse_array(input, &mut incr),
            't' | 'f' => Self::parse_bool(input, &mut incr),
            'n' => Self::parse_null(input, &mut incr),
            '0'..='9' => Self::parse_number(input, &mut incr),
            _ => Err((incr, "Not a valid json format")),
        }
    }

    // This must exclusively be used by `parse_string` to make any sense.
    fn parse_object(
        input: &[u8],
        incr: &mut usize,
        name: String,
    ) -> Result<Json, (usize, &'static str)> {
        if input[*incr] as char != ':' {
            return Err((*incr, "Error parsing object."));
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing object."));
        }

        loop {
            match input[*incr] as char {
                '\r' | '\n' | '\t' | ' ' => {
                    *incr += 1;

                    if *incr >= input.len() {
                        return Err((*incr, "Error parsing object."));
                    }
                },
                _ => {
                 break;
                }
            }
        }

        let value = match input[*incr] as char {
            '{' => Self::parse_json(input, incr)?,
            '[' => Self::parse_array(input, incr)?,
            '\"' => Self::parse_string(input, incr)?,
            't' | 'f' => Self::parse_bool(input, incr)?,
            'n' => Self::parse_null(input, incr)?,
            '0'..='9' => Self::parse_number(input, incr)?,
            _ => {
                return Err((*incr, "Error parsing object."));
            }
        };

        Ok(Json::OBJECT {
            name,

            value: Box::new(value),
        })
    }

    // Parse if you thik it's something like `{...}`
    fn parse_json(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result: Vec<Json> = Vec::new();

        if input[*incr] as char != '{' {
            return Err((*incr, "Error parsing json."));
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing json."));
        }

        loop {
            let json = match input[*incr] as char {
                ',' => {
                    *incr += 1;
                    continue;
                }
                '\"' => Self::parse_string(input, incr)?,
                '[' => Self::parse_array(input, incr)?,
                't' | 'f' => Self::parse_bool(input, incr)?,
                'n' => Self::parse_null(input, incr)?,
                '0'..='9' => Self::parse_number(input, incr)?,
                '}' => {
                    *incr += 1;

                    return Ok(Json::JSON(result));
                }
                '{' => Self::parse_json(input, incr)?,
                '\r' | '\n' | '\t' | ' ' => {                    
                    *incr += 1;

                    if *incr >= input.len() {
                        return Err((*incr, "Error parsing json."));
                    }

                    continue;
                },
                _ => {
                    return Err((*incr, "Error parsing json."));
                }
            };

            result.push(json);
        }
    }

    // Parse a &str if you're sure it resembles `[...`
    fn parse_array(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result: Vec<Json> = Vec::new();

        if input[*incr] as char != '[' {
            return Err((*incr, "Error parsing array."));
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing array."));
        }

        loop {
            let json = match input[*incr] as char {
                ',' => {
                    *incr += 1;
                    continue;
                }
                '\"' => Self::parse_string(input, incr)?,
                '[' => Self::parse_array(input, incr)?,
                '{' => Self::parse_json(input, incr)?,
                't' | 'f' => Self::parse_bool(input, incr)?,
                'n' => Self::parse_null(input, incr)?,
                '0'..='9' => Self::parse_number(input, incr)?,
                ']' => {
                    *incr += 1;

                    return Ok(Json::ARRAY(result));
                },
                '\r' | '\n' | '\t' | ' ' => {
                    *incr += 1;

                    if *incr >= input.len() {
                        return Err((*incr, "Error parsing array."));
                    }

                    continue;
                },
                _ => {
                    return Err((*incr, "Error parsing array."));
                }
            };

            result.push(json);
        }
    }

    // Parse a &str if you know that it corresponds to/starts with a json String.
    fn parse_string(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result: Vec<u8> = Vec::new();

        if input[*incr] as char != '\"' {
            return Err((*incr, "Error parsing string."));
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing string."));
        }

        loop {
            match input[*incr] {
                b'\"' => {
                    *incr += 1;

                    let result = String::from_utf8(result)
                        .map_err(|_| (*incr, "Error parsing non-utf8 string."))?;

                    if *incr < input.len() {
                        if input[*incr] as char == ':' {
                            return Self::parse_object(input, incr, result);
                        } else {
                            return Ok(Json::STRING(result));
                        }
                    } else {
                        return Ok(Json::STRING(result));
                    }
                },
                b'\\' => {
                    Self::parse_string_escape_sequence(input, incr, &mut result)?;
                },
                c => {
                    result.push(c);

                    *incr += 1;

                    if *incr >= input.len() {
                        return Err((*incr, "Error parsing string."));
                    }
                }
            }
        }
    }

    // Parse an escape sequence inside a string
    fn parse_string_escape_sequence(
        input: &[u8],
        incr: &mut usize,
        result: &mut Vec<u8>,
    ) -> Result<(), (usize, &'static str)> {
        if input[*incr] as char != '\\' {
            return Err((*incr, "Error parsing string escape sequence."));
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing string escape sequence."));
        }

        match input[*incr] as char {
            '\"' | '\\' | '/' => {
                result.push(input[*incr]);
            }
            'b' => {
                result.push(b'\x08');
            }
            'f' => {
                result.push(b'\x0c');
            }
            'n' => {
                result.push(b'\n');
            }
            'r' => {
                result.push(b'\r');
            }
            't' => {
                result.push(b'\t');
            }
            'u' => {
                const BAD_UNICODE: &str = "Error parsing unicode string escape sequence.";

                if *incr + 4 >= input.len() {
                    return Err((*incr, BAD_UNICODE));
                }

                let hex = (&input[*incr + 1..*incr + 5]).to_vec();
                let hex = String::from_utf8(hex).map_err(|_| (*incr, BAD_UNICODE))?;
                let value = u16::from_str_radix(&hex, 16).map_err(|_| (*incr, BAD_UNICODE))?;
                let value = std::char::from_u32(value as u32).ok_or((*incr, BAD_UNICODE))?;

                let mut buffer = [0; 4];
                result.extend(value.encode_utf8(&mut buffer).as_bytes());
                *incr += 4;
            }
            _ => {
                return Err((*incr, "Error parsing invalid string escape sequence."));
            }
        }

        *incr += 1;

        if *incr >= input.len() {
            return Err((*incr, "Error parsing string escape sequence."));
        }

        Ok(())
    }

    fn parse_number(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result = String::new();

        loop {
            match input[*incr] as char {
                ',' | ']' | '}' | '\r' | '\n' | '\t' | ' ' => {
                    break;
                },
                c => {
                    result.push(c);

                    *incr += 1;

                    if *incr >= input.len() {
                        match result.parse::<f64>() {
                            Ok(num) => {
                                return Ok(Json::NUMBER(num));
                            }
                            Err(_) => {
                                return Err((*incr, "Error parsing number."));
                            }
                        }
                    }
                }
            }
        }

        match result.parse::<f64>() {
            Ok(num) => {
                return Ok(Json::NUMBER(num));
            }
            Err(_) => {
                return Err((*incr, "Error parsing number."));
            }
        }
    }

    fn parse_bool(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result = String::new();

        loop {
            match input[*incr] as char {
                ',' | ']' | '}' | '\r' | '\n' | '\t' | ' ' => {
                    break;
                },
                c => {
                    result.push(c);

                    *incr += 1;

                    if *incr >= input.len() {
                        if result == "true" {
                            return Ok(Json::BOOL(true));
                        }

                        if result == "false" {
                            return Ok(Json::BOOL(false));
                        }

                        return Err((*incr, "Error parsing bool."));
                    }
                }
            }
        }

        if result == "true" {
            return Ok(Json::BOOL(true));
        }

        if result == "false" {
            return Ok(Json::BOOL(false));
        }

        return Err((*incr, "Error parsing bool."));
    }

    fn parse_null(input: &[u8], incr: &mut usize) -> Result<Json, (usize, &'static str)> {
        let mut result = String::new();

        loop {
            match input[*incr] as char {
                ',' | ']' | '}' | '\r' | '\n' | '\t' | ' ' => {
                    break;
                },
                c => {
                    result.push(c);

                    *incr += 1;

                    if *incr >= input.len() {
                        if result == "null" {
                            return Ok(Json::NULL);
                        } else {
                            return Err((*incr, "Error parsing null."));
                        }
                    }
                }
            }
        }

        if result == "null" {
            return Ok(Json::NULL);
        } else {
            return Err((*incr, "Error parsing null."));
        }
    }
}

#[cfg(test)]
mod tests;