Declarative JSON data manipulation for OCaml
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 99.1%
Dune 0.3%
Other 0.7%
59 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-json https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-json
git@git.recoil.org:gazagnaire.org/ocaml-json git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-json

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

Json - Declarative JSON data manipulation for OCaml#

Json is an OCaml library for declarative JSON data manipulation. It provides:

  • Codecs for describing JSON data with OCaml values of your choice. These descriptions can decode, encode, query, and update JSON data without requiring callers to construct a generic JSON representation.
  • Json.t, the generic JSON value type. AST helper constructors and operations live in Json.Value.
  • A byte-stream JSON codec with optional text-location tracking and layout preservation. The codec is compatible with effect-based concurrency.
  • Browser-native JSON support through the json.brr library.

The typed combinator API lives under Json.Codec. In particular, Json.Codec.Object is the object-codec module; top-level Json.Object is only the Json.t variant constructor. The main Json module keeps Json.t as the primary generic JSON value type and provides byte-level helpers such as Json.of_string, Json.to_string, Json.decode, and Json.encode.

Json is distributed under the ISC license and is a fork of the excellent Jsont library. The core engine is exactly the same; the public API is slightly different.

Homepage: https://tangled.org/gazagnaire.org/ocaml-json

Installation#

Install with opam:

$ opam install nox-json

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-json

The main library depends on bytesrw, fmt, and loc. Browser support is provided by the optional Dune library json.brr, which depends on brr.

Documentation#

Build and browse the API documentation locally with:

dune build @doc

If the package is installed in a switch with odig, use:

odig doc json

Quick Example#

module C = Json.Codec

type item = { task : string; done_ : bool }

let item task done_ = { task; done_ }

let item_codec =
  C.Object.map ~kind:"item" item
  |> C.Object.member "task" C.string ~enc:(fun i -> i.task)
  |> C.Object.member "done" C.bool ~enc:(fun i -> i.done_)
  |> C.Object.seal

let decode s = Json.of_string item_codec s
let encode item = Json.to_string item_codec item

For generic Json.t values, use Json.Value constructors:

let value =
  let open Json.Value in
  object' [ member (name "ok") (bool true) ]

let text = Json.Value.to_string value

Examples#

More complete examples live in the test/codecs directory: