serde_qs icon indicating copy to clipboard operation
serde_qs copied to clipboard

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

Open lizelive opened this issue 3 years ago • 2 comments

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'

lizelive avatar May 07 '22 20:05 lizelive

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

samscott89 avatar May 11 '22 16:05 samscott89

@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)));
}

omid avatar Jul 11 '22 08:07 omid