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

Serialize Vec<T> #6

Closed
nbigaouette opened this issue Nov 29, 2016 · 16 comments
Closed

Serialize Vec<T> #6

nbigaouette opened this issue Nov 29, 2016 · 16 comments

Comments

@nbigaouette
Copy link

I need to serialize using serde_urlencoded::to_string() a Vec<i64> into name[]=val1&name[]=val2&....

It does not seems possible serde_urlencoded... or am I missing something?

@nox
Copy link
Owner

nox commented Dec 1, 2016

It's not currently possible, I've wished to implement it for a while now, will probably look at it next week.

nbigaouette added a commit to nbigaouette/gitlab-api-rs that referenced this issue Dec 4, 2016
@nbigaouette
Copy link
Author

To make things more "interesting", I would also need the Vec to be serialized to name=val1,val2,val3,... Would that be possible too?

@nox
Copy link
Owner

nox commented Dec 7, 2016

I won't support the comma separated thing, but the repeated key name yes.

@samscott89
Copy link

Hey @nbigaouette, I've created serde_qs to try and fill this gap, since I think the extra functionality needed would be unnecessarily cumbersome to this library.

Let me know if it's suitable for your use case. I showed in the examples how you could also support the comma-separated vector lists (though actually the same strategy would presumably work fine here as well).

@nox nox closed this as completed Jan 8, 2018
@antoine-de
Copy link

Just to be sure to understand, you don't plan to support the repeated key name ? You suggest serde_qs for this too ?

@nox
Copy link
Owner

nox commented Jan 23, 2019

Yes, anything that requires temporary allocation won't be added to this crate.

@TeXitoi
Copy link

TeXitoi commented Jan 23, 2019

With @antoine-de we don't really understand your temporary allocation.

What we need is:

#[derive(Deserialize, PartialEq)]
struct Params {
    foo: Vec<i32>,
}

assert_eq!(
    serde_urlencoded::from_bytes(b"foo=23&foo=42"),
    Ok(Params { foo: vec![23, 42] })
);

If it's OK for you, we can implement it.

@nox
Copy link
Owner

nox commented Jan 23, 2019

#[derive(Deserialize)]
struct Params {
    foo: Vec<i32>,
    bar: String,
}

serde_urlencoded::from_bytes::<Params>(b"foo=23&bar=baguette&foo=42")

Handling sequences mean traversing the entire string to get all the pairs with the same key, and only then you can deserialise that to a Vec<i32>, I won't accept any such patch in this crate, but AFAIK this kind of thing isn't out of scope for serde_qs.

@samscott89
Copy link

Handling sequences mean traversing the entire string to get all the pairs with the same key, and only then you can deserialise that to a Vec, I won't accept any such patch in this crate, but AFAIK this kind of thing isn't out of scope for serde_qs.

Yeah, this is true to the extent of my understanding. The performance of serde_qs is significantly worse than serde_urlencoded due to this, so keeping the separation seems like a good idea to me.

And yes, serde_qs can handle most structs. In the case of vecs, you can either use indexed arrays foo[0]=...&foo[1]=... or unindexed, foo[]=...&foo[]=....

@antoine-de
Copy link

ok great @samscott89 , I'll see how to plug serde_qs in actix then.

thanks for your replies @nox , sorry for having bothered you.

@lnicola
Copy link

lnicola commented Jun 19, 2019

@samscott89 is serde_qs supposed to handle percent-encoded brackets? s[]=1+2&s[]=2 seems to work, but s%5B%5D=1+2&s%5B%5D=2 doesn't. If I'm not missing anything, that's what browsers send for forms.

@nox serde_urlencoded advertises support for application/x-www-form-urlencoded, but this issue makes that feel misleading.

@nox
Copy link
Owner

nox commented Jun 19, 2019

How is that misleading? application/x-www-form-urlencoded specifically doesn't have any concept of nesting or even serialising a sequence of values. This crate (and url) does handle percent-encoded brackets, as the WHATWG URL Standard says to do it.

@lnicola
Copy link

lnicola commented Jun 19, 2019

Misleading for someone who wants to use it for something that looks like an array sent by a web browser :-).

@nox
Copy link
Owner

nox commented Jun 19, 2019

There is no standard for that and not every backend tooling handle such things with brackets. For example some Perl libraries just deserialise multiple entries with the same key as an array, which is why I decided to not support this feature in this crate directly.

@antoine-de
Copy link

@lnicola serde_qs handles this, but in non strict mode. I have an example here.

@samscott89
Copy link

For the record, I agree with @nox :)

An alternative potential implementation to unify the various options would follow the swagger/openapi spec: https://swagger.io/docs/specification/serialization/#query

The serde_qs approach roughly corresponds to "deepObject" style for example.

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

6 participants