Skip to content

Commit

Permalink
fix: lazyvalue serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Dec 10, 2024
1 parent cce4311 commit 9e87e64
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/lazyvalue/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 18 additions & 6 deletions src/lazyvalue/ser.rs
Original file line number Diff line number Diff line change
@@ -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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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()
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lazyvalue/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl Default for LazyValue<'_> {
Self {
raw: JsonSlice::Raw(&b"null"[..]),
inner: Inner::default(),
has_trailings: false,
}
}
}
Expand Down

0 comments on commit 9e87e64

Please sign in to comment.