I am using atdgen to create json translators. I am getting “Unbound module Bi_outbuf” errors in the generated code. I can’t find any docs about module Bi_outbuf.
Have I failed to install something?
I am using atdgen to create json translators. I am getting “Unbound module Bi_outbuf” errors in the generated code. I can’t find any docs about module Bi_outbuf.
Have I failed to install something?
I suspect you’re using yojson 2 (recent!) with atdgen, which might not
be up-to-date with that yet. Yojson2 removed biniou as a dependency,
which is where Bi_outbuf is from. I think.
So rollback to older Yojson or include biniou as a dependency in dune?
Any obvious advantages either way?
Rollback to yojson < 2 should work. Including biniou as a dependency
might work, but maybe atdgen expects yojson to use biniou, which it
won’t.
A simple dependency didn’t work. Rolling back.
Any reason you need to use atdgen specifically for JSON codecs? Why not use a Yojson PPX like ppx_deriving_yojson
or ppx_yojson_conv
?
Because I hadn’t heard about them? I will look.
Is there a currently most accepted solution for parsing JSON?
That would probably be yojson: GitHub - ocaml-community/yojson: Low-level JSON parsing and pretty-printing library for OCaml
Just to clarify–are you asking about parsing as in taking a string as input and producing a JSON structure as output? Or decoding as in, taking a JSON structure as input and producing a custom OCaml type as output?
I am asking about Decoding.
In that case, go with one of the PPXs. They automatically derive encoders/decoders from OCaml type definitions. E.g. say you get a JSON object:
{"a": 1, "b": true, "c": "3"}
You can declare a corresponding type and automatically derive the codec:
type my_type = {
a : float;
b : bool;
c : string;
} [@@deriving yojson]
This will give you a function my_type_of_yojson
you can then use to decode JSON values into the custom type.
I recently had the impression adding a ppx had a huge footprint on binary size. (10MB became 20MB with LWT operators so maybe not related to yojson). But watch out in case.
(Wanted to have this post appear under the relevant previous posting, so deleted it and re-created but may not This forum software is really encroaching).
Maybe if you’re using a lot of them? I just checked and a tool I wrote using ppx_deriving_yojson
is 4.1 MB after stripping. It also uses multiple libraries on top of the httpaf stack so it’s doing quite a lot. IMHO this is quite small especially given the usual OCamler attitude of not even blinking an eye at 100 MB Js_of_ocaml payloads served to the browser.
Re: Lwt PPX, IMHO it’s obsolete now with let-ops support.
I did that. I have tried to use that function and keep getting “unbound value”. I have looked for example code and have not found it.
I am new to OCaml, but not to programming.
With respect to the larger design question, atdgen would be better if it generated more languages, it’s obviously what to use in a multilingual environment. I am tempted to implement it for Elm.
Sounds like something that can be debugged. If you can post a small repro that shows the problem, we can help. Here’s a small working example that I created a while back: GitHub - yawaramin/gen_json_string: Utility to generate fake JSON data from a JSON Schema
EDIT: lots more examples in the tests: ppx_deriving_yojson/test_ppx_yojson.ml at master · ocaml-ppx/ppx_deriving_yojson · GitHub
W.r.t. atdgen, if you are looking for a more widely-supported serialization stack, there are other options like Thrift-JSON, and even GraphQL.
I’m just past hello, world at this point, so things are small, and a bit cargo cultish.
type t =
{
capital : float
} [@@deriving of_yojson]
val of_file : string -> t
open Core
let of_file filename =
In_channel.read_all filename
|> Yojson.Safe.from_string
|> t_of_yojson
Yes, I get tempted by a lot of things I should not do.
Can you show the exact error message and the dune file? We need to know if you are linking in the correct libraries into the build.
jrootham@Doonesbury:~/dev/politics/mchc/murb_model$ dune build
File "lib/project.ml", line 6, characters 7-18:
6 | |> t_of_yojson
^^^^^^^^^^^
jrootham@Doonesbury:~/dev/politics/mchc/murb_model$ cat lib/dune
(library
(name murb_model)
(libraries yojson core))
Your dune file needs to include a preprocess
directive (see here) to run the ppx_yojson_conv ppx:
(library
(name murb_model)
(preprocess (pps ppx_yojson_conv))
(libraries yojson core))