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

adds initial support for json literals #523

Merged
merged 1 commit into from
May 31, 2020

Conversation

jbr
Copy link
Member

@jbr jbr commented May 21, 2020

No description provided.

fn from(json_value: serde_json::Value) -> Self {
Response::new(StatusCode::Ok)
.body_json(&json_value)
.unwrap_or_else(|_| Response::new(StatusCode::InternalServerError))
Copy link
Member Author

@jbr jbr May 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure if this is reachable, since a serde_json::Value should basically be the safest thing to turn into json, as the intermediate representation before stringification

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably use the Status trait here instead so we don't lose the original error message.

Copy link
Member Author

@jbr jbr May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as returning status: From<serde_json::Value> for Response needs to return a Response, not a tide::Error. I can imagine the value of adding an Option<tide::Error> on Response (or using ext in a systematic way) and adding a TryInto or downcast, but that's way beyond the scope of this PR. If people want to retain the original error message from transforming a json literal into a Response, they'll need to explicitly do the body_json call for now

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

src/response.rs Show resolved Hide resolved
@@ -1,2 +1,3 @@
//! The Tide prelude.
pub use http_types::Status;
pub use serde_json::json;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to make serde_json a feature dependency, even if enabled by default

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected to need to do that when starting this pr, but discovered it was already a normal dependency. We'd probably want to gate json_body on this as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that would be a good improvement, given that serde_json is not the lightest dependency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a "json" feature, enabled by default, and gated everything json-related in tide on it. Probably could stand to do the same for http-types as well

@jbr jbr changed the title initial support for json literals adds initial support for json literals, adds "json" feature May 24, 2020
@yoshuawuyts
Copy link
Member

@jbr it seems tests are still failing; I'd love to get these to pass so we can merge this PR.

@jbr
Copy link
Member Author

jbr commented May 31, 2020

@yoshuawuyts Fixed!

@jbr
Copy link
Member Author

jbr commented May 31, 2020

Wait actually: I was just digging in on adding a similar json feature for http-types, but ran into this issue: This header takes a json value, which means http-types csp stuff needs serde_json, which means tide always is going to have a serde_json dependency, in which case is there any reason to add the feature flag?

@yoshuawuyts
Copy link
Member

Heh, yeah probably always supporting JSON is fine. Serde even works on embedded systems, so don't think there's really any practical reason to make this opt-out.

@jbr
Copy link
Member Author

jbr commented May 31, 2020

I'll back out the feature-gate parts of this pr

@yoshuawuyts yoshuawuyts merged commit c7a9d50 into http-rs:master May 31, 2020
@jbr jbr changed the title adds initial support for json literals, adds "json" feature adds initial support for json literals May 31, 2020
@yoshuawuyts yoshuawuyts mentioned this pull request Jun 1, 2020
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

Successfully merging this pull request may close these issues.

4 participants