Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

marshalling a hash table in ocaml

I have a hash table in ocaml and I want to store this entire hash table as value field in a Berkeley DB. So I am trying to Marshal the hash table using Marshal.to_string. This returns a string but when I try to unmarshal the same string using Marshal.from_string, an Exception is thrown. Any ideas on what the issue here is?

like image 531
user2592026 Avatar asked Mar 07 '26 08:03

user2592026


1 Answers

You have to annotate the type of the value you're unmarshaling. Like so (in top-level):

type t = (string, string) Hashtbl.t;;

let key = "key" in
let t_original : t = Hashtbl.create 1 in
Hashtbl.add t_original key "value";
let t_marshalled = Marshal.to_string t_original [] in
let t_unmarshalled : t = Marshal.from_string t_marshalled 0 in
assert ((Hashtbl.find t_original key) = (Hashtbl.find t_unmarshalled key));;
like image 166
lebowski Avatar answered Mar 10 '26 03:03

lebowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!