From 902fb6c3d625e12023a72498475574413a6e620a Mon Sep 17 00:00:00 2001 From: Walther Chen Date: Sat, 4 Aug 2018 11:08:11 -0400 Subject: use non-strict parsing to allow round-trip in example --- examples/introduction.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/introduction.rs b/examples/introduction.rs index d980719..9878968 100644 --- a/examples/introduction.rs +++ b/examples/introduction.rs @@ -7,6 +7,8 @@ extern crate serde_urlencoded as urlencoded; use rand::Rng; use std::collections::HashMap; +use qs::Config; + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] struct Address { city: String, @@ -64,9 +66,15 @@ fn main() { // as a list of pairs using serde_urlencoded: let pairs: Vec<(String, String)> = urlencoded::from_str(&encoded).unwrap(); println!("`serde_urlencoded` from_str to pairs:\n\t{:?}", pairs); + // However, the best way is to use serde_qs to deserialize the entire thing // into a struct: - let params: QueryParams = qs::from_str(&encoded).unwrap(); + // + // (For this round trip to work, it's necessary to parse the query string + // in non-strict mode, to allow parsing of url_encoded square brackets + // in the key). + let qs_non_strict = Config::new(5, false); + let params: QueryParams = qs_non_strict.deserialize_str(&encoded).unwrap(); assert_eq!(params, example_params); println!("`serde_qs` from_str to struct:\n\t{:?}", params); -- cgit v1.2.3