-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add Json::erased constructor #509
Conversation
@davidpdrsn ignoring todos and broken doctest, wdyt about the general idea? |
I like it! |
4ad3b37
to
834676d
Compare
Docs could certainly still be better, but I'm not feeling very inspired. Are you okay with merging with the |
834676d
to
6aa85fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides the missing docs it looks good to me!
@@ -196,6 +221,39 @@ where | |||
} | |||
} | |||
|
|||
pub struct JsonSerializationError(serde_json::Error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we only warn for missing docs on CI. Should probably be an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. This type also needs docs 😅
Just realized that the |
Just thought of something else: As currently implemented, this makes An alternative way of implementing this, which also wouldn't expose that the How do you feel about that? |
And, another thing: Maybe there's a better name for this than |
I guess if there's a separate
|
I think thats okay. Realistically we're unlikely to move away from serde_json. So I don't we have to introduce some kind of
I agree its not great 🤔 Could we do impl<T> TryFrom<T> for Json<Box<serde_json::value::RawValue>>
where
T: Serialize,
{
type Error = JsonSerializationError;
fn try_from(value: T) -> Result<Self, Self::Error> {
// ...
}
} and sidestep the naming issue that way? |
I think |
Maybe we should release this as a tiny standalone crate 🤷 Then we can more easily |
Perhaps some sort of "axum-util" kinda crate could be good to have. Just as sort of a grab bag of things that seem nice but where we're not super confident about the API. This could go in there together with https://github.com/davidpdrsn/axum-resource. I also have an extractor like this in a project of mine: pub struct Cached<T>(pub T);
#[async_trait]
impl<B, T> FromRequest<B> for Cached<T>
where
B: Send,
T: FromRequest<B> + Clone + Send + Sync + 'static,
{
type Rejection = T::Rejection;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
if let Ok(Extension(value)) = Extension::from_request(req).await {
Ok(Self(value))
} else {
let value = T::from_request(req).await?;
req.extensions_mut().unwrap().insert(value.clone());
Ok(Self(value))
}
}
} This is useful when you have a tree of extractors that might contain duplicate parts. For example I have a I'm not sure this is super useful but would be nice to put it somewhere without too much commitment. |
I agree. Rocket had sth. like that too (rocket-contrib) until the things in there matured more and moved into their own crate. I'll close this PR and create a separate one for |
So I couldn't actually do the copy-free hack I wanted because
Box<RawValue>
can't be converted toBox<str>
withserde_json
s current API (though the impl would be easy to add there). The idea there way to use the castaway crate or implement the same logic manually, to "specialize" theBox<RawValue>
body in theIntoResponse
impl.Might still be worth having this.
To do:
serde_json::Error
in that implementsIntoResponse
? This would allow returningJson::Erased
from a handler, lettingIntoResponse
"handle" the error the same way as serialization errors inJson<OwnType>
are handled.