Skip to content

Commit

Permalink
docs: added docs for json (#1917)
Browse files Browse the repository at this point in the history
* fix: added docs for json

* fix: doc tests
  • Loading branch information
jayy-lmao authored Jul 1, 2022
1 parent bd40cc9 commit 79ebd30
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions sqlx-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,47 @@ use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::types::Type;

/// Json for json and jsonb fields
///
/// Will attempt to cast to type passed in as the generic.
///
/// ```toml
/// [dependencies]
/// serde_json = { version = "1.0", features = ["raw_value"] }
///
/// ```
///
/// # Example
///
/// ```
/// # use serde::Deserialize;
/// #[derive(Deserialize)]
/// struct Book {
/// name: String
/// }
///
/// #[derive(sqlx::FromRow)]
/// struct Author {
/// name: String,
/// books: sqlx::types::Json<Book>
/// }
/// ```
///
/// Can also be used to turn the json/jsonb into a hashmap
/// ```
/// use std::collections::HashMap;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Book {
/// name: String
/// }
/// #[derive(sqlx::FromRow)]
/// struct Library {
/// id: String,
/// dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
/// }
/// ```
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
Expand Down

0 comments on commit 79ebd30

Please sign in to comment.