···
(* Jsont codec for User - handles both user_id and id fields *)
-
match Jsont_bytesrw.decode_string' Jsont.json s with
-
| Error _ -> Error "Failed to decode JSON"
-
| Jsont.Object (fields, _) ->
-
let assoc = List.map (fun ((k, _), v) -> (k, v)) fields in
-
match List.assoc_opt "user_id" assoc with
-
| Some (Jsont.Number (f, _)) -> Some (int_of_float f)
-
match List.assoc_opt "id" assoc with
-
| Some (Jsont.Number (f, _)) -> Some (int_of_float f)
-
match List.assoc_opt "email" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
match List.assoc_opt "full_name" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
match List.assoc_opt "short_name" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
(match (user_id, email, full_name) with
-
| (Some user_id, Some email, Some full_name) ->
-
(* Keep unknown fields *)
-
let unknown_fields = List.filter (fun (k, _) ->
-
k <> "user_id" && k <> "id" && k <> "email" && k <> "full_name" && k <> "short_name"
-
let unknown_mems = List.map (fun (k, v) -> ((k, Jsont.Meta.none), v)) unknown_fields in
-
let unknown = Jsont.Object (unknown_mems, Jsont.Meta.none) in
-
Ok { user_id; email; full_name; short_name; unknown }
-
| _ -> Error "Missing required user fields")
-
| _ -> Error "Expected JSON object for user"
-
let to_string { user_id; email; full_name; short_name; unknown } =
-
(("user_id", Jsont.Meta.none), Jsont.Number (float_of_int user_id, Jsont.Meta.none));
-
(("email", Jsont.Meta.none), Jsont.String (email, Jsont.Meta.none));
-
(("full_name", Jsont.Meta.none), Jsont.String (full_name, Jsont.Meta.none));
-
let fields = match short_name with
-
| Some sn -> (("short_name", Jsont.Meta.none), Jsont.String (sn, Jsont.Meta.none)) :: fields
-
let unknown_mems = match unknown with
-
| Jsont.Object (mems, _) -> mems
-
let json = Jsont.Object (fields @ unknown_mems, Jsont.Meta.none) in
-
match Jsont_bytesrw.encode_string' Jsont.json json with
-
| Error e -> failwith ("Failed to encode user: " ^ Jsont.Error.to_string e)
-
Jsont.of_of_string ~kind:"User" of_string ~enc:to_string
let of_json (json : Zulip.json) : (t, Zulip.zerror) result =
match Zulip.Encode.from_json jsont json with
···
(* Jsont codec for Reaction - handles user_id in different locations *)
-
match Jsont_bytesrw.decode_string' Jsont.json s with
-
| Error _ -> Error "Failed to decode JSON"
-
| Jsont.Object (fields, _) ->
-
let assoc = List.map (fun ((k, _), v) -> (k, v)) fields in
-
match List.assoc_opt "emoji_name" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
match List.assoc_opt "emoji_code" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
match List.assoc_opt "reaction_type" assoc with
-
| Some (Jsont.String (s, _)) -> Some s
-
(* user_id can be either directly in the object or nested in a "user" field *)
-
match List.assoc_opt "user_id" assoc with
-
| Some (Jsont.Number (f, _)) -> Some (int_of_float f)
-
match List.assoc_opt "user" assoc with
-
| Some (Jsont.Object (user_fields, _)) ->
-
let user_assoc = List.map (fun ((k, _), v) -> (k, v)) user_fields in
-
(match List.assoc_opt "user_id" user_assoc with
-
| Some (Jsont.Number (f, _)) -> Some (int_of_float f)
-
(match (emoji_name, emoji_code, reaction_type, user_id) with
-
| (Some emoji_name, Some emoji_code, Some reaction_type, Some user_id) ->
-
(* Keep unknown fields *)
-
let unknown_fields = List.filter (fun (k, _) ->
-
k <> "emoji_name" && k <> "emoji_code" && k <> "reaction_type" && k <> "user_id" && k <> "user"
-
let unknown_mems = List.map (fun (k, v) -> ((k, Jsont.Meta.none), v)) unknown_fields in
-
let unknown = Jsont.Object (unknown_mems, Jsont.Meta.none) in
-
Ok { emoji_name; emoji_code; reaction_type; user_id; unknown }
-
| _ -> Error "Missing required reaction fields")
-
| _ -> Error "Expected JSON object for reaction"
-
let to_string { emoji_name; emoji_code; reaction_type; user_id; unknown } =
-
(("emoji_name", Jsont.Meta.none), Jsont.String (emoji_name, Jsont.Meta.none));
-
(("emoji_code", Jsont.Meta.none), Jsont.String (emoji_code, Jsont.Meta.none));
-
(("reaction_type", Jsont.Meta.none), Jsont.String (reaction_type, Jsont.Meta.none));
-
(("user_id", Jsont.Meta.none), Jsont.Number (float_of_int user_id, Jsont.Meta.none));
-
let unknown_mems = match unknown with
-
| Jsont.Object (mems, _) -> mems
-
let json = Jsont.Object (fields @ unknown_mems, Jsont.Meta.none) in
-
match Jsont_bytesrw.encode_string' Jsont.json json with
-
| Error e -> failwith ("Failed to encode reaction: " ^ Jsont.Error.to_string e)
-
Jsont.of_of_string ~kind:"Reaction" of_string ~enc:to_string
let of_json (json : Zulip.json) : (t, Zulip.zerror) result =
match Zulip.Encode.from_json jsont json with