Skip to main content

SqlModelDump

Trait SqlModelDump 

Source
pub trait SqlModelDump: Model + Serialize {
    // Provided methods
    fn sql_model_dump(&self, options: DumpOptions) -> DumpResult { ... }
    fn sql_model_dump_json(&self) -> Result<String, Error> { ... }
    fn sql_model_dump_json_pretty(&self) -> Result<String, Error> { ... }
    fn sql_model_dump_json_by_alias(&self) -> Result<String, Error> { ... }
    fn sql_model_dump_json_with_options(
        &self,
        options: DumpOptions,
    ) -> Result<String, Error> { ... }
}
Expand description

Model-aware dump that supports field aliases and computed field exclusion.

Unlike the generic ModelDump, this trait uses the Model::fields() metadata to transform field names to their serialization aliases in the output and to handle computed fields properly.

§Example

#[derive(Model, Serialize, Deserialize)]
struct User {
    #[sqlmodel(serialization_alias = "userName")]
    name: String,
    #[sqlmodel(computed)]
    full_name: String, // Derived field, not in DB
}

let user = User { name: "Alice".to_string(), full_name: "Alice Smith".to_string() };
let json = user.sql_model_dump(DumpOptions::default().by_alias())?;
assert_eq!(json["userName"], "Alice");

// Exclude computed fields
let json = user.sql_model_dump(DumpOptions::default().exclude_computed_fields())?;
assert!(json.get("full_name").is_none());

Provided Methods§

Source

fn sql_model_dump(&self, options: DumpOptions) -> DumpResult

Serialize a model to a JSON value, optionally applying aliases.

Source

fn sql_model_dump_json(&self) -> Result<String, Error>

Serialize a model to a JSON string with default options.

Source

fn sql_model_dump_json_pretty(&self) -> Result<String, Error>

Serialize a model to a pretty-printed JSON string.

Source

fn sql_model_dump_json_by_alias(&self) -> Result<String, Error>

Serialize with aliases to a JSON string.

Source

fn sql_model_dump_json_with_options( &self, options: DumpOptions, ) -> Result<String, Error>

Serialize a model to a JSON string with full options support.

This method supports all DumpOptions including the indent option:

  • indent: None - compact JSON output
  • indent: Some(n) - pretty-printed with n spaces indentation

Compared to model_dump_json_with_options, this method also applies Model-specific transformations like serialization aliases.

§Example
use sqlmodel_core::validate::{SqlModelDump, DumpOptions};

// With aliases and 2-space indent
let json = user.sql_model_dump_json_with_options(
    DumpOptions::default().by_alias().indent(2)
)?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Model + Serialize> SqlModelDump for T

Blanket implementation for all Model types that implement Serialize.