Ocsigen/Eliom tutorial以一个提供"Hello,world!“的应用程序示例开始。作为HTML:
open Eliom_content.Html5.D
let main_service =
Eliom_registration.Html5.register_service
~path:["graff"]
~get_params:Eliom_parameter.unit
(fun () () ->
Lwt.return
(html
(head (title (pcdata "Page title")) [])
(body [h1 [pcdata "Graffiti"]])))如何将其作为JSON使用呢?具体来说,如何注册JSON服务,以及应该使用什么库/组合器来生成/序列化JSON (js_of_ocaml?)?
发布于 2013-06-13 23:57:47
我不确定你想要做什么,但是,关于JSON,你可以使用“派生”(cf.JSON )通过使用OCaml类型创建Deriving_Json类型,如下所示:
type deriving_t = (string * string) deriving (Json)这将创建与OCaml类型相对应的JSON类型。
这里是使用这种类型与服务器通信的方法(如果你不知道服务器的功能,这里是关于它的documentation和关于服务器端的客户端值):
(* first you have to create a server function (this allowed the client to call a function from the server *)
let json_call =
server_function
Json.t<deriving_t>
(fun (a,b) ->
Lwt.return (print_endline ("log on the server: "^a^b)))
(* let say that distillery has already generate all the needed stuff (main_service, Foobar_app, etc..) *)
let () =
Foobar_app.register
~service:main_service
(fun () () ->
{unit{
(* here I call my server function by using ocaml types directly, it will be automatically serialize *)
ignore (%json_call ("hello", "world"))
}};
Lwt.return
(Eliom_tools.F.html
~title:"foobar"
~css:[["css";"foobar.css"]]
Html5.F.(body [
h2 [pcdata "Welcome from Eliom's distillery!"];
])))如果你想使用一些客户端/服务器通信,你应该看看Eliom_bus,Eliom_comet或者Eliom_react。
(抱歉,我不能创建超过2个链接:)但是您可以在ocsigen.org网站上找到文档)。
希望这能对你有所帮助。
发布于 2013-06-13 23:39:29
JSON如果您想要与客户端Eliom程序通信,则不需要将数据序列化为。任何OCaml类型的序列化/反序列化都是由Eliom自动完成的。只需使用OCaml服务(或者,更简单:服务器函数并从您的OCaml客户端程序调用函数)。
https://stackoverflow.com/questions/17076575
复制相似问题