code :
open Yojson
(* Define an OCaml type *)
type person = {
name : string;
age : int;
}
[@@deriving to_yojson]
(* Example person *)
let my_person = { name = "Alice"; age = 30 }
(* let xx = person_to_yojson my_person *)
(* Convert person to JSON and store in a file *)
let store_person_as_json_file filename person =
let json_str = Yojson.Safe.to_string (person_to_yojson person) in
let oc = open_out filename in
output_string oc json_str;
close_out oc
(* Usage *)
let () = store_person_as_json_file "person.json" my_person
while executing this I am getting error
dune build
File "bin/main.ml", line 36, characters 40-56:
36 | let json_str = Yojson.Safe.to_string (person_to_yojson person) in
^^^^^^^^^^^^^^^^
Error: Unbound value person_to_yojson
dune file
(executable
(public_name cst_viewer)
(name main)
(libraries cst_viewer syntax yojson ppx_deriving_yojson))
(dirs syntax ml js_parser ext)
(env
(dev
(env-vars
(CPPO_FLAGS -U=RELEASE)))
(release
(env-vars
(CPPO_FLAGS -D=RELEASE))
(ocamlopt_flags
(:standard -O3 -unbox-closures)))
(static
(env-vars
(CPPO_FLAGS -D=RELEASE))
(ocamlopt_flags
(:standard -O3 -unbox-closures))))
AIM : wanted to convert the custom type of ocaml to json. please let me know if I am doing anything wrong or if there’s any other way to convert OCaml custom types to Json?