package ppx_deriving_yojson
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=22cd5e3a69bf573a34e104adc9c936e37e599c8e7d9f4fe92c267c15de06ffc3
md5=83128c06b0569309351886d9fd0dbb74
Description
ppx_deriving_yojson is a ppx_deriving plugin that provides a JSON codec generator.
README
[@@deriving yojson]
deriving Yojson is a ppx_deriving plugin that generates JSON serializers and deserializes that use the Yojson library from an OCaml type definition.
Sponsored by Evil Martians.
Installation
deriving Yojson can be installed via OPAM:
$ opam install ppx_deriving_yojson
Usage
In order to use deriving yojson, require the package ppx_deriving_yojson
.
Syntax
deriving yojson generates two functions per type:
# #require "ppx_deriving";;
# type ty = .. [@@deriving yojson];;
val ty_of_yojson : Yojson.Safe.json -> (ty, string) Result.result
val ty_to_yojson : ty -> Yojson.Safe.json
When the deserializing function returns `Error loc
, loc
points to the point in the JSON hierarchy where the error has occurred.
It is possible to generate only serializing or deserializing functions by using [@@deriving to_yojson]
or [@@deriving of_yojson]
. It is also possible to generate an expression for serializing or deserializing a type by using [%to_yojson:]
or [%of_yojson:]
; non-conflicting versions [%derive.to_yojson:]
or [%derive.of_yojson:]
are available as well.
If the type is called t
, the functions generated are {of,to}_yojson
instead of t_{of,to}_yojson
.
Semantics
deriving yojson handles tuples, records, normal and polymorphic variants; builtin types: int
, int32
, int64
, nativeint
, float
, bool
, char
, string
, bytes
, ref
, list
, array
, option
and their Mod.t
aliases.
The following table summarizes the correspondence between OCaml types and JSON values:
OCaml type | JSON value | Remarks |
---|---|---|
int , int32 , float |
Number | |
int64 , nativeint |
Number | Can exceed range of double |
bool |
Boolean | |
string , bytes |
String | |
char |
String | Strictly one character in length |
list , array |
Array | |
ref |
'a | |
option |
Null or 'a | |
A record | Object | |
Yojson.Safe.json |
any | Identity transformation |
Variants (regular and polymorphic) are represented using arrays; the first element is a string with the name of the constructor, the rest are the arguments. Note that the implicit tuple in a polymorphic variant is flattened. For example:
# type pvs = [ `A | `B of int | `C of int * string ] list [@@deriving yojson];;
# type v = A | B of int | C of int * string [@@deriving yojson];;
# type vs = v list [@@deriving yojson];;
# print_endline (Yojson.Safe.to_string (vs_to_yojson [A; B 42; C (42, "foo")]));;
[["A"],["B",42],["C",42,"foo"]]
# print_endline (Yojson.Safe.to_string (pvs_to_yojson [`A; `B 42; `C (42, "foo")]));;
[["A"],["B",42],["C",42,"foo"]]
Record variants are represented in the same way as if the nested structure was defined separately. For example:
# type v = X of { v: int } [@@deriving yojson];;
# print_endline (Yojson.Safe.to_string (v_to_yojson (X { v = 0 })));;
["X",{"v":0}]
Record variants are currently not supported for extensible variant types.
By default, objects are deserialized strictly; that is, all keys in the object have to correspond to fields of the record. Passing strict = false
as an option to the deriver (i.e. [@@deriving yojson { strict = false }]
) changes the behavior to ignore any unknown fields.
Options
Option attribute names may be prefixed with yojson.
to avoid conflicts with other derivers.
[@key]
If the JSON object keys differ from OCaml conventions, lexical or otherwise, it is possible to specify the corresponding JSON key implicitly using [@key "field"]
, e.g.:
type geo = {
lat : float [@key "Latitude"];
lon : float [@key "Longitude"];
}
[@@deriving yojson]
[@name]
If the JSON variant names differ from OCaml conventions, it is possible to specify the corresponding JSON string explicitly using [@name "constr"]
, e.g.:
type units =
| Metric [@name "metric"]
| Imperial [@name "imperial"]
[@@deriving yojson]
[@encoding]
Very large int64
and nativeint
numbers can wrap when decoded in a runtime which represents all numbers using double-precision floating point, e.g. JavaScript and Lua. It is possible to specify the [@encoding `string]
attribute to encode them as strings.
[@default]
It is possible to specify a default value for fields that can be missing from the JSON object, e.g.:
type pagination = {
pages : int;
current : (int [@default 0]);
} [@@deriving yojson]
Fields with default values are not required to be present in inputs and will not be emitted in outputs.
License
deriving yojson is distributed under the terms of MIT license.
Dependencies (8)
-
cppo_ocamlbuild
build
-
cppo
build
-
ocamlbuild
build
-
ocamlfind
build
-
ppx_deriving
>= "4.0" & < "4.3"
- result
-
yojson
< "1.6.0"
-
ocaml
< "4.08"
Dev Dependencies (2)
-
ppx_import
with-test & >= "1.1" & <= "1.5"
-
ounit
with-test & >= "2.0.0"
Used by (62)
- archetype
-
aws-s3
< "1.1.0"
-
builder-web
>= "0.2.0"
- camels
- coclobas
- current_ocluster
- dap
-
earlybird
< "1.0.0"
-
easy_logging_yojson
< "0.8.2"
- fiat-p256
-
frama-c
>= "25.0"
- fred
-
fstar
>= "0.9.6.0~alpha1"
-
gdbprofiler
< "0.2"
-
gemini
< "0.3.0"
-
goblint
< "2.0.0"
- grpc-examples
- hacl_x25519
- hardcaml-lua
- hl_yaml
-
i3ipc
< "0.4"
- index-bench
-
irmin-bench
< "3.0.0"
-
jhupllib
>= "0.1.1"
-
jupyter
< "2.7.0"
- karamel
-
ketrew
>= "3.0.0"
-
key-parsers
>= "0.3.0" & < "0.10.0"
-
kremlin
< "transition"
- kubecaml
- lambda-runtime
-
links
>= "0.7.3" & < "0.9"
- mirage-crypto-ec
- morbig
- netml
- noise
-
nsq
>= "0.2.4" & < "0.4.0"
- ocluster-api
- oframl
-
ogen
>= "0.1.3"
-
ojs-base
>= "0.3.0" & < "0.6.0"
- opam-check-npm-deps
-
oraft
< "0.2.0"
- pds-reachability
- petr4
-
pkcs11
< "0.18.0"
- pkcs11-driver
- ppx_json_types
-
ppx_netblob
>= "1.2.1"
-
repr-bench
< "0.2.1"
- safemoney
-
satyrographos
>= "0.0.2.8"
-
slacko
>= "0.13.0" & < "0.15.0"
-
socket-daemon
>= "0.3.0"
- stitch
- textrazor
- u2f
-
usbmux
>= "1.3.2"
- vercel
-
wamp
< "1.0"
- webauthn
- zeit
Conflicts (1)
-
ppx_deriving
= "4.2"