Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lizelive opened this issue May 7, 2022 · 2 comments
Open

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

lizelive opened this issue May 7, 2022 · 2 comments

Comments

@lizelive
Copy link

lizelive commented May 7, 2022

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
Copy link
Owner

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
Copy link
Contributor

omid commented Jul 11, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants