cloudwego / cloudwego/sonic-rs

Error when deserializing `Value` to `T`

Open
#165 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
920
Forks
68
Avg merge
2h 5m
Merged PRs (30d)
1

Description

Hello! I'm trying to write a helper function that deserializes Value to T and returns a default value if deserialization fails. I noticed that if I replace sonic_rs with serde_json, it's works.

Here is a example code:
```rust
pub fn deserialize_skip_error<'de, T, D>(deserializer: D) -> Result
where
T: serde::Deserialize<'de> + Default,
D: serde::Deserializer<'de>,
{
let value = sonic_rs::Value::deserialize(deserializer)?;
let inner = T::deserialize(value).unwrap_or_default();
Ok(inner)
}
```
And error logs here:
```log
the trait bound `sonic_rs::Value: extract_id::_::_serde::Deserializer<'_>` is not satisfied
the trait `extract_id::_::_serde::Deserializer<'_>` is not implemented for `sonic_rs::Value`
but it is implemented for `&sonic_rs::Value` for that trait implementation, expected `&sonic_rs::Value`, found `sonic_rs::Value`
```
Windows 11
rustc `1.86.0-nightly (bef3c3b01 2025-02-04)`

crates version:
sonic_rs: `main branch`
serde: `1.0.216`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.