From fce44d4587ba11b080fe0266c483d0be684ffd0d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 12:51:24 +0900 Subject: refactor(php-shim): split json_decode into assoc and obj variants The assoc flag was always a literal at every call site, so the boolean carried no information the function name could not. json_decode_assoc and json_decode_obj make the resulting PhpMixed shape visible at the call site. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/json.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'crates/shirabe-php-shim') diff --git a/crates/shirabe-php-shim/src/json.rs b/crates/shirabe-php-shim/src/json.rs index a3bed2c0..b899d548 100644 --- a/crates/shirabe-php-shim/src/json.rs +++ b/crates/shirabe-php-shim/src/json.rs @@ -64,10 +64,20 @@ pub fn json_encode_ex( Ok(s) } -// PHP's two-argument `json_decode`: without JSON_THROW_ON_ERROR it never throws, -// returning null on malformed input. With `assoc` false, JSON objects decode to -// stdClass-equivalent `PhpMixed::Object` values; with `assoc` true, to associative arrays. -pub fn json_decode(s: &str, assoc: bool) -> anyhow::Result { +// PHP's `json_decode($s, true)`: JSON objects decode to associative arrays. Without +// JSON_THROW_ON_ERROR it never throws, returning null on malformed input. +pub fn json_decode_assoc(s: &str) -> anyhow::Result { + json_decode(s, true) +} + +// PHP's `json_decode($s, false)`: JSON objects decode to stdClass-equivalent +// `PhpMixed::Object` values. Without JSON_THROW_ON_ERROR it never throws, returning null on +// malformed input. +pub fn json_decode_obj(s: &str) -> anyhow::Result { + json_decode(s, false) +} + +fn json_decode(s: &str, assoc: bool) -> anyhow::Result { match serde_json::from_str::(s) { Ok(value) => Ok(json_value_to_php_mixed(value, assoc)), Err(_) => Ok(PhpMixed::Null), -- cgit v1.3.1-4-g156e