Yaml encoder/decoder for OCaml jsont codecs

yamlt: add decode_string and decode_value convenience functions

- decode_string: decode directly from a YAML string
- decode_value/decode_value': decode from pre-parsed Yamlrw.value

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

+52
+28
lib/yamlt.ml
··· 779 779 Result.map_error Jsont.Error.to_string 780 780 (decode' ?layout ?locs ?file ?max_depth ?max_nodes t reader) 781 781 782 + let decode_string ?layout ?locs ?file ?max_depth ?max_nodes t s = 783 + let reader = Bytesrw.Bytes.Reader.of_string s in 784 + decode ?layout ?locs ?file ?max_depth ?max_nodes t reader 785 + 786 + (* Convert Yamlrw.value to Jsont.json for type-driven decoding *) 787 + let rec value_to_json (v : Yamlrw.value) : Jsont.json = 788 + let meta = Jsont.Meta.none in 789 + match v with 790 + | `Null -> Jsont.Null ((), meta) 791 + | `Bool b -> Jsont.Bool (b, meta) 792 + | `Float f -> Jsont.Number (f, meta) 793 + | `String s -> Jsont.String (s, meta) 794 + | `A items -> Jsont.Array (List.map value_to_json items, meta) 795 + | `O fields -> 796 + let mems = 797 + List.map 798 + (fun (k, v) -> ((k, meta), value_to_json v)) 799 + fields 800 + in 801 + Jsont.Object (mems, meta) 802 + 803 + let decode_value' t v = 804 + let json = value_to_json v in 805 + Jsont.Json.decode' t json 806 + 807 + let decode_value t v = 808 + Result.map_error Jsont.Error.to_string (decode_value' t v) 809 + 782 810 (* Encoder *) 783 811 784 812 type encoder = {
+24
lib/yamlt.mli
··· 104 104 ('a, Jsont.Error.t) result Seq.t 105 105 (** [decode_all'] is like {!val-decode_all} but preserves the error structure. *) 106 106 107 + val decode_string : 108 + ?layout:bool -> 109 + ?locs:bool -> 110 + ?file:Jsont.Textloc.fpath -> 111 + ?max_depth:int -> 112 + ?max_nodes:int -> 113 + 'a Jsont.t -> 114 + string -> 115 + ('a, string) result 116 + (** [decode_string t s] decodes a value from YAML string [s] according to 117 + type [t]. This is a convenience wrapper around {!val-decode}. *) 118 + 119 + val decode_value : 'a Jsont.t -> Yamlrw.value -> ('a, string) result 120 + (** [decode_value t v] decodes a value from a pre-parsed {!Yamlrw.value} 121 + according to type [t]. 122 + 123 + This is useful when you have already parsed YAML into its JSON-compatible 124 + representation (e.g., when using {!Yamlrw.of_string}) and want to decode 125 + it using a Jsont codec without re-parsing the YAML text. *) 126 + 127 + val decode_value' : 'a Jsont.t -> Yamlrw.value -> ('a, Jsont.Error.t) result 128 + (** [decode_value'] is like {!val-decode_value} but preserves the error 129 + structure. *) 130 + 107 131 (** {1:encode Encode} *) 108 132 109 133 (** YAML output format. *)