Movatterモバイル変換


[0]ホーム

URL:


packageppx_protocol_conv

  1. Overview
  2. Docs

You can search for identifiers within the package.

in-package search v0.2.0

Ppx for generating serialisation and de-serialisation functions of ocaml types

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_protocol_conv-5.2.2.tbz
sha256=994362c2185d12f732e522e1e457b7de67745e594b898368c878424e93f84587
sha512=237b236a257f35ad671194f6ee0690dfc85eef9b088a928e7b0582b23b5acc19b6727318be6b7abfa0f6c1052047b820e7a0345d8cadb3c0280e18dc3da6e453

Description

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

Pre-defined drivers are available in separate packages:ppx_protocol_conv_json (Yojson.Safe.json)ppx_protocol_conv_jsonm (Ezjson.value)ppx_protocol_conv_msgpack (Msgpck.t)ppx_protocol_conv_xml-light (Xml.xml)ppx_protocol_conv_xmlm (Xmlm.node)ppx_protocol_conv_yaml (Yaml.value)

Published:23 Jan 2025

README

Ppx Protocol Conv

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

This page contains an simple overview of functionality provided. More information is available in thewiki pages

Table of contents

  1. Features

  2. Examples

  3. Drivers

  4. Custom drivers

  5. Not supported

Features

The ppx supports the following features:

  • records

  • recursive and non-recursive types

  • variants

  • polymophic variants

  • All primitive types (except nativeint)

The following drivers exists

  • Json which serializes toYojson.Safe.t

  • Jsonm which serializes toEzjsonm.value

  • Msgpack which serializes toMsgpck.t

  • Yaml which serializes toYaml.t

  • Xml_light which serializes toXml.xml list

  • Xmlm which serializes toEzxmlm.node

Examples

open Protocol_conv_jsontype a = {  x: int;  y: string [@key "Y"]  z: int list [@default [2;3]]} [@@deriving protocol ~driver:(module Json)]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.tval a_of_json_exn: Json.t -> aval a_of_json: Json.t -> (a, exn) resultval b_to_json: a -> Json.tval b_of_json_exn: Json.t -> aval b_of_json: Json.t -> (b, exn) result
a_to_json { x=42; y:"really"; z:[6;7] }

Evaluates to

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

to_protocol deriver will generate serilisation of the type.of_protocol deriver generates de-serilisation of the type, whileprotocol deriver will generate both serilisation and de-serilisation functions.

Attributes

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

Variant and polymorphic variant constructors names can be changed using the[@name <string>] attribute.

If a record field is not present in the input when deserialising, as default value can be assigned using[@default <expr>]. If the value to be serialized matches the default value, the field will be omitted (Some drivers allow disabling this functonality. Comparrison uses polymorphic compare, so be careful.

Signatures

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

Drivers

Drivers specify concrete serialization and deserialization. Users of the library can elect to implement their own driver seecustom drivers, or use predefined drivers:

  • Json which serializes toYojson.Safe.t

  • Jsonm which serializes toEzjsonm.value

  • Msgpack which serializes toMsgpck.t

  • Yaml which serializes toYaml.t

  • Xml_light which serializes toXml.xml list

  • Xmlm which serializes toEzxmlm.node

Custom drivers

It is easy to provide custom drivers by implementing the signature:

include Protocol_conv.Runtime.Driver with  type t = ...

See thedrivers directory for examples on how to implemented new drivers. Submissions of new drivers are more than welcome.

Not supported

  • Generalised algebraic datatypes

  • Extensible types

  • Extensible polymorphic variants

Dependencies (4)

  1. ppxlib>= "0.9.0" & < "0.36.0"
  2. dune>= "1.2"
  3. base>= "v0.14.0"
  4. ocaml>= "4.07"

Dev Dependencies (3)

  1. alcotestwith-test & >= "0.8.0"
  2. sexplibwith-test
  3. ppx_sexp_convwith-test

Conflicts

None


[8]ページ先頭

©2009-2025 Movatter.jp