package ppx_meta_conv

  1. Overview
  2. Docs
PPX for converting between OCaml values and JSON, Sexp and camlon

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_meta_conv-4.1.0.tar.gz
md5=aad544375f23a3979aafe9defdebf4bd
sha512=991ad7433c8f2f51b298d4c99b35b873aab66c5d1da1beaa18f8f58f8e708e5a4299f0bb657af9d12bfcd3d645f48b3efe21e13b8c62e303604bd374e88f7d3a

Description

Published: 05 Aug 2022

README

Meta_conv for ppx

ppx_meta_conv is a plugin/wrapper for ppx_deriving to provide data conversion between OCaml values and tree formed data structures such as JSON, Sexp, pseudo OCaml value code and so on.

ppx_meta_conv is a PPX port of meta_conv for CamlP4, which is a generalization of type_conv. The first objective of meta_conv and ppx_meta_conv is to provide an easy way to implement conversions of data formats as possible. If you get performance problems probably you should check other ppx based data converters specific to one data format.

Typical usage of ppx_meta_conv is like type ty = ... [@@deriving conv{target}]. ppx_meta_conv creates conversion functions between ty and target, namely target_of_ty, ty_of_target and ty_of_target_exn. ppx_meta_conv itself knows nothing about the data type target except its name, and generate code which composes primitives defined in module Target_conv. The primitives of Target_conv must be given externally. As conversions, currently ppx_meta_conv_ocaml, ppx_meta_conv_tiny_json and ppx_meta_conv_sexp packages are provided.

Basic

type 'a t = <definition> [@@deriving conv{name}]

Multiple targets

type t = <definition> [@@deriving conv{target_1; ..; target_n}]

Only one direction

(* Only defines ocaml_of_t. t_of_ocaml is skipped *)
type t = <definition> [@@deriving conv{ocaml_of}]

Inlined

[%ocaml_of: type] (* type to Ocaml.t *)
[%of_ocaml: type] (* Ocaml.t to type. Failure is reported as `Error *)
[%of_ocaml_exn: type] (* Ocaml.t to type. Failure is reported as an exception *)

Using special name

Normally tag names of the external data structure (such as JSON) are as same as the names of variant constructors and record fields. You can override them by [@conv.as xxx] attribute:

type 'a t =
  | Zee                          (* The default name "Zee" is used *)
  | Foo [@conv.as foo]           (* "foo", instead of "Foo" *)
  | Bar [@conv.as "bar"]         (* "bar", instead of "Bar" *)
  | Boo [@conv.as {json="boo"}] (* "boo", instead of "Boo" only for "json" converter *)
  [@@deriving conv{ocaml, json}]
type t =
  { x : int [@conv.as X];           (* "X" is used, instead of "x" *) 
    y : float [@conv.as {json="Y"}] (* "Y" is used instead of "y" only for "json" converter *) 
  } 
  [@@deriving conv{ocaml; json}]

Field for Leftovers

External data structure may contain unexpected fields for OCaml programs, we can keep those leftovers as they are using a special type named mc_leftovers.

type 'a mc_leftovers = (string * 'a) list
type t =
  { x : int;
    y : float;
    rest : Tiny_json.t mc_leftovers;
  [@@deriving conv{json}]

With the above definition, json_of_t can handle JSON records which contain at least x and y fields. The other fields than x and y are stored in rest.

Ignore unknown fields

By default, ty_of_target and ty_of_target_exn functions fail if the input data contains unknown fields to these functions. Attribute [@conv.ignore_unknown_fields] makes these functions ignore such unknown fields:

type t = {
    foo: int;
    bar: float;
  } [@conv.ignore_unknown_fields] (* t_of_ocaml does not fail even if the source contains fields other than foo and bar *)
    [@@deriving conv{ocaml}]

Optional field

Type mc_option is equivalent with option but has a special meaning in ppx_meta_conv. Record fields with this type may not exist in the target data structure:

type t = { x : int;
           y : float mc_option  (* the source may have a field y of type float,
                                   or may not have it at all. *)
         } 
         [@@deriving conv{name}]

Dependencies (4)

  1. spotlib >= "4.0.0"
  2. ppx_deriving >= "5.2.0"
  3. ppxx >= "2.5.0"
  4. dune >= "2.0"

Dev Dependencies

None

Used by (4)

  1. dune_watch >= "0.1.0"
  2. scaml
  3. sociaml-facebook-api >= "0.4.1"
  4. treeprint >= "2.0.0" & < "2.3.0"

Conflicts (3)

  1. camlon < "2.0.0"
  2. sexplib < "112.24.01"
  3. tiny_json < "1.1.0"
OCaml

Innovation. Community. Security.