samscott89 / serde_qs

Serde support for querystring-style strings
Apache License 2.0
193 stars 68 forks source link

Some people use comma to join array, qs can parse it: #61

Open lizelive opened 2 years ago

lizelive commented 2 years ago

qs docs state that

Some people use comma to join array, qs can parse it:

var arraysOfObjects = qs.parse('a=b,c', { comma: true })
assert.deepEqual(arraysOfObjects, { a: ['b', 'c'] })

this would be much more readable

You may use the arrayFormat option to specify the format of the output array:

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
// 'a[0]=b&a[1]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
// 'a[]=b&a[]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
// 'a=b&a=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'
samscott89 commented 2 years ago

Hey @lizelive ! Thanks for the suggestion.

I would consider accepting a PR that adds this as a configuration mode, but it's unlikely that I'll be able to get to adding this feature myself in the near future.

We do have an existing example that should help achieve the same functionality: https://github.com/samscott89/serde_qs/blob/main/examples/csv_vectors.rs

omid commented 2 years ago

@lizelive any suggestion/idea how the API should look like?

And specially when we want to integrate it in http frameworks like actix:

#[derive(Deserialize)]
pub struct UsersFilter {
   id: Vec<u64>,
}

// Use `QsQuery` extractor for query information.
// The correct request for this handler would be `/users?id[]=1124&id[]=88"`
async fn filter_users(info: QsQuery<UsersFilter>) -> HttpResponse {
    HttpResponse::Ok().body(
        info.id.iter().map(|i| i.to_string()).collect::<Vec<String>>().join(", ")
    )
}
fn main() {
    let app = App::new().service(
       web::resource("/users")
           .route(web::get().to(filter_users)));
}