Skip to content

Serialize and deserialize support for Map type.#41

Merged
volovyks merged 1 commit intonear:masterfrom
zgendao:master
Dec 1, 2021
Merged

Serialize and deserialize support for Map type.#41
volovyks merged 1 commit intonear:masterfrom
zgendao:master

Conversation

@PopcornPaws
Copy link
Contributor

It would be nice if Borsh-js would support Map types out of the box.

For example, I would like to use a rust BTreeMap type on the contract side that can be serialized/deserialized on the client side in a Map type. As far as I know, BTreeMaps are serialized similarly to a Vec, namely there is 4 bytes reserved at the front to encode the length (size) in LE representation, then the keys and values are serialized in a sorted sequence, so, contrary to a HashMap type, there's no danger of iterating over the key-value pairs randomly.

What do you guys think?

@zaxozhu
Copy link

zaxozhu commented Nov 22, 2021

@volovyk-s would you please take a look?

@volovyks
Copy link
Contributor

@PopcornPaws awesome contribution! Have you tested it with borsh-rs? Can we serialize it here and deserialize it in Rust and vise-versa?

@PopcornPaws
Copy link
Contributor Author

PopcornPaws commented Nov 30, 2021

@PopcornPaws awesome contribution! Have you tested it with borsh-rs? Can we serialize it here and deserialize it in Rust and vise-versa?

@volovyk-s Yep, I tested the (de)serialization of a BTreeMap<[u8; 32], Pubkey>, a BTreeMap<String, Option<u32> and a BTreeMap<u16, String> type in Rust and it worked seamlessly in both ways.

@volovyks volovyks merged commit b80a6d4 into near:master Dec 1, 2021
@volovyks
Copy link
Contributor

volovyks commented Dec 1, 2021

Thank you @PopcornPaws !

@PopcornPaws
Copy link
Contributor Author

@volovyk-s thank you!

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.

3 participants