summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorWalther Chen <walther.chen@gmail.com>2018-08-04 11:08:11 -0400
committerWalther Chen <walther.chen@gmail.com>2018-08-04 11:08:11 -0400
commit902fb6c3d625e12023a72498475574413a6e620a (patch)
treeb6d55744eb404374c965757eb61d1c4e0197aee1 /examples
parent5696a51e46649997ca346089e916b489a348386d (diff)
use non-strict parsing to allow round-trip in example
Diffstat (limited to 'examples')
-rw-r--r--examples/introduction.rs10
1 files changed, 9 insertions, 1 deletions
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);