You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 (specificallyx-www-form-urlencoded
). It would be quite handy if there was something likeJSON::Serialziable
, but for form data, sayURI::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
onObject
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 pollutingObject
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, andURI::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.The text was updated successfully, but these errors were encountered: