Skip to content

Conversation

@matthiasbeyer
Copy link

@matthiasbeyer matthiasbeyer commented Sep 30, 2025

Following #4793 (comment), this adds documentation to the sql_types::Json type 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:

  • Make sure the code example works
  • Document the serialize_as part
  • Document how to use the json() function in sqlite to get the desired behaviour out of SQLite (link to the approriate SQLite docs)
  • Clearify that the diesel implementation uses a Into::into() approach for converting from (for example) serde_json::Value to your custom type when using deserialize_as rather than TryFrom, which might be unexpected (Question for you: Any reason why this is From/Into rather than TryFrom?)

CC @weiznich

@weiznich weiznich requested a review from a team September 30, 2025 18:14
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

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)]
Copy link
Member

Choose a reason for hiding this comment

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

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);
Copy link
Member

Choose a reason for hiding this comment

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

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)))
Copy link
Member

Choose a reason for hiding this comment

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

For this variant you need to directly convert the address to a serde_json::Value as there is no attribute that's applied.

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.

2 participants