Skip to content

Support BtreeMap and HashMap for deserialization using TryGetableFromJson#3009

Merged
Huliiiiii merged 7 commits intoSeaQL:masterfrom
chinmaypandya:feature/implement_trygetable_for_treemap_hahmap
Mar 29, 2026
Merged

Support BtreeMap and HashMap for deserialization using TryGetableFromJson#3009
Huliiiiii merged 7 commits intoSeaQL:masterfrom
chinmaypandya:feature/implement_trygetable_for_treemap_hahmap

Conversation

@chinmaypandya
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Member

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

@chinmaypandya
Copy link
Copy Markdown
Contributor Author

Yeah sure let me reduce the comments content

Comment thread src/executor/query.rs Outdated

#[cfg(feature = "with-json")]
/// This implementation allows SeaORM to extract JSON database columns
/// directly into `HashMap<K, V>` using [`TryGetable`].
Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
@chinmaypandya
Copy link
Copy Markdown
Contributor Author

Hello @Huliiiiii, @Expurple, @tyt2y3. Good day!
Can I get an update for review for the PR ?

@Huliiiiii Huliiiiii merged commit 9c27b5a into SeaQL:master Mar 29, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

🎉 Released In 2.0.0-rc.38 🎉

Huge thanks for the contribution!
This feature has now been released, so it's a great time to upgrade.
Show some love with a ⭐ on our repo, every star counts!

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