We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
To Reproduce
use std::str::FromStr; struct PriceAmount(f64, f64); impl<'de> serde::de::Deserialize<'de> for PriceAmount { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::de::Deserializer<'de>, { struct PriceAmountVisitor; impl<'de> serde::de::Visitor<'de> for PriceAmountVisitor { type Value = PriceAmount; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("an array of two stringified floats") } fn visit_seq<A>(self, mut seq: A) -> Result<PriceAmount, A::Error> where A: serde::de::SeqAccess<'de>, { let price_str = seq .next_element::<String>()? .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?; let price = f64::from_str(&price_str).map_err(serde::de::Error::custom)?; let amount_str = seq .next_element::<String>()? .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?; let amount = f64::from_str(&amount_str).map_err(serde::de::Error::custom)?; Ok(PriceAmount(price, amount)) } } deserializer.deserialize_seq(PriceAmountVisitor) } } fn de_vec_str_f64_f64<'de, D>(deserializer: D) -> Result<Vec<(f64, f64)>, D::Error> where D: serde::de::Deserializer<'de>, { struct VecTupleVisitor; impl<'de> serde::de::Visitor<'de> for VecTupleVisitor { type Value = Vec<(f64, f64)>; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("a JSON array of arrays with two stringified floats each") } fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error> where S: serde::de::SeqAccess<'de>, { let mut vec = Vec::new(); while let Some(price_amount) = seq.next_element::<PriceAmount>()? { vec.push((price_amount.0, price_amount.1)); } Ok(vec) } } deserializer.deserialize_seq(VecTupleVisitor) } #[test] fn test_de_vec_str_f64_f64() { #[derive(serde::Deserialize)] struct OrderBookTest { #[serde(deserialize_with = "de_vec_str_f64_f64")] asks: Vec<(f64, f64)>, } let order_book_str = r#"{"asks":[["1.2", "100"], ["1.3", "150"], ["1.4", "200"]]}"#; let order_book: OrderBookTest = serde_json::from_str(order_book_str).unwrap(); assert_eq!(order_book.asks[0].0, 1.2); let _order_book: OrderBookTest = sonic_rs::from_str(order_book_str).unwrap(); }
Expected behavior
json parse ok like serde_json
current behaviour
called `Result::unwrap()` on an `Err` value: Error(Expected this character to be '"' or '}', line: 1, column: 25) "100"], ["1.3", ........^.......
sonic-rs version:
0.3.4
Environment:
Ubuntu 20.04
1.77.0-x86_64-unknown-linux-gnu
The text was updated successfully, but these errors were encountered:
Thanks, I reproduced it. I will investigate it
Sorry, something went wrong.
The problem is : When visit_seq does not use next_element until None, the closed brace ] will not be consumed.
visit_seq
next_element
None
]
It is a bug and should be fixed in the library, I will fix it.
I add below code to PriceAmount deserialize, and deserialize is ok
while seq.next_element::<String>().is_ok() { // consume buffer continue; }
Successfully merging a pull request may close this issue.
To Reproduce
Expected behavior
json parse ok like serde_json
current behaviour
sonic-rs version:
0.3.4
Environment:
Ubuntu 20.04
1.77.0-x86_64-unknown-linux-gnu
The text was updated successfully, but these errors were encountered: