Crates.io | serde_structuredqs |
lib.rs | serde_structuredqs |
version | 0.2.0 |
source | src |
created_at | 2023-06-11 02:14:43.37368 |
updated_at | 2023-06-24 08:43:53.009602 |
description | A Rust library for serialize/deserialize structured query-string. |
homepage | |
repository | https://github.com/fjnkt98/serde_structuredqs |
max_upload_size | |
id | 887173 |
size | 92,141 |
This crate is a Rust library for serialize/deserialize structured query-string. This crate was strongly inspired by serde_urlencoded and serde_qs.
Serialize nested struct with period-delimited keys.
use serde::Serialize;
#[derive(Serialize)]
struct Parent {
key1: String,
key2: Option<String>,
child: Child,
}
#[derive(Serialize)]
struct Child {
key3: i32,
key4: Option<i32>,
}
assert_eq!(
serde_structuredqs::to_string(&Parent {
key1: String::from("foo"),
key2: Some(String::from("ほげ")),
child: Child {
key3: 100,
key4: None,
}
})
.unwrap(),
String::from("key1=foo&key2=%E3%81%BB%E3%81%92&child.key3=100")
)
And deserialize query-string with period-delimited keys into nested struct.
use serde::Deserialize;
#[derive(Debug, Deserialize, Eq, PartialEq)]
struct Parent {
key1: String,
key2: Option<String>,
child: Child,
}
#[derive(Debug, Deserialize, Eq, PartialEq)]
struct Child {
key3: i32,
key4: Option<i32>,
}
assert_eq!(
serde_structuredqs::from_str::<Parent>(
"key1=foo&key2=%E3%81%BB%E3%81%92&child.key3=100"
)
.unwrap(),
Parent {
key1: String::from("foo"),
key2: Some(String::from("ほげ")),
child: Child {
key3: 100,
key4: None,
}
}
)
The value of a field of type Vec
is serialized as a comma-separated string.
use serde::Serialize;
#[derive(Serialize)]
struct MyStruct {
key: Vec<i32>,
}
assert_eq!(
serde_structuredqs::to_string(&MyStruct {
key: vec![100, 200],
}).unwrap(),
String::from("key=100%2C200")
)
Similarly, comma-separated values are deserialized as Vec
.
use serde::Deserialize;
#[derive(Debug, Deserialize, Eq, PartialEq)]
struct MyStruct {
key: Vec<String>,
}
assert_eq!(
serde_structuredqs::from_str::<MyStruct>(
"key=foo%2Cbar%2C%E3%81%BB%E3%81%92%2C%E3%81%B5%E3%81%8C"
)
.unwrap(),
MyStruct {
key: vec![
String::from("foo"),
String::from("bar"),
String::from("ほげ"),
String::from("ふが"),
]
}
)