Warning: Reason support is experimental. We are looking for beta-tester and contributors.

How to write a JSON service?

  • If you want to communicate with a client side Eliom program, you do not need to serialize yourself your data to JSON. Serialization/Deserialization of any OCaml type is done automatically by Eliom. Just use OCaml services (or, simpler: server functions and call the function from your OCaml client side program).
  • If you want to use your own JSON format, you need to have your own serialisation function to JSON (or for example to use some OCaml library like yojson to generate JSON). In that case, you can register your service using Eliom_registration.String instead of Eliom_registration.Html (see example below). The handler function must return the JSON value as a string, and a the content-type.
  • It is even possible to define yourself your own registration module, to be used instead of Eliom_registration.String. Thus, you can use an OCaml representation of the JSON value (and you don't call the serializer yourself). Have a look on how modules like Eliom_registration.String are implemented to know how to do that.
let my_json_service =
  Eliom_service.create
    ~path:(Eliom_service.Path ["my_json_service"])
    ~meth:(Eliom_service.Get Eliom_parameter.unit)
    ()

let _ =
  Eliom_registration.String.register
    ~service:my_json_service
    (fun () () ->
      Lwt.return ("{text:'something'}", "application/json"))

Comments:
If you want to get result from a Database, it could already be in JSON format.
Anyway, you probably do not need to pretty-print JSON yourself.