Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js_of_ocaml and Deriving_Json

I need some help to get js_of_ocaml working. There's not much information about it on the net, and the manual is very sparse (no snippets or usage examples, no comment sections).

I have a Card module on the server with a card record. I'm sending a card list to the client using Ajax, and there I want to read and traverse this list. What I end up with is this:

let json = Json.unsafe_input (Js.string http_frame.XmlHttpRequest.content) in

...where json has type 'a, according to documentation (not when I run it, of course).

I can log json##length and get the correct length of the list. Where do I go from here? Ideally, I'd like to use Deriving_Json to type-safe get a card list again, but I could also use a for-loop (not as elegant, but whatever).

like image 207
Olle Härstedt Avatar asked Jul 24 '13 00:07

Olle Härstedt


1 Answers

Type declarations like type name = ... deriving(Json) creates Json_name module. Here is example.

type card =  {a: int; b: string; } deriving(Json)
type t = card list deriving(Json)

let _ =
  let str = Json.to_string<t> [{a = 10; b = "abc";}; {a = 20; b = "xyz";}] in
  let rs = Json.from_string<t> str in
  Firebug.console##log(Js.string str);
  List.iter (fun r -> Firebug.console##log_2(r.a, Js.string r.b)) rs

And I'm not sure why, I got "Uncaught ReferenceError: unix_inet_addr_of_string is not defined".

So add function unix_inet_addr_of_string () {return 0;} to js file.

like image 133
zakki Avatar answered Oct 20 '22 23:10

zakki