diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-16 12:59:46 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-16 12:59:46 +0900 |
| commit | baf9aff3134ac5a10260d3be421a2c17a0180d64 (patch) | |
| tree | 6040df4de5932e46b14cbefa2c239de2756caf23 /crates/shirabe-php-shim | |
| parent | fce44d4587ba11b080fe0266c483d0be684ffd0d (diff) | |
| download | php-shirabe-baf9aff3134ac5a10260d3be421a2c17a0180d64.tar.gz php-shirabe-baf9aff3134ac5a10260d3be421a2c17a0180d64.tar.zst php-shirabe-baf9aff3134ac5a10260d3be421a2c17a0180d64.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim')
| -rw-r--r-- | crates/shirabe-php-shim/src/json.rs | 119 |
1 files 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<PhpMixed> { } 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), + 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<String, PhpMixed> = 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<D>(self, deserializer: D) -> Result<PhpMixed, D::Error> + 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<E>(self) -> Result<PhpMixed, E> { + Ok(PhpMixed::Null) + } + + fn visit_bool<E>(self, v: bool) -> Result<PhpMixed, E> { + Ok(PhpMixed::Bool(v)) + } + + fn visit_i64<E>(self, v: i64) -> Result<PhpMixed, E> { + 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<E>(self, v: u64) -> Result<PhpMixed, E> { + Ok(match i64::try_from(v) { + Ok(i) => PhpMixed::Int(i), + Err(_) => PhpMixed::Float(v as f64), + }) + } + + fn visit_f64<E>(self, v: f64) -> Result<PhpMixed, E> { + Ok(PhpMixed::Float(v)) + } + + fn visit_str<E>(self, v: &str) -> Result<PhpMixed, E> { + Ok(PhpMixed::String(v.to_owned())) + } + + fn visit_string<E>(self, v: String) -> Result<PhpMixed, E> { + Ok(PhpMixed::String(v)) + } + + fn visit_seq<A>(self, mut seq: A) -> Result<PhpMixed, A::Error> + 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<A>(self, mut map: A) -> Result<PhpMixed, A::Error> + where + A: MapAccess<'de>, + { + let mut data = IndexMap::new(); + while let Some(key) = map.next_key::<String>()? { + // 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) + }) } } |
