package ppx_protocol_conv

  1. Overview
  2. Docs
Serialization and de-serialization of ocaml types to/from json, msgpack and xml_light.

Install

Dune Dependency

Authors

Maintainers

Sources

1.0.0.tar.gz
sha256=944996354ab3148cb862c7aa0fb7fcd42a131e1c09df484b35275f4c9d474535
md5=e63f02fa4328194070c72b3d9a0ae9d5

Description

The syntax extension generates code to serialize and de-serialize types. The ppx itself does not contain any protocol specific code, but relies on 'drivers' that defines serialization and de-serialisation of basic types and structures.

The library provides the following drivers for serialization and deserialization:

  • json (Yojson.Safe.json)
  • xml_light (Xml.xml)
  • msgpack (Msgpck.t)

Published: 01 Mar 2018

README

Ppx Protocol Conv

Ppx protocol conv (de)serialisers using deriving, which allows for plugable (de)serialisers.

Example Usage

open Protocol_conv
open Protocol_conv_json
type a = {
  x: int;
  y: string [@key "Y"]
} [@@deriving protocol ~driver:(module Json) ~flags:(`Mangle Json.mangle)]

type b = A of int
       | B of int [@key "b"]
       | C
[@@deriving protocol ~driver:(module Json)]

will generate the functions:

val a_to_json: a -> Json.t
val a_of_json: Json.t -> a

val b_to_json: a -> Json.t
val b_of_json: Json.t -> a
a_to_json { x=42; y:"really" }

Evaluates to

[ "x", `Int 42; "Y", `String "really"] (* Yojson.Safe.json *)

to_protocol deriver will generate serilization of the type. of_protocol deriver generates de-serilization of the type, while protocol deriver will generate both serilizarion and de-serilization functions.

Flags can be specified using the driver argument ~flags. For the json module, the mangle function transforms record label names to be lower camelcase: a_bc_de -> aBcDe and a_bc_de_ -> aBcDe. Beware that this may cause name collisions, which can only be determined at compile time.

Attributes

Record label names can be changed using [@key <string>]

Variant constructors names can also be changed using the [@key <string>] attribute.

Signatures

The ppx also handles signature, but disallows [@key ...] and ~flags:... as these does not impact signatures.

Drivers

The protocol deriver implements:

  • Json which serializes to Yojson.Safe.t

  • Xml_light which serializes to Xml.xml list

  • Msgpack which serializes to Msgpck.t

Custom drivers

It should be easy to provide custom drivers by implementing the signature:

include Lib.Driver with type t = ... and type flags = ...

See the drivers directory for examples on how to implemented new drivers. Submissions of useful drivers are welcome

Limitations

The json driver will currently serialize type t option option as t option. This means that Some None and None is both mapped to Null.

Dependencies (10)

  1. ppx_metaquot build
  2. jbuilder >= "1.0+beta7"
  3. ppx_core
  4. ppx_driver >= "v0.10.1"
  5. ppx_type_conv
  6. base < "v0.11"
  7. msgpck
  8. xml-light
  9. yojson < "1.6.0"
  10. ocaml >= "4.04"

Dev Dependencies

None

Used by (2)

  1. aws-s3 >= "1.1.1" & < "3.0.0"
  2. ppx_deriving_protocol >= "0.8.1"

Conflicts

None

OCaml

Innovation. Community. Security.