JSON type-safe encoding and decoding using Daniel Bünzli's jsont library. Use when Claude needs to: (1) Define typed JSON codecs for OCaml record types, (2) Parse JSON strings to OCaml values, (3) Serialize OCaml values to JSON, (4) Handle optional fields and backwards compatibility, (5) Work with nested JSON structures
View on GitHubavsm/ocaml-claude-marketplace
ocaml-dev
January 20, 2026
Select agents to install to:
npx add-skill https://github.com/avsm/ocaml-claude-marketplace/blob/main/plugins/ocaml-dev/skills/jsont/SKILL.md -a claude-code --skill jsontInstallation paths:
.claude/skills/jsont/# Jsont JSON Encoding/Decoding
## Dependencies
```dune
(libraries jsont jsont.bytesrw)
```
## Core Patterns
### Simple Object Codec
Map a JSON object to an OCaml record using `Jsont.Object.map` with `mem` for required fields:
```ocaml
type header = {
message_id : string;
method_ : string;
timestamp : int;
}
let header_codec =
Jsont.Object.map ~kind:"header"
(fun message_id method_ timestamp -> { message_id; method_; timestamp })
|> Jsont.Object.mem "messageId" Jsont.string ~enc:(fun h -> h.message_id)
|> Jsont.Object.mem "method" Jsont.string ~enc:(fun h -> h.method_)
|> Jsont.Object.mem "timestamp" Jsont.int ~enc:(fun h -> h.timestamp)
|> Jsont.Object.finish
```
### Optional Fields
Use `opt_mem` for optional JSON fields. The constructor receives `'a option`:
```ocaml
type config = {
name : string;
timeout : int; (* default if missing *)
}
let config_codec =
Jsont.Object.map ~kind:"config"
(fun name timeout_opt ->
{ name; timeout = Option.value ~default:30 timeout_opt })
|> Jsont.Object.mem "name" Jsont.string ~enc:(fun c -> c.name)
|> Jsont.Object.opt_mem "timeout" Jsont.int ~enc:(fun c -> Some c.timeout)
|> Jsont.Object.finish
```
### Skip Unknown Fields
Use `skip_unknown` before `finish` to ignore extra JSON fields (tolerant parsing):
```ocaml
let tolerant_codec =
Jsont.Object.map ~kind:"data" (fun id -> { id })
|> Jsont.Object.mem "id" Jsont.string ~enc:(fun d -> d.id)
|> Jsont.Object.skip_unknown (* ignore extra fields *)
|> Jsont.Object.finish
```
### Nested Objects
Compose codecs for nested structures:
```ocaml
type request = { header : header; payload : payload }
let request_codec payload_codec =
Jsont.Object.map ~kind:"request" (fun header payload -> { header; payload })
|> Jsont.Object.mem "header" header_codec ~enc:(fun r -> r.header)
|> Jsont.Object.mem "payload" payload_codec ~enc:(fun r -> r.payload)
|> Jsont.Object.finish
```
### Lists
Use `Jsont.list` for JSON arrays:
``