1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[derive(Debug, Clone)]
4pub struct JsonMinifier {
5 pub is_string: bool,
6 pub escaped_quotation: u8,
7}
8
9impl Default for JsonMinifier {
10 fn default() -> Self {
11 Self::new()
12 }
13}
14
15impl JsonMinifier {
16 pub fn new() -> Self {
17 JsonMinifier {
18 is_string: false,
19 escaped_quotation: 0,
20 }
21 }
22}
23
24#[inline]
25pub fn keep_element(minifier: &mut JsonMinifier, item1: &char, item2: Option<&char>) -> bool {
26 let remove_element: bool =
27 item1.is_ascii_control() || is_whitespace_outside_string(minifier, item1, item2);
28 !remove_element
29}
30
31#[inline]
32fn is_whitespace_outside_string(
33 minifier: &mut JsonMinifier,
34 item1: &char,
35 item2: Option<&char>,
36) -> bool {
37 if !minifier.is_string && item1.eq(&'"') {
38 minifier.is_string = true;
39 } else if minifier.is_string {
40 if item1.eq(&'\\') && item2.eq(&Some(&'"')) {
41 minifier.escaped_quotation = 4;
42 }
43 if minifier.escaped_quotation > 0 {
44 minifier.escaped_quotation -= 1;
45 } else if item1.eq(&'"') {
46 minifier.is_string = false;
47 }
48 }
49 !minifier.is_string && item1.is_whitespace()
50}
51