js_of_ocaml and Deriving_Json
Asked Answered
B

1

6

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).

Bathyscaphe answered 24/7, 2013 at 0:44 Comment(0)
H
3

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.

Hardan answered 25/7, 2013 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.