package ppx_deriving_ezjsonm
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
sha256=315c02140658f548b580e7247db6e3f3cdb0d9a9681c21a6261a4bd410d41008
    
    
  sha512=950f765c9c221c2413e5d96f05dd14af1d64e218657041be22e9dcce30f2409a496a81661e84c9aed651b4a2444a0efc293ec2d306aa8b622ddd7d678a92cf20
    
    
  doc/index.html
Deriving JSON
This deriver generates code targetted at the Ezjsonm library. This shares a core type with the Yaml library meaning the deriver works in exactly the same way as ppx_deriving_yaml.
The derivers favour usefulness over efficiency.
A Simple Example
To use the library, add a preprocessing stanza to your dune library.
(preprocess
  (pps
   ppx_deriving_ezjsonm))So the documentation can include checked code examples, we first must require the deriver.
# #require "ppx_deriving_ezjsonm";;From there, you can annotate your type declarations with [@@deriving ezjsonm]. By default this will generate two functions, of_yaml and to_yaml. If the type is not called t, the type's name will be prepended to these functions separated by a single hyphen.
# module Person : sig
    type t [@@deriving ezjsonm]
  val set_name : t -> string -> t
  end = struct 
    type t = {
      name : string;
      age : int;
    }[@@deriving ezjsonm]
    let set_name t name = { t with name }
  end;;
module Person :
  sig
    type t
    val to_ezjsonm : t -> Ezjsonm.value
    val of_ezjsonm : Ezjsonm.value -> (t, [> `Msg of string ]) result
    val set_name : t -> string -> t
  endYou can then use these functions in conjunction with the Ezjsonm libary to read, manipulate and write JSON values. For example, this little JSON value:
# let raw_json = "{\"name\": \"Alice\", \"age\": 42 }"
val raw_json : string = "{\"name\": \"Alice\", \"age\": 42 }"
# let p = Ezjsonm.value_from_string raw_json 
  |> Person.of_ezjsonm
  |> Result.get_ok;;
val p : Person.t = <abstr>Then we change the name of the person and convert back to JSON.
# Person.set_name p "Bob" |> Person.to_ezjsonm |> Ezjsonm.value_to_string;;
- : string = "{\"name\":\"Bob\",\"age\":42}"Attributes
For more information about the possible attributes, please see the documentation for ppx_deriving_yaml.