Skip to content
New issue

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

deserialize string stringified array Vec<(f64,f64)> fail on sonic_rs while serde_json deserialize ok #73

Closed
pymongo opened this issue Apr 17, 2024 · 3 comments · Fixed by #76
Labels
C-bug This is a bug-report. Bug-fix PRs use `C-enhancement` instead.

Comments

@pymongo
Copy link
Contributor

pymongo commented Apr 17, 2024

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

@liuq19
Copy link
Collaborator

liuq19 commented Apr 17, 2024

Thanks, I reproduced it. I will investigate it

@liuq19
Copy link
Collaborator

liuq19 commented Apr 17, 2024

The problem is : When visit_seq does not use next_element until None, the closed brace ] will not be consumed.

It is a bug and should be fixed in the library, I will fix it.

@liuq19 liuq19 added the C-bug This is a bug-report. Bug-fix PRs use `C-enhancement` instead. label Apr 17, 2024
@pymongo
Copy link
Contributor Author

pymongo commented Apr 17, 2024

I add below code to PriceAmount deserialize, and deserialize is ok

while seq.next_element::<String>().is_ok() {
    // consume buffer
    continue;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug This is a bug-report. Bug-fix PRs use `C-enhancement` instead.
Development

Successfully merging a pull request may close this issue.

2 participants