Skip to content

Support BtreeMap and HashMap for deserialization using TryGetableFromJson#3009

Open
chinmaypandya wants to merge 5 commits intoSeaQL:masterfrom
chinmaypandya:feature/implement_trygetable_for_treemap_hahmap
Open

Support BtreeMap and HashMap for deserialization using TryGetableFromJson#3009
chinmaypandya wants to merge 5 commits intoSeaQL:masterfrom
chinmaypandya:feature/implement_trygetable_for_treemap_hahmap

Conversation

@chinmaypandya
Copy link

@chinmaypandya chinmaypandya commented Mar 14, 2026

PR Info

Field now supports BTreeMap and HashMap

  • Dependencies:
  • Dependents:

New Features

  • Implement TryGetableFromJson for HashMap<K, V> and BTreeMap<K, V> to
    allow SeaORM to deserialize JSON database columns directly into these map
    types.
impl<K, V> TryGetableFromJson for HashMap<K, V>
where
    K: DeserializeOwned + Eq + Hash,
    V: DeserializeOwned,
{}

impl<K, V> TryGetableFromJson for BTreeMap<K, V>
where
    K: DeserializeOwned + Ord,
    V: DeserializeOwned,
{}

This enables users to define entity fields such as:

BTreeMap<String, T>
HashMap<String, T>

for JSON columns without requiring custom wrapper types.

Added unit tests:

  • json_deserialize_to_btreemap
  • json_deserialize_to_hashmap

Example usage

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct Component {
    base_price: Decimal,
    component_type: String,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "product")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    pub name: String,
    pub component_list: BTreeMap<String, Component>, // Now possible
}

Tests

Screenshot 2026-03-15 at 1 44 53 AM

Bug Fixes

Breaking Changes

Changes

  • [ ]

Chinmay Pandya added 2 commits March 15, 2026 01:37
…mJson

Implement `TryGetableFromJson` for `HashMap<K, V>` and `BTreeMap<K, V>` to
allow SeaORM to deserialize JSON database columns directly into these map
types.

This enables users to define entity fields such as:

    BTreeMap<String, T>
    HashMap<String, T>

for JSON columns without requiring custom wrapper types.

Added unit tests:
- json_deserialize_to_btreemap
- json_deserialize_to_hashmap
@Huliiiiii
Copy link
Member

Could you clean up the comments? A lot of them are unnecessary.

@chinmaypandya
Copy link
Author

Yeah sure let me reduce the comments content


#[cfg(feature = "with-json")]
/// This implementation allows SeaORM to extract JSON database columns
/// directly into `HashMap<K, V>` using [`TryGetable`].
Copy link
Member

Choose a reason for hiding this comment

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

This is also unnecessary.

Copy link
Author

Choose a reason for hiding this comment

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

So no comments at all ? Should I also remove cfg feature ?

Copy link
Member

Choose a reason for hiding this comment

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

TryGetableFromJson requires with-json feature

Copy link
Author

Choose a reason for hiding this comment

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

Okay so I will just remove the comments

Copy link
Author

Choose a reason for hiding this comment

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

Done! please review

@Huliiiiii Huliiiiii requested review from Expurple and tyt2y3 March 15, 2026 12:37
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.

Json field to support BTreeMap and HashMap

2 participants