camlspotter avatar camlspotter committed 16cdc26

dev

Comments (0)

Files changed (2)

 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

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
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.