From 9e87e64db452739d7f74dc6c4fc0b174ebda3cc2 Mon Sep 17 00:00:00 2001 From: "liuqiang.06" Date: Tue, 10 Dec 2024 22:22:40 +0800 Subject: [PATCH] fix: lazyvalue serialize --- src/lazyvalue/owned.rs | 21 ++++++++++++++++----- src/lazyvalue/ser.rs | 24 ++++++++++++++++++------ src/lazyvalue/value.rs | 1 + 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/lazyvalue/owned.rs b/src/lazyvalue/owned.rs index 1a51abe..78a53e4 100644 --- a/src/lazyvalue/owned.rs +++ b/src/lazyvalue/owned.rs @@ -476,11 +476,22 @@ impl serde::ser::Serialize for OwnedLazyValue { { match &self.0 { LazyPacked::Raw(raw) => { - let raw = raw.raw.as_str(); - let mut s = serializer.serialize_struct(super::TOKEN, 1)?; - // will directly write raw in `LazyValueStrEmitter::seriazlie_str` - s.serialize_field(super::TOKEN, raw)?; - s.end() + if raw.has_trailings { + let lv = crate::get(raw.raw.as_str(), crate::pointer![]).map_err(|e| { + serde::ser::Error::custom(format!("get raw failed: {}", e)) + })?; + let raw = lv.as_raw_str(); + let mut s = serializer.serialize_struct(super::TOKEN, 1)?; + // will directly write raw in `LazyValueStrEmitter::seriazlie_str` + s.serialize_field(super::TOKEN, raw)?; + s.end() + } else { + let raw = raw.raw.as_str(); + let mut s = serializer.serialize_struct(super::TOKEN, 1)?; + // will directly write raw in `LazyValueStrEmitter::seriazlie_str` + s.serialize_field(super::TOKEN, raw)?; + s.end() + } } LazyPacked::NonEscStrRaw(raw) => { let raw = raw.as_str(); diff --git a/src/lazyvalue/ser.rs b/src/lazyvalue/ser.rs index 5961624..201ac6b 100644 --- a/src/lazyvalue/ser.rs +++ b/src/lazyvalue/ser.rs @@ -1,17 +1,29 @@ use ::serde::ser::SerializeStruct; -use super::{owned::OwnedLazyValue, value::LazyValue}; +use super::{value::LazyValue}; impl<'a> serde::ser::Serialize for LazyValue<'a> { fn serialize(&self, serializer: S) -> std::result::Result where S: serde::Serializer, { - let raw = self.as_raw_str(); - let mut s = serializer.serialize_struct(super::TOKEN, 1)?; - // will directly write raw in `LazyValueStrEmitter::seriazlie_str` - s.serialize_field(super::TOKEN, raw)?; - s.end() + if self.has_trailings { + let raw = crate::get(self.as_raw_str(), crate::pointer![]).map_err(|e| { + serde::ser::Error::custom(format!("get raw failed: {}", e)) + })?; + let raw = raw.as_raw_str(); + let mut s = serializer.serialize_struct(super::TOKEN, 1)?; + // will directly write raw in `LazyValueStrEmitter::seriazlie_str` + s.serialize_field(super::TOKEN, raw)?; + s.end() + + } else { + let raw = self.as_raw_str(); + let mut s = serializer.serialize_struct(super::TOKEN, 1)?; + // will directly write raw in `LazyValueStrEmitter::seriazlie_str` + s.serialize_field(super::TOKEN, raw)?; + s.end() + } } } diff --git a/src/lazyvalue/value.rs b/src/lazyvalue/value.rs index e47326b..5248679 100644 --- a/src/lazyvalue/value.rs +++ b/src/lazyvalue/value.rs @@ -199,6 +199,7 @@ impl Default for LazyValue<'_> { Self { raw: JsonSlice::Raw(&b"null"[..]), inner: Inner::default(), + has_trailings: false, } } }