package js_of_ocaml
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
Compiler from OCaml bytecode to JavaScript
Install
dune-project
Dependency
Authors
Maintainers
Sources
js_of_ocaml-6.5.0.tbz
sha256=a991ce25175a5a13240c8bf64354b8f35e0a8d7223b90bef01ba32bc6f929cf4
sha512=ce476986cb2c78990f29b1d7a01bb173a0d30ad0e0a80ba7bde9aa8365952ed01eda4935d92a323b982b0c1c897b20ef01eabe75fe80e4da858f73f6a2d25232
doc/ppx-deriving.html
JSON derivation
The js_of_ocaml-ppx_deriving_json package provides a PPX deriver for serializing OCaml values to JSON.
Important: The serialization format follows js_of_ocaml's internal representation. It is designed for communication between a js_of_ocaml program and a server-side OCaml application, not for interacting with third-party APIs.
Installation
opam install js_of_ocaml-ppx_deriving_json
Usage
Add [@@deriving json] to your type definitions:
type person = {
name : string;
age : int;
}
[@@deriving json]
(* Serialize to a JSON string *)
let json_str = Deriving_Json.to_string person_json { name = "Alice"; age = 30 }
(* Deserialize from a JSON string *)
let alice = Deriving_Json.from_string person_json json_strThe [@@deriving json] attribute generates:
person_to_json : Buffer.t -> person -> unit— write a value to a bufferperson_of_json : Deriving_Json_lexer.lexbuf -> person— read a value from a lexer bufferperson_json : person Deriving_Json.t— a witness, usable withDeriving_Json.to_stringandDeriving_Json.from_stringfor string conversion (as in the example above)
Note: For types named t, the prefix is omitted (e.g., of_json instead of t_of_json).
The [%to_json: type] and [%of_json: type] syntax also works with any type expression:
let json = [%to_json: int list] [1; 2; 3]
let nums = [%of_json: int list] jsonSupported types
The deriver supports:
- Basic types:
int,float,string,bool - Options:
'a option - Lists and arrays:
'a list,'a array - Records and variants
- Polymorphic variants
- Recursive types
See also
- JSON serialization — Direct JSON output
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page