Skip to content

Add JSON backend#1582

Open
wxrdnx wants to merge 3 commits intorems-project:sail2from
wxrdnx:json-backend
Open

Add JSON backend#1582
wxrdnx wants to merge 3 commits intorems-project:sail2from
wxrdnx:json-backend

Conversation

@wxrdnx
Copy link
Copy Markdown

@wxrdnx wxrdnx commented Dec 22, 2025

I've implemented a simple JSON backend that dumps the internal Sail AST into a JSON file.
The goal is to convert the AST into a cross-language representation that it can be used by languages other than Ocaml.

@Alasdair
Copy link
Copy Markdown
Collaborator

Rather than using string interpolation like

let rec json_of_value = function
  | V_vector values -> sprintf "{\"V_vector\":%s}" (json_of_list json_of_value values)

The better way to do this in OCaml is to use polymorphic variants, which is how we represent JSON elsewhere, e.g the above would be

let rec json_of_value = function
  | V_vector values -> `Assoc [("V_vector", json_of_list json_of_value values)]

The type we use is the Yojson Safe variant https://ocaml.org/p/yojson/3.0.0/doc/yojson/Yojson/Safe/index.html. With polymorphic variants you don't actually need to import the library, as they are structurally typed, but you can for type annotations.

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.

2 participants