Commits

camlspotter committed 16cdc26

dev

  • Participants
  • Parent commits baf9bbb
  • Branches dev

Comments (0)

Files changed (2)

File lib/json_conv.ml

 open Printf
 
 type 'a decoder = ('a, Json.t) Meta_conv.Types.Decoder.t
+type 'a decoder_exn = ('a, Json.t) Decoder.t_exn
 exception Error of Json.t Error.t
 
 let errorf v adrs fmt = 
 
 let lazy_t_of_json f = generic_lazy_t_of (fun e -> raise (Error e)) f
 let mc_lazy_t_of_json = generic_mc_lazy_t_of 
+
+type 'a named_list = (string * 'a) list
+
+let json_of_named_list enc xs = Object (List.map (fun (name, a) -> (name, enc a)) xs)
+
+(* CR jfuruse: this should be available in Meta_conv *)
+let list_mapi f xs =
+  List.rev (fst (List.fold_left (fun (rev,i) x ->
+    (f x i :: rev), i+1) ([],0) xs))
+
+let named_list_of_json dec ?(adrs=[]) xs =
+  let string_a_decoder : (string * 'a) decoder = fun ?adrs (name, a) -> 
+    match dec a with
+    | `Ok v -> `Ok (name, v)
+    | `Error e -> `Error e
+  in
+  generic_list_of 
+    (function Object xs -> Some xs | _ -> None)
+    string_a_decoder

File lib/json_conv.mli

 end
 
 type 'a decoder = ('a, Json.t) Decoder.t
+type 'a decoder_exn = ('a, Json.t) Decoder.t_exn
 exception Error of Json.t Error.t
 
 val string_of_json    : string decoder
 val option_of_json    : 'a decoder -> 'a option decoder
 val lazy_t_of_json    : 'a decoder -> 'a lazy_t decoder
 val mc_lazy_t_of_json : 'a decoder -> ('a, Json.t) mc_lazy_t decoder
+
+type 'a named_list = (string * 'a) list
+
+val json_of_named_list : ('a -> Json.t) -> 'a named_list -> Json.t
+val named_list_of_json : 'a decoder -> 'a named_list decoder
+val named_list_of_json_exn : 'a decoder -> 'a named_list decoder_exn
+
+