···779779 Result.map_error Jsont.Error.to_string
780780 (decode' ?layout ?locs ?file ?max_depth ?max_nodes t reader)
781781782782+let decode_string ?layout ?locs ?file ?max_depth ?max_nodes t s =
783783+ let reader = Bytesrw.Bytes.Reader.of_string s in
784784+ decode ?layout ?locs ?file ?max_depth ?max_nodes t reader
785785+786786+(* Convert Yamlrw.value to Jsont.json for type-driven decoding *)
787787+let rec value_to_json (v : Yamlrw.value) : Jsont.json =
788788+ let meta = Jsont.Meta.none in
789789+ match v with
790790+ | `Null -> Jsont.Null ((), meta)
791791+ | `Bool b -> Jsont.Bool (b, meta)
792792+ | `Float f -> Jsont.Number (f, meta)
793793+ | `String s -> Jsont.String (s, meta)
794794+ | `A items -> Jsont.Array (List.map value_to_json items, meta)
795795+ | `O fields ->
796796+ let mems =
797797+ List.map
798798+ (fun (k, v) -> ((k, meta), value_to_json v))
799799+ fields
800800+ in
801801+ Jsont.Object (mems, meta)
802802+803803+let decode_value' t v =
804804+ let json = value_to_json v in
805805+ Jsont.Json.decode' t json
806806+807807+let decode_value t v =
808808+ Result.map_error Jsont.Error.to_string (decode_value' t v)
809809+782810(* Encoder *)
783811784812type encoder = {
+24
lib/yamlt.mli
···104104 ('a, Jsont.Error.t) result Seq.t
105105(** [decode_all'] is like {!val-decode_all} but preserves the error structure. *)
106106107107+val decode_string :
108108+ ?layout:bool ->
109109+ ?locs:bool ->
110110+ ?file:Jsont.Textloc.fpath ->
111111+ ?max_depth:int ->
112112+ ?max_nodes:int ->
113113+ 'a Jsont.t ->
114114+ string ->
115115+ ('a, string) result
116116+(** [decode_string t s] decodes a value from YAML string [s] according to
117117+ type [t]. This is a convenience wrapper around {!val-decode}. *)
118118+119119+val decode_value : 'a Jsont.t -> Yamlrw.value -> ('a, string) result
120120+(** [decode_value t v] decodes a value from a pre-parsed {!Yamlrw.value}
121121+ according to type [t].
122122+123123+ This is useful when you have already parsed YAML into its JSON-compatible
124124+ representation (e.g., when using {!Yamlrw.of_string}) and want to decode
125125+ it using a Jsont codec without re-parsing the YAML text. *)
126126+127127+val decode_value' : 'a Jsont.t -> Yamlrw.value -> ('a, Jsont.Error.t) result
128128+(** [decode_value'] is like {!val-decode_value} but preserves the error
129129+ structure. *)
130130+107131(** {1:encode Encode} *)
108132109133(** YAML output format. *)