| 488 | /// Note that this method will not alter the primary key values in ActiveModel. |
| 489 | #[cfg(feature = "with-json")] |
| 490 | fn set_from_json(&mut self, json: serde_json::Value) -> Result<(), DbErr> |
| 491 | where |
| 492 | <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<Self>, |
| 493 | for<'de> <<Self as ActiveModelTrait>::Entity as EntityTrait>::Model: |
| 494 | serde::de::Deserialize<'de>, |
| 495 | { |
| 496 | use crate::Iterable; |
| 497 | |
| 498 | // Backup primary key values |
| 499 | let primary_key_values: Vec<(<Self::Entity as EntityTrait>::Column, ActiveValue<Value>)> = |
| 500 | <<Self::Entity as EntityTrait>::PrimaryKey>::iter() |
| 501 | .map(|pk| (pk.into_column(), self.take(pk.into_column()))) |
| 502 | .collect(); |
| 503 | |
| 504 | // Replace all values in ActiveModel |
| 505 | *self = Self::from_json(json)?; |
| 506 | |
| 507 | // Restore primary key values |
| 508 | for (col, active_value) in primary_key_values { |
| 509 | match active_value { |
| 510 | ActiveValue::Unchanged(v) | ActiveValue::Set(v) => self.set(col, v), |
| 511 | NotSet => self.not_set(col), |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | Ok(()) |
| 516 | } |
| 517 | |
| 518 | /// Create ActiveModel from a JSON value |
| 519 | #[cfg(feature = "with-json")] |