sql_types::Json: Add documentation how to de/serialize to custom type#4798
Draft
matthiasbeyer wants to merge 2 commits intodiesel-rs:mainfrom
Draft
sql_types::Json: Add documentation how to de/serialize to custom type#4798matthiasbeyer wants to merge 2 commits intodiesel-rs:mainfrom
matthiasbeyer wants to merge 2 commits intodiesel-rs:mainfrom
Conversation
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
weiznich
reviewed
Oct 2, 2025
Member
weiznich
left a comment
There was a problem hiding this comment.
Thanks for opening this PR.
I think that's a good location to document these things and it's also a good idea to include examples. I left a few remarks on what's potentially broken and causes the CI to fail.
| /// struct Contact { | ||
| /// pub id: i32, | ||
| /// pub name: String, | ||
| /// #[diesel(deserialize_as = serde_json::Value)] |
Member
There was a problem hiding this comment.
This requires an impl TryFrom<serde_json::Value> for Address
| /// # .get_result::<serde_json::Value>(connection)?; | ||
| /// | ||
| /// let santa = contacts.filter(id.eq(1)).first(connection)?; | ||
| /// assert_eq!(santas_address, santda.address); |
Member
There was a problem hiding this comment.
Suggested change
| /// assert_eq!(santas_address, santda.address); | |
| /// assert_eq!(santas_address, santa.address); |
that's likely a typo
| /// }; | ||
| /// | ||
| /// #let inserted_address = insert_into(contacts) | ||
| /// # .values((name.eq("Claus"), address.eq(&santas_address))) |
Member
There was a problem hiding this comment.
For this variant you need to directly convert the address to a serde_json::Value as there is no attribute that's applied.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following #4793 (comment), this adds documentation to the
sql_types::Jsontype on how to de/serialize from/to custom types with JSON.Right now this is in draft mode, to get some feedback whether I added the docs in the right location (I believe so, because that's where I'd expect them) and whether the idea of having a code example at that location was the right way of doing this.
What is left to do here:
serialize_aspartjson()function in sqlite to get the desired behaviour out of SQLite (link to the approriate SQLite docs)Into::into()approach for converting from (for example)serde_json::Valueto your custom type when usingdeserialize_asrather thanTryFrom, which might be unexpected (Question for you: Any reason why this isFrom/Intorather thanTryFrom?)CC @weiznich