From baf9aff3134ac5a10260d3be421a2c17a0180d64 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 12:59:46 +0900 Subject: refactor(php-shim): decode JSON straight into PhpMixed json_decode built a serde_json::Value first and then converted it. Drive serde_json's Deserializer with a DeserializeSeed instead, so the value is built in one pass without the intermediate representation. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/json.rs | 119 +++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 30 deletions(-) diff --git a/crates/shirabe-php-shim/src/json.rs b/crates/shirabe-php-shim/src/json.rs index b899d548..3f10c2a6 100644 --- a/crates/shirabe-php-shim/src/json.rs +++ b/crates/shirabe-php-shim/src/json.rs @@ -1,5 +1,7 @@ use crate::PhpMixed; use indexmap::IndexMap; +use serde::de::{DeserializeSeed, MapAccess, SeqAccess, Visitor}; +use std::fmt; pub trait JsonSerializable { fn json_serialize(&self) -> PhpMixed; @@ -78,39 +80,96 @@ pub fn json_decode_obj(s: &str) -> anyhow::Result { } 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), + let mut deserializer = serde_json::Deserializer::from_str(s); + let Ok(value) = PhpMixedSeed { assoc }.deserialize(&mut deserializer) else { + return Ok(PhpMixed::Null); + }; + if deserializer.end().is_err() { + return Ok(PhpMixed::Null); } + Ok(value) } -fn json_value_to_php_mixed(value: serde_json::Value, assoc: bool) -> PhpMixed { - match value { - serde_json::Value::Null => PhpMixed::Null, - serde_json::Value::Bool(b) => PhpMixed::Bool(b), - serde_json::Value::Number(n) => match n.as_i64() { - Some(i) => PhpMixed::Int(i), - // Integers beyond i64 and any fractional/exponent number decode to float, - // matching PHP's default (non-bigint) behaviour. - None => PhpMixed::Float(n.as_f64().unwrap_or(0.0)), - }, - serde_json::Value::String(s) => PhpMixed::String(s), - serde_json::Value::Array(items) => PhpMixed::List( - items - .into_iter() - .map(|item| json_value_to_php_mixed(item, assoc)) - .collect(), - ), - serde_json::Value::Object(entries) => { - let data: IndexMap = entries - .into_iter() - .map(|(k, v)| (k, json_value_to_php_mixed(v, assoc))) - .collect(); - if assoc { - PhpMixed::Array(data) - } else { - PhpMixed::Object(data) - } +#[derive(Clone, Copy, Debug)] +struct PhpMixedSeed { + assoc: bool, +} + +impl<'de> DeserializeSeed<'de> for PhpMixedSeed { + type Value = PhpMixed; + + fn deserialize(self, deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_any(self) + } +} + +impl<'de> Visitor<'de> for PhpMixedSeed { + type Value = PhpMixed; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a JSON value") + } + + fn visit_unit(self) -> Result { + Ok(PhpMixed::Null) + } + + fn visit_bool(self, v: bool) -> Result { + Ok(PhpMixed::Bool(v)) + } + + fn visit_i64(self, v: i64) -> Result { + Ok(PhpMixed::Int(v)) + } + + // Integers beyond i64 and any fractional/exponent number decode to float, matching PHP's + // default (non-bigint) behaviour. + fn visit_u64(self, v: u64) -> Result { + Ok(match i64::try_from(v) { + Ok(i) => PhpMixed::Int(i), + Err(_) => PhpMixed::Float(v as f64), + }) + } + + fn visit_f64(self, v: f64) -> Result { + Ok(PhpMixed::Float(v)) + } + + fn visit_str(self, v: &str) -> Result { + Ok(PhpMixed::String(v.to_owned())) + } + + fn visit_string(self, v: String) -> Result { + Ok(PhpMixed::String(v)) + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: SeqAccess<'de>, + { + let mut items = Vec::new(); + while let Some(item) = seq.next_element_seed(self)? { + items.push(item); + } + Ok(PhpMixed::List(items)) + } + + fn visit_map(self, mut map: A) -> Result + where + A: MapAccess<'de>, + { + let mut data = IndexMap::new(); + while let Some(key) = map.next_key::()? { + // A duplicate key keeps its original position and takes the later value, like PHP. + data.insert(key, map.next_value_seed(self)?); } + Ok(if self.assoc { + PhpMixed::Array(data) + } else { + PhpMixed::Object(data) + }) } } -- cgit v1.3.1-4-g156e