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

[RFC] Add URI::Params::Serializable #14680

Open
Blacksmoke16 opened this issue Jun 9, 2024 · 0 comments · May be fixed by #14684
Open

[RFC] Add URI::Params::Serializable #14680

Blacksmoke16 opened this issue Jun 9, 2024 · 0 comments · May be fixed by #14684

Comments

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Jun 9, 2024

Feature Request

In Crystal it's usually considered a best practice to use proper objects when the structure of the data is known as opposed to raw hashes or JSON::Any. However one area where this is lacking is when working with form data (specifically x-www-form-urlencoded). It would be quite handy if there was something like JSON::Serialziable, but for form data, say URI::Params::Serializable. So I wanted to start a discussion around adding this feature.

Ideally this could be included into a type to allow deserializing a URI::Params into that type. It would also be able to serialize that object into a form data string. With some considerations:

Form data can be nested, however URI::Params.encode does not allow for this, so would either have to save this for a future iteration if it's not too common, add support for it in .encode, or manually handle it within #to_form_data or whatever we want to call it. In the other direction,

I also think it feels a bit weird to have a .from_form_data on Object like we do for JSON/YAML, so maybe it would be sufficient to have this scoped just to the type it is included in, versus polluting Object as well.

The main use case for me would be in the context of a web framework where you could easily define some DTO object that includes this, then deserialize the query string or request body into like you can with JSON payloads.

We already have URI::Params to handle parsing the query string, and URI::Params::Builder to handle the generating side of things. So don't think it would be too difficult to implement. Would be happy to work up a prototype if this is something we'd think would be worth having.

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

Successfully merging a pull request may close this issue.

1 participant