Skip to main content

vivacity_core/
phpjson.rs

1//! JSON re-encoding reproducing PHP's `json_encode($data, 0)` applied to data
2//! coming from `json_decode($json, true)` (the JsonFile::parseJson ->
3//! JsonFile::encode pipeline, Composer 2.10.3, see docs/reference/JsonFile.php).
4//!
5//! Byte-exact semantics:
6//! - compact output (`{"k":v}`), insertion order preserved;
7//! - `/` escaped as `\/`, non-ASCII as lowercase-hex `\uXXXX` (surrogate
8//!   pairs beyond the BMP), controls `\b \f \n \r \t` then `\u00XX`;
9//! - assoc quirk: an empty object becomes `[]`, an object whose keys are
10//!   exactly "0".."n-1" in order becomes an array (PHP lost the
11//!   object/array distinction at decode time);
12//! - shortest round-trip floats (serialize_precision=-1): fixed notation
13//!   without a trailing `.0` for integral values (`1.0` -> `1`), exponential
14//!   `d[.ddd|.0]e±X` outside (-4, 17]; see `encode_double`.
15
16use crate::error::{Error, Result};
17use serde_json::Value;
18
19/// Options mirroring the json_encode flags used by Composer.
20#[derive(Debug, Clone, Copy)]
21pub struct EncodeOptions {
22    pub pretty: bool,
23    pub escape_slashes: bool,
24    pub escape_unicode: bool,
25}
26
27/// Flags 0 (content-hash): compact, slashes and unicode escaped.
28pub const FLAGS_ZERO: EncodeOptions = EncodeOptions {
29    pretty: false,
30    escape_slashes: true,
31    escape_unicode: true,
32};
33
34/// JsonFile default (files written by Composer):
35/// JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE.
36pub const FLAGS_JSONFILE: EncodeOptions = EncodeOptions {
37    pretty: true,
38    escape_slashes: false,
39    escape_unicode: false,
40};
41
42pub fn php_json_encode(value: &Value) -> Result<String> {
43    php_json_encode_with(value, FLAGS_ZERO)
44}
45
46/// Sentinel key: an object reduced to this key encodes as `{}` (an empty
47/// `stdClass` on the PHP side, which the array semantics cannot express).
48pub const STDCLASS_MARKER: &str = "\u{0}stdClass";
49
50/// An empty object that will encode as `{}`.
51pub fn empty_stdclass() -> Value {
52    let mut m = serde_json::Map::new();
53    m.insert(STDCLASS_MARKER.to_owned(), Value::Null);
54    Value::Object(m)
55}
56
57pub fn php_json_encode_with(value: &Value, opts: EncodeOptions) -> Result<String> {
58    let mut out = String::new();
59    encode_into(value, &mut out, opts, 0)?;
60    Ok(out)
61}
62
63fn newline_indent(out: &mut String, level: usize) {
64    out.push('\n');
65    for _ in 0..level {
66        out.push_str("    ");
67    }
68}
69
70fn encode_into(value: &Value, out: &mut String, opts: EncodeOptions, level: usize) -> Result<()> {
71    match value {
72        Value::Null => out.push_str("null"),
73        Value::Bool(true) => out.push_str("true"),
74        Value::Bool(false) => out.push_str("false"),
75        Value::Number(n) => encode_number(n, out)?,
76        Value::String(s) => encode_string_with(s, out, opts),
77        Value::Array(items) => encode_list(items.iter(), out, opts, level)?,
78        Value::Object(map) => {
79            if map.len() == 1 && map.contains_key(STDCLASS_MARKER) {
80                // Empty `new \stdClass` (Locker::fixupJsonDataType): `{}` where
81                // an empty array would give `[]`.
82                out.push_str("{}");
83            } else if is_php_list(map) {
84                encode_list(map.values(), out, opts, level)?;
85            } else {
86                out.push('{');
87                for (i, (key, item)) in map.iter().enumerate() {
88                    if i > 0 {
89                        out.push(',');
90                    }
91                    if opts.pretty {
92                        newline_indent(out, level + 1);
93                    }
94                    encode_string_with(key, out, opts);
95                    out.push(':');
96                    if opts.pretty {
97                        out.push(' ');
98                    }
99                    encode_into(item, out, opts, level + 1)?;
100                }
101                if opts.pretty && !map.is_empty() {
102                    newline_indent(out, level);
103                }
104                out.push('}');
105            }
106        }
107    }
108    Ok(())
109}
110
111fn encode_list<'a>(
112    items: impl ExactSizeIterator<Item = &'a Value>,
113    out: &mut String,
114    opts: EncodeOptions,
115    level: usize,
116) -> Result<()> {
117    let len = items.len();
118    out.push('[');
119    for (i, item) in items.enumerate() {
120        if i > 0 {
121            out.push(',');
122        }
123        if opts.pretty {
124            newline_indent(out, level + 1);
125        }
126        encode_into(item, out, opts, level + 1)?;
127    }
128    if opts.pretty && len > 0 {
129        newline_indent(out, level);
130    }
131    out.push(']');
132    Ok(())
133}
134
135/// After `json_decode(..., true)`, PHP encodes as an array any assoc array
136/// whose keys are exactly 0..n-1 in order (an empty object included).
137fn is_php_list(map: &serde_json::Map<String, Value>) -> bool {
138    map.keys()
139        .enumerate()
140        .all(|(i, k)| k.as_str() == i.to_string())
141}
142
143fn encode_number(n: &serde_json::Number, out: &mut String) -> Result<()> {
144    if let Some(i) = n.as_i64() {
145        out.push_str(&i.to_string());
146    } else if let Some(u) = n.as_u64() {
147        // PHP_INT_MAX == i64::MAX: beyond it, json_decode produces a float.
148        encode_double(u as f64, out)?;
149    } else if let Some(f) = n.as_f64() {
150        encode_double(f, out)?;
151    }
152    Ok(())
153}
154
155/// Double formatting of `json_encode` with serialize_precision=-1:
156/// shortest-round-trip digits, fixed notation iff the decimal point lies in
157/// (-4, 17] (bounds measured empirically on PHP 8.5, cf. differential tests),
158/// else exponential `d[.ddd|.0]e±X`. No `.0` on integral values in fixed.
159/// Shortest significant digits that round-trip, and decimal exponent, like
160/// `zend_gcvt` mode 0 (dtoa). Rust's `{:e}` gives the same string except on
161/// an exact tie between two candidates (the value sits halfway, e.g.
162/// 2124202659384827.25 -> "...27.2" or "...27.3"): dtoa rounds to the even
163/// digit, Rust rounds up. Rust's fixed-precision formatting (exact,
164/// half-to-even) decides like dtoa; we keep it if it still round-trips
165/// (always true away from an asymmetric interval edge).
166fn shortest_digits(a: f64) -> (String, String) {
167    let sci = format!("{:e}", a);
168    let (mantissa, exp) = sci.split_once('e').unwrap_or((sci.as_str(), "0"));
169    let digits: String = mantissa.chars().filter(|c| *c != '.').collect();
170    let exact = format!("{:.*e}", digits.len().saturating_sub(1), a);
171    if exact != sci && exact.parse::<f64>() == Ok(a) {
172        let (m, e) = exact.split_once('e').unwrap_or((exact.as_str(), "0"));
173        let mut d: String = m.chars().filter(|c| *c != '.').collect();
174        while d.len() > 1 && d.ends_with('0') {
175            d.pop();
176        }
177        return (d, e.to_owned());
178    }
179    (digits, exp.to_owned())
180}
181
182fn encode_double(f: f64, out: &mut String) -> Result<()> {
183    if !f.is_finite() {
184        return Err(Error::NonFiniteFloat(f));
185    }
186    if f == 0.0 {
187        out.push_str(if f.is_sign_negative() { "-0" } else { "0" });
188        return Ok(());
189    }
190    if f.is_sign_negative() {
191        out.push('-');
192    }
193    let (digits, exp) = shortest_digits(f.abs());
194    let exp: i32 = exp.parse().map_err(|_| Error::NonFiniteFloat(f))?;
195    let dec_point = exp + 1; // value = 0.digits x 10^dec_point
196
197    if dec_point > -4 && dec_point <= 17 {
198        let n = digits.len() as i32;
199        if dec_point <= 0 {
200            out.push_str("0.");
201            for _ in 0..-dec_point {
202                out.push('0');
203            }
204            out.push_str(&digits);
205        } else if dec_point >= n {
206            out.push_str(&digits);
207            for _ in 0..(dec_point - n) {
208                out.push('0');
209            }
210        } else {
211            out.push_str(&digits[..dec_point as usize]);
212            out.push('.');
213            out.push_str(&digits[dec_point as usize..]);
214        }
215    } else {
216        out.push_str(&digits[..1]);
217        out.push('.');
218        if digits.len() > 1 {
219            out.push_str(&digits[1..]);
220        } else {
221            out.push('0');
222        }
223        let e = dec_point - 1;
224        if e >= 0 {
225            out.push_str("e+");
226        } else {
227            out.push_str("e-");
228        }
229        out.push_str(&e.abs().to_string());
230    }
231    Ok(())
232}
233
234fn encode_string_with(s: &str, out: &mut String, opts: EncodeOptions) {
235    out.push('"');
236    for c in s.chars() {
237        match c {
238            '"' => out.push_str("\\\""),
239            '\\' => out.push_str("\\\\"),
240            '/' if opts.escape_slashes => out.push_str("\\/"),
241            '\u{08}' => out.push_str("\\b"),
242            '\u{0c}' => out.push_str("\\f"),
243            '\n' => out.push_str("\\n"),
244            '\r' => out.push_str("\\r"),
245            '\t' => out.push_str("\\t"),
246            c if (c as u32) < 0x20 => {
247                push_unicode_escape(c as u32, out);
248            }
249            c if c.is_ascii() => out.push(c),
250            // Without JSON_UNESCAPED_LINE_TERMINATORS, json_encode escapes
251            // U+2028/U+2029 even with JSON_UNESCAPED_UNICODE.
252            '\u{2028}' => out.push_str("\\u2028"),
253            '\u{2029}' => out.push_str("\\u2029"),
254            c if !opts.escape_unicode => out.push(c),
255            c => {
256                let cp = c as u32;
257                if cp > 0xFFFF {
258                    // UTF-16 surrogate pair, like json_encode.
259                    let v = cp - 0x10000;
260                    push_unicode_escape(0xD800 + (v >> 10), out);
261                    push_unicode_escape(0xDC00 + (v & 0x3FF), out);
262                } else {
263                    push_unicode_escape(cp, out);
264                }
265            }
266        }
267    }
268    out.push('"');
269}
270
271fn push_unicode_escape(cp: u32, out: &mut String) {
272    use std::fmt::Write as _;
273    // write! on a String is infallible.
274    let _ = write!(out, "\\u{cp:04x}");
275}
276
277#[cfg(test)]
278mod tests {
279    use super::*;
280    use serde_json::json;
281
282    fn enc(v: Value) -> String {
283        php_json_encode(&v).expect("encodable")
284    }
285
286    #[test]
287    fn compact_object_preserves_order() {
288        let v: Value = serde_json::from_str(r#"{"b":1,"a":{"z":true,"y":null}}"#).unwrap();
289        assert_eq!(enc(v), r#"{"b":1,"a":{"z":true,"y":null}}"#);
290    }
291
292    #[test]
293    fn slash_and_unicode_are_escaped() {
294        assert_eq!(enc(json!("a/b")), r#""a\/b""#);
295        assert_eq!(enc(json!("héhé")), "\"h\\u00e9h\\u00e9\"");
296        assert_eq!(enc(json!("🎼")), "\"\\ud83c\\udfbc\"");
297        assert_eq!(enc(json!("tab\tok")), "\"tab\\tok\"");
298        assert_eq!(enc(json!("\u{1}")), "\"\\u0001\"");
299    }
300
301    #[test]
302    fn empty_object_becomes_array() {
303        let v: Value = serde_json::from_str(r#"{"extra":{}}"#).unwrap();
304        assert_eq!(enc(v), r#"{"extra":[]}"#);
305    }
306
307    #[test]
308    fn sequential_numeric_keys_become_array() {
309        let v: Value = serde_json::from_str(r#"{"0":"a","1":"b"}"#).unwrap();
310        assert_eq!(enc(v), r#"["a","b"]"#);
311        // Non-sequential order -> stays an object.
312        let v: Value = serde_json::from_str(r#"{"1":"a","0":"b"}"#).unwrap();
313        assert_eq!(enc(v), r#"{"1":"a","0":"b"}"#);
314        // Gap in the indices -> stays an object.
315        let v: Value = serde_json::from_str(r#"{"0":"a","2":"b"}"#).unwrap();
316        assert_eq!(enc(v), r#"{"0":"a","2":"b"}"#);
317    }
318
319    #[test]
320    fn numbers_round_trip() {
321        assert_eq!(enc(json!(42)), "42");
322        assert_eq!(enc(json!(-7)), "-7");
323        assert_eq!(enc(json!(1.5)), "1.5");
324        // Empirical PHP boundaries (see encode_double + oracle tests).
325        assert_eq!(enc(json!(1.0)), "1");
326        assert_eq!(enc(json!(1.0e-7)), "1.0e-7");
327        assert_eq!(enc(json!(0.0001)), "0.0001");
328        assert_eq!(enc(json!(1.0e-5)), "1.0e-5");
329        assert_eq!(enc(json!(9.9e16)), "99000000000000000");
330        assert_eq!(enc(json!(1.0e17)), "1.0e+17");
331        assert_eq!(enc(json!(1.23e17)), "1.23e+17");
332        assert_eq!(enc(json!(-0.0)), "-0");
333        assert_eq!(enc(json!(5e-324)), "5.0e-324");
334        assert_eq!(
335            enc(json!(1.7976931348623157e308)),
336            "1.7976931348623157e+308"
337        );
338        assert_eq!(
339            enc(json!(12345678901234567890_u64)),
340            "1.2345678901234567e+19"
341        );
342    }
343}