crates/edge/json/src/write.rs 2.8 KiB 91 lines Raw
1 //! The JSON writer.2 //!3 //! The output is compact: no spaces, no newline, keys in the sorted order that4 //! [`Value::Object`] already holds. Only `"`, `\` and the bytes below 0x20 are5 //! escaped, so text stays readable and stays UTF-8.6 //!7 //! JSON has no infinity, so a non-finite float writes as `null`. A float whose8 //! value is whole writes as an integer and therefore reads back as9 //! [`Value::Int`].10 11 use crate::value::Value;12 13 /// The hex digits used by `\u00XX`.14 const HEX: [char; 16] = [15     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',16 ];17 18 /// Appends the compact JSON form of `value` to `out`.19 pub fn encode(value: &Value, out: &mut String) {20     match value {21         Value::Null => out.push_str("null"),22         Value::Bool(b) => out.push_str(if *b { "true" } else { "false" }),23         Value::Int(n) => out.push_str(&n.to_string()),24         Value::Float(x) => {25             // Rust's Display gives the shortest form that reads back as the26             // same bits.27             if x.is_finite() {28                 out.push_str(&x.to_string());29             } else {30                 out.push_str("null");31             }32         }33         Value::Str(s) => write_string(s, out),34         Value::Array(items) => {35             out.push('[');36             for (index, item) in items.iter().enumerate() {37                 if index > 0 {38                     out.push(',');39                 }40                 encode(item, out);41             }42             out.push(']');43         }44         Value::Object(map) => {45             out.push('{');46             for (index, (key, value)) in map.iter().enumerate() {47                 if index > 0 {48                     out.push(',');49                 }50                 write_string(key, out);51                 out.push(':');52                 encode(value, out);53             }54             out.push('}');55         }56     }57 }58 59 /// Appends `value` as a JSON string.60 fn write_string(value: &str, out: &mut String) {61     out.push('"');62     for c in value.chars() {63         match c {64             '"' => out.push_str("\\\""),65             '\\' => out.push_str("\\\\"),66             '\u{08}' => out.push_str("\\b"),67             '\u{0c}' => out.push_str("\\f"),68             '\n' => out.push_str("\\n"),69             '\r' => out.push_str("\\r"),70             '\t' => out.push_str("\\t"),71             c => {72                 let code = c as u32;73                 if code < 0x20 {74                     out.push_str("\\u00");75                     out.push(HEX[((code >> 4) & 0xf) as usize]);76                     out.push(HEX[(code & 0xf) as usize]);77                 } else {78                     out.push(c);79                 }80             }81         }82     }83     out.push('"');84 }85 86 /// The compact JSON form of `value`.87 pub fn to_string(value: &Value) -> String {88     let mut out = String::new();89     encode(value, &mut out);90     out91 }