Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing Hashtbls with [@to_yojson]: Unbound value Hashtbl.to_yojson #113

Open
dexterleng opened this issue Jan 1, 2020 · 1 comment

Comments

@dexterleng
Copy link

I am trying to serialize Hashtbls as associative lists in JSON.

Following the example in the docs:

let yojson_of_kgram_lookup m =
    [%to_yojson: (int * kgram list) list] (Hashtbl.to_alist m)

type project_file = {
  file_name: string;
  selected_kgrams_by_hash: ((int, kgram list) Hashtbl.t) [@to_yojson yojson_of_kgram_lookup];
} [@@deriving yojson]

I get this error:

project_compare.ml|1 col 1 error| Unbound value Hashtbl.to_yojson      
@ttamttam
Copy link

ttamttam commented Jan 25, 2020

I was to report the following in a new issue, but it seems to me it is the same problem?

I would have expected

`Assoc [("f1", `Int 2); ("f2", `Int 2)]?

in the following?

─( 19:31:23 )─< command 0 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppx_deriving_yojson";;
─( 19:31:24 )─< command 1 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # type ty = { f1: int [@to_yojson fun i -> `Int(succ i)]; f2: int} [@@deriving to_yojson];;
type ty = { f1 : int; f2 : int; }
val ty_to_yojson : ty -> Yojson.Safe.t = <fun>
─( 19:31:39 )─< command 2 >────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # ty_to_yojson {f1:1; f2:2};;
utop # ty_to_yojson {f1=1; f2=2};;
- : Yojson.Safe.t = `Assoc [("f1", `Int 1); ("f2", `Int 2)]

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

No branches or pull requests

2 participants