Skip to content

Driver Parameters

Anders Peter Fugmann edited this page Nov 8, 2019 · 5 revisions

Driver parameters

The following provided drivers shares a common framework, which allows users to changes default behavior when serialising and de-serialising.

To change the default behavior do:

module MyJsonDriver = Json.Make(
  struct
    (* Uppercase all record field names *)
    let field_name name = String.uppercase_ascii 
    
    (* Lowercase all variant names *)
    let variant_name name = String.lowercase_ascii

    (* Constructors that takes no arguments are serialized as a string *)
    let constructors_without_arguments_as_string = true 

    (* Emit all values, even if they match the default values in ([@default x]) tag *)
    let omit_default_values = false (* Fields with tag [@default x], where the x matches the given value *)

    (* Lazy value are deserialised only when forced. Note that forcing the value may raise an exception,
       if the subtype could not be deserialised *) 
    let eager = false

    (* Silently ignore extra fields in records *)
    let strict = false 
  end : Ppx_protocol_driver.Parameters)

type t = ...
[@@deriving protocol ~driver:(module MyJsonDriver)]

For up to date information, please see: Parameters documentation.

Recommendation

If you only need to changes a single parameter, you can use:

module MyJsonDriver = Json.Make(struct
  include Ppx_protocol_driver.Default_parameters
  let strict = true
end : Ppx_protocol_driver.Parameters) 
Clone this wiki locally