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
extern crate itertools;
#[cfg(test)]
#[macro_use]
extern crate serde_json;
#[cfg(not(test))]
extern crate serde_json;

pub fn to_json(v: &serde_json::Value) -> String {
    let s = String::new();
    to_json_(v, s, "")
}

fn escape_json_string(out: &mut String, s: &str) {
    out.push_str("\"");

    let bytes = s.as_bytes();

    let mut start = 0;

    for (i, &byte) in bytes.iter().enumerate() {
        let escape = ESCAPE[byte as usize];
        if escape == 0 {
            continue;
        }

        if start < i {
            out.push_str(&s[start..i]);
        }

        let char_escape = CharEscape::from_escape_table(escape, byte);
        out.push_str(&write_char_escape(char_escape));

        start = i + 1;
    }

    if start != bytes.len() {
        out.push_str(&s[start..]);
    }

    out.push_str("\"");
}

const TAB: &str = "  ";

fn to_json_(v: &serde_json::Value, mut out: String, prefix: &str) -> String {
    // pretty printer for json that prints the dict keys in sorted order
    let prefix2 = format!("{}{}", prefix, TAB);
    match v {
        serde_json::Value::String(s) => escape_json_string(&mut out, s),
        serde_json::Value::Null => out.push_str("null"),
        serde_json::Value::Bool(b) => if *b {
            out.push_str("true")
        } else {
            out.push_str("false")
        },
        serde_json::Value::Number(n) => out.push_str(&format!("{}", n)),
        serde_json::Value::Array(a) => {
            let len = a.len();
            if len == 0 {
                out.push_str("[]");
            } else {
                out.push_str("[\n");
                for (idx, item) in itertools::enumerate(a.iter()) {
                    out.push_str(&prefix2);
                    out = to_json_(item, out, &prefix2);
                    if idx < len - 1 {
                        out.push_str(",\n");
                    }
                }
                out.push_str("\n");
                out.push_str(&prefix);
                out.push_str("]");
            }
        }
        serde_json::Value::Object(m) => {
            let len = m.len();
            if len == 0 {
                out.push_str("{}");
            } else {
                out.push_str("{\n");
                for (idx, k) in itertools::enumerate(itertools::sorted(m.keys())) {
                    let v = m.get(k).unwrap();
                    out.push_str(&prefix2);
                    escape_json_string(&mut out, k);
                    out.push_str(": ");
                    out = to_json_(v, out, &prefix2);
                    if idx < len - 1 {
                        out.push_str(",\n");
                    }
                }
                out.push_str("\n");
                out.push_str(&prefix);
                out.push_str("}");
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use serde_json::{value::Number, Value};

    fn a(v: Value, out: &'static str) {
        assert_eq!(super::to_json(&v), out);
    }

    #[test]
    fn to_json() {
        a(json! {null}, "null");
        a(json! {1}, "1");
        a(Value::Number(Number::from_f64(1.0).unwrap()), "1");
        a(
            Value::Number(Number::from_f64(-1.0002300e2).unwrap()),
            "-100.023",
        );
        a(json!{"foo"}, "\"foo\"");
        a(json!{r#"hello "world""#}, r#""hello \"world\"""#);
        a(
            json!{[1, 2]},
            "[
  1,
  2
]",
        );
        a(
            json!{[1, 2, []]},
            "[
  1,
  2,
  []
]",
        );
        a(
            json!{[1, 2, [1, 2, [1, 2]]]},
            "[
  1,
  2,
  [
    1,
    2,
    [
      1,
      2
    ]
  ]
]",
        );
        a(
            json!{{"yo": 1, "lo": 2, "no": {}}},
            "{
  \"lo\": 2,
  \"no\": {},
  \"yo\": 1
}",
        );
        a(
            json!{{"yo": 1, "lo": 2, "baz": {"one": 1, "do": 2, "tres": {"x": "x", "y": "y"}}}},
            "{
  \"baz\": {
    \"do\": 2,
    \"one\": 1,
    \"tres\": {
      \"x\": \"x\",
      \"y\": \"y\"
    }
  },
  \"lo\": 2,
  \"yo\": 1
}",
        );
        a(
            json!{{"yo": 1, "lo": 2, "baz": {"one": 1, "do": 2, "tres": ["x", "x", "y", "y"]}}},
            "{
  \"baz\": {
    \"do\": 2,
    \"one\": 1,
    \"tres\": [
      \"x\",
      \"x\",
      \"y\",
      \"y\"
    ]
  },
  \"lo\": 2,
  \"yo\": 1
}",
        );
    }
}

const BB: u8 = b'b'; // \x08
const TT: u8 = b't'; // \x09
const NN: u8 = b'n'; // \x0A
const FF: u8 = b'f'; // \x0C
const RR: u8 = b'r'; // \x0D
const QU: u8 = b'"'; // \x22
const BS: u8 = b'\\'; // \x5C
const U: u8 = b'u'; // \x00...\x1F except the ones above

// Lookup table of escape sequences. A value of b'x' at index i means that byte
// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.
#[cfg_attr(rustfmt, rustfmt_skip)]
static ESCAPE: [u8; 256] = [
    //  1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    U,  U,  U,  U,  U,  U,  U,  U, BB, TT, NN,  U, FF, RR,  U,  U, // 0
    U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U,  U, // 1
    0,  0, QU,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 2
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 3
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 4
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, BS,  0,  0,  0, // 5
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 6
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 7
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 8
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 9
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // A
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // B
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // C
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // D
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // E
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // F
];

/// Represents a character escape code in a type-safe manner.
pub enum CharEscape {
    /// An escaped quote `"`
    Quote,
    /// An escaped reverse solidus `\`
    ReverseSolidus,
    /// An escaped solidus `/`
    Solidus,
    /// An escaped backspace character (usually escaped as `\b`)
    Backspace,
    /// An escaped form feed character (usually escaped as `\f`)
    FormFeed,
    /// An escaped line feed character (usually escaped as `\n`)
    LineFeed,
    /// An escaped carriage return character (usually escaped as `\r`)
    CarriageReturn,
    /// An escaped tab character (usually escaped as `\t`)
    Tab,
    /// An escaped ASCII plane control character (usually escaped as
    /// `\u00XX` where `XX` are two hex characters)
    AsciiControl(u8),
}

impl CharEscape {
    #[inline]
    fn from_escape_table(escape: u8, byte: u8) -> CharEscape {
        match escape {
            self::BB => CharEscape::Backspace,
            self::TT => CharEscape::Tab,
            self::NN => CharEscape::LineFeed,
            self::FF => CharEscape::FormFeed,
            self::RR => CharEscape::CarriageReturn,
            self::QU => CharEscape::Quote,
            self::BS => CharEscape::ReverseSolidus,
            self::U => CharEscape::AsciiControl(byte),
            _ => unreachable!(),
        }
    }
}

#[inline]
fn write_char_escape(char_escape: CharEscape) -> String {
    use self::CharEscape::*;

    let mut out: Vec<u8> = vec![];
    match char_escape {
        Quote => out.extend(b"\\\""),
        ReverseSolidus => out.extend(b"\\\\"),
        Solidus => out.extend(b"\\/"),
        Backspace => out.extend(b"\\b"),
        FormFeed => out.extend(b"\\f"),
        LineFeed => out.extend(b"\\n"),
        CarriageReturn => out.extend(b"\\r"),
        Tab => out.extend(b"\\t"),
        AsciiControl(byte) => {
            static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";
            let bytes = &[
                b'\\',
                b'u',
                b'0',
                b'0',
                HEX_DIGITS[(byte >> 4) as usize],
                HEX_DIGITS[(byte & 0xF) as usize],
            ];
            out.extend(bytes);
        }
    };
    String::from_utf8(out).unwrap()
}