From cc1f19ab227a1e38ac1c7cee710936adf1723168 Mon Sep 17 00:00:00 2001 From: Spencer Thomason Date: Thu, 2 Jul 2026 13:01:05 -0700 Subject: [PATCH] feat: add OwnedLazyValue::as_raw_str() method Matches LazyValue::as_raw_str(). Returns Option because OwnedLazyValue can be in a Parsed state with no raw text. Enables zero-copy deserialization from OwnedLazyValue. Fixes #194 --- src/lazyvalue/owned.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lazyvalue/owned.rs b/src/lazyvalue/owned.rs index ec2378d9..8720f9f9 100644 --- a/src/lazyvalue/owned.rs +++ b/src/lazyvalue/owned.rs @@ -72,6 +72,28 @@ impl Default for OwnedLazyValue { } impl OwnedLazyValue { + /// Return the raw JSON text if this value is still in its unparsed (lazy) + /// form. Returns `None` if the value has already been fully parsed into a + /// concrete representation. + /// + /// This is the owned counterpart to [`LazyValue::as_raw_str`]. + /// + /// # Examples + /// + /// ``` + /// use sonic_rs::{get, OwnedLazyValue}; + /// + /// let input = r#"{"a": [1, 2, 3]}"#; + /// let own = OwnedLazyValue::from(get(input, &["a"]).unwrap()); + /// assert_eq!(own.as_raw_str(), Some("[1, 2, 3]")); + /// ``` + pub fn as_raw_str(&self) -> Option<&str> { + match &self.0 { + LazyPacked::Raw(raw) => Some(raw.raw.as_str()), + _ => None, + } + } + pub(crate) fn from_non_esc_str(raw: FastStr) -> Self { Self(LazyPacked::NonEscStrRaw(raw)) }