aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-16 12:51:24 +0900
committernsfisis <nsfisis@gmail.com>2026-08-16 12:51:24 +0900
commitfce44d4587ba11b080fe0266c483d0be684ffd0d (patch)
tree753e87ac7ed2e57d635a60c10a4ce01b7e0f0f8e /crates/shirabe-php-shim
parentb12c2b15f0487d54d359a581e99ced68713914d0 (diff)
downloadphp-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.tar.gz
php-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.tar.zst
php-shirabe-fce44d4587ba11b080fe0266c483d0be684ffd0d.zip
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim')
-rw-r--r--crates/shirabe-php-shim/src/json.rs18
1 files changed, 14 insertions, 4 deletions
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<T: serde::Serialize + ?Sized>(
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<PhpMixed> {
+// 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<PhpMixed> {
+ 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<PhpMixed> {
+ json_decode(s, false)
+}
+
+fn json_decode(s: &str, assoc: bool) -> anyhow::Result<PhpMixed> {
match serde_json::from_str::<serde_json::Value>(s) {
Ok(value) => Ok(json_value_to_php_mixed(value, assoc)),
Err(_) => Ok(PhpMixed::Null),