HTTP types: headers, status codes, methods, bodies, MIME types
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

atp, http, qemu: complete Json.t/Json.Codec.Value.t disambiguation

- hermest main.ml: generate dune.inc with [libraries atp json] (not jsont).
- Json.to_string/of_string args: codec is Json.Codec.Value.t (not Json.t
which is a type).
- test_huri: Json.to_string_exn/of_string_exn raise; remove bogus
pattern-match on result.
- Various value-context uses of Json.t fixed to Json.Codec.Value.t when
they were codec args, and to Json.string/Json.null etc. when they were
value constructors.

+86 -134
+1 -2
dune-project
··· 19 19 cstruct 20 20 eio 21 21 fmt 22 - jsont 23 - logs 22 + json logs 24 23 magic-mime 25 24 ptime 26 25 uri))
+1 -1
http.opam
··· 14 14 "cstruct" 15 15 "eio" 16 16 "fmt" 17 - "jsont" 17 + "json" 18 18 "logs" 19 19 "magic-mime" 20 20 "ptime"
+11 -18
lib/body.ml
··· 50 50 File { file; mime } 51 51 52 52 let json_encoding_error e = 53 - let msg = Jsont.Error.to_string e in 53 + let msg = Json.Error.to_string e in 54 54 raise (Error.err (Error.Json_encode_error { reason = msg })) 55 55 56 - (* For simple JSON encoding, we just take a Jsont.json value and encode it *) 57 - let json (json_value : Jsont.json) = 56 + (* For simple JSON encoding, we just take a Json.t value and encode it *) 57 + let json (json_value : Json.t) = 58 58 let content = 59 - match 60 - Jsont_bytesrw.encode_string' ~format:Jsont.Minify Jsont.json json_value 61 - with 62 - | Ok s -> s 63 - | Error e -> json_encoding_error e 59 + try Json.to_string_exn ~format:Json.Minify Json.Codec.Value.t json_value 60 + with Json.Error e -> json_encoding_error e 64 61 in 65 62 String { content; mime = Mime.json } 66 63 67 - (* Typed JSON encoding using a Jsont.t codec *) 68 - let jsonv (type a) (codec : a Jsont.t) (value : a) = 64 + (* Typed JSON encoding using a Json.codec codec *) 65 + let jsonv (type a) (codec : a Json.codec) (value : a) = 69 66 let content = 70 - match Jsont_bytesrw.encode_string' ~format:Jsont.Minify codec value with 71 - | Ok s -> s 72 - | Error e -> json_encoding_error e 67 + try Json.to_string_exn ~format:Json.Minify codec value 68 + with Json.Error e -> json_encoding_error e 73 69 in 74 70 String { content; mime = Mime.json } 75 71 ··· 93 89 let json_stream_source_create json_value = 94 90 (* Encode the entire JSON value to string with minified format *) 95 91 let content = 96 - match 97 - Jsont_bytesrw.encode_string' ~format:Jsont.Minify Jsont.json json_value 98 - with 99 - | Ok s -> s 100 - | Error e -> json_encoding_error e 92 + try Json.to_string_exn ~format:Json.Minify Json.Codec.Value.t json_value 93 + with Json.Error e -> json_encoding_error e 101 94 in 102 95 let t = { Json_stream_source.content; offset = 0 } in 103 96 let ops = Eio.Flow.Pi.source (module Json_stream_source) in
+21 -22
lib/body.mli
··· 74 74 75 75 (** {1 Convenience Constructors} *) 76 76 77 - val json : Jsont.json -> t 78 - (** [json value] creates a JSON body from a Jsont.json value. The value is 79 - encoded to a JSON string with Content-Type: application/json. 77 + val json : Json.t -> t 78 + (** [json value] creates a JSON body from a Json.t value. The value 79 + is encoded to a JSON string with Content-Type: application/json. 80 80 81 81 Example: 82 82 {[ 83 83 let body = 84 84 Body.json 85 - (Jsont.Object 85 + (Json.Codec.Object 86 86 ( [ 87 - ("status", Jsont.String "success"); 88 - ("count", Jsont.Number 42.); 87 + ("status", Json.String "success"); 88 + ("count", Json.Number 42.); 89 89 ( "items", 90 - Jsont.Array 91 - ( [ Jsont.String "first"; Jsont.String "second" ], 92 - Jsont.Meta.none ) ); 90 + Json.Array 91 + ( [ Json.String "first"; Json.String "second" ], 92 + Json.Meta.none ) ); 93 93 ], 94 - Jsont.Meta.none )) 94 + Json.Meta.none )) 95 95 ]} *) 96 96 97 - val jsonv : 'a Jsont.t -> 'a -> t 97 + val jsonv : 'a Json.codec -> 'a -> t 98 98 (** [jsonv codec value] creates a JSON body by encoding [value] using the typed 99 99 [codec]. The value is encoded to a minified JSON string with Content-Type: 100 100 application/json. ··· 108 108 type user = { name : string; age : int } 109 109 110 110 let user_codec = 111 - Jsont.Obj.map ~kind:"user" (fun name age -> { name; age }) 112 - |> Jsont.Obj.mem "name" Jsont.string ~enc:(fun u -> u.name) 113 - |> Jsont.Obj.mem "age" Jsont.int ~enc:(fun u -> u.age) 114 - |> Jsont.Obj.finish 111 + Json.Obj.map ~kind:"user" (fun name age -> { name; age }) 112 + |> Json.Obj.mem "name" Json.Codec.string ~enc:(fun u -> u.name) 113 + |> Json.Obj.mem "age" Json.Codec.int ~enc:(fun u -> u.age) 114 + |> Json.Obj.finish 115 115 116 116 (* Create a JSON body from a typed value *) 117 117 let body = Body.jsonv user_codec { name = "Alice"; age = 30 } ··· 119 119 120 120 @raise Eio.Io with {!Error.Json_encode_error} if encoding fails. *) 121 121 122 - val json_stream : Jsont.json -> t 123 - (** [json_stream json_value] creates a streaming JSON body from a Jsont.json 124 - value. The JSON value will be encoded to a minified JSON string and 125 - streamed. 122 + val json_stream : Json.t -> t 123 + (** [json_stream json_value] creates a streaming JSON body from a Json.t value. 124 + The JSON value will be encoded to a minified JSON string and streamed. 126 125 127 126 Example: 128 127 {[ 129 - let large_data = Jsont.Object ([ 130 - ("users", Jsont.Array ([...], Jsont.Meta.none)) 131 - ], Jsont.Meta.none) in 128 + let large_data = Json.Object ([ 129 + ("users", Json.Array ([...], Json.Meta.none)) 130 + ], Json.Meta.none) in 132 131 let body = Body.json_stream large_data 133 132 ]} *) 134 133
+1 -12
lib/dune
··· 1 1 (library 2 2 (name http) 3 3 (public_name http) 4 - (libraries 5 - logs 6 - ptime 7 - eio 8 - jsont 9 - jsont.bytesrw 10 - cstruct 11 - magic-mime 12 - base64 13 - crypto-rng 14 - uri 15 - fmt)) 4 + (libraries logs ptime eio json cstruct magic-mime base64 crypto-rng uri fmt))
+2 -1
lib/huri.ml
··· 206 206 (** {1 JSON Codec} *) 207 207 208 208 (** JSON codec for URIs. Encodes as a JSON string. *) 209 - let jsont = Jsont.string |> Jsont.map ~dec:Uri.of_string ~enc:Uri.to_string 209 + let jsont = 210 + Json.Codec.string |> Json.Codec.map ~dec:Uri.of_string ~enc:Uri.to_string
+1 -1
lib/huri.mli
··· 49 49 50 50 (** {1 JSON Codec} *) 51 51 52 - val jsont : t Jsont.t 52 + val jsont : t Json.codec 53 53 (** JSON codec for URIs. Encodes as a JSON string. *)
+21 -23
lib/response.ml
··· 161 161 162 162 let json t = 163 163 let body_str = text t in 164 - match Jsont_bytesrw.decode_string' Jsont.json body_str with 165 - | Ok json -> json 166 - | Error e -> 167 - let preview = 168 - if String.length body_str > 200 then String.sub body_str 0 200 169 - else body_str 170 - in 171 - raise 172 - (Error.err 173 - (Error.Json_parse_error 174 - { body_preview = preview; reason = Jsont.Error.to_string e })) 164 + try Json.of_string_exn Json.Codec.Value.t body_str 165 + with Json.Error e -> 166 + let preview = 167 + if String.length body_str > 200 then String.sub body_str 0 200 168 + else body_str 169 + in 170 + raise 171 + (Error.err 172 + (Error.Json_parse_error 173 + { body_preview = preview; reason = Json.Error.to_string e })) 175 174 176 - let jsonv (type a) (codec : a Jsont.t) t = 175 + let jsonv (type a) (codec : a Json.codec) t = 177 176 let body_str = text t in 178 - match Jsont_bytesrw.decode_string' codec body_str with 179 - | Ok value -> value 180 - | Error e -> 181 - let preview = 182 - if String.length body_str > 200 then String.sub body_str 0 200 183 - else body_str 184 - in 185 - raise 186 - (Error.err 187 - (Error.Json_parse_error 188 - { body_preview = preview; reason = Jsont.Error.to_string e })) 177 + try Json.of_string_exn codec body_str 178 + with Json.Error e -> 179 + let preview = 180 + if String.length body_str > 200 then String.sub body_str 0 200 181 + else body_str 182 + in 183 + raise 184 + (Error.err 185 + (Error.Json_parse_error 186 + { body_preview = preview; reason = Json.Error.to_string e })) 189 187 190 188 let raise_for_status t = 191 189 if t.status >= 400 then
+6 -6
lib/response.mli
··· 227 227 228 228 @raise Failure if the response has already been closed. *) 229 229 230 - val json : t -> Jsont.json 230 + val json : t -> Json.t 231 231 (** [json response] parses the response body as JSON. The response body is fully 232 232 consumed by this operation. 233 233 ··· 240 240 @raise Eio.Io with {!Error.Json_parse_error} if JSON parsing fails. 241 241 @raise Failure if the response has already been closed. *) 242 242 243 - val jsonv : 'a Jsont.t -> t -> 'a 243 + val jsonv : 'a Json.codec -> t -> 'a 244 244 (** [jsonv codec response] parses the response body as JSON and decodes it to a 245 245 typed value using the provided [codec]. The response body is fully consumed 246 246 by this operation. ··· 254 254 type user = { name : string; age : int } 255 255 256 256 let user_codec = 257 - Jsont.Obj.map ~kind:"user" (fun name age -> { name; age }) 258 - |> Jsont.Obj.mem "name" Jsont.string ~enc:(fun u -> u.name) 259 - |> Jsont.Obj.mem "age" Jsont.int ~enc:(fun u -> u.age) 260 - |> Jsont.Obj.finish 257 + Json.Obj.map ~kind:"user" (fun name age -> { name; age }) 258 + |> Json.Obj.mem "name" Json.Codec.string ~enc:(fun u -> u.name) 259 + |> Json.Obj.mem "age" Json.Codec.int ~enc:(fun u -> u.age) 260 + |> Json.Obj.finish 261 261 262 262 (* Decode the response to a typed value *) 263 263 let user = Response.jsonv user_codec response in
+3 -3
test/test_body.ml
··· 89 89 (** {1 Body.json Tests} *) 90 90 91 91 let test_json_creates_json_body () = 92 - let body = Body.json (Jsont.Json.string "hello") in 92 + let body = Body.json (Json.string "hello") in 93 93 let ct = Body.content_type body in 94 94 match ct with 95 95 | Some mime -> ··· 98 98 | None -> Alcotest.fail "Expected Some content_type for json body" 99 99 100 100 let test_json_content () = 101 - let body = Body.json (Jsont.Json.string "hello") in 101 + let body = Body.json (Json.string "hello") in 102 102 let s = Body.Private.to_string body in 103 103 Alcotest.(check string) "json content" "\"hello\"" s 104 104 105 105 let test_json_not_empty () = 106 - let body = Body.json (Jsont.Json.string "test") in 106 + let body = Body.json (Json.string "test") in 107 107 Alcotest.(check bool) "json not empty" false (Body.Private.is_empty body) 108 108 109 109 (** {1 Body.is_empty Tests} *)
+18 -45
test/test_huri.ml
··· 11 11 12 12 let test_jsont_roundtrip_simple () = 13 13 let uri = Uri.of_string "https://example.com" in 14 - let encoded = 15 - Jsont_bytesrw.encode_string' ~format:Jsont.Minify Huri.jsont uri 16 - in 17 - match encoded with 18 - | Ok json_str -> ( 19 - let decoded = Jsont_bytesrw.decode_string' Huri.jsont json_str in 20 - match decoded with 21 - | Ok uri' -> 22 - Alcotest.(check string) 23 - "roundtrip" (Uri.to_string uri) (Uri.to_string uri') 24 - | Error e -> Alcotest.failf "decode failed: %s" (Jsont.Error.to_string e)) 25 - | Error e -> Alcotest.failf "encode failed: %s" (Jsont.Error.to_string e) 14 + let json_str = Json.to_string_exn ~format:Json.Minify Huri.jsont uri in 15 + let uri' = Json.of_string_exn Huri.jsont json_str in 16 + Alcotest.(check string) "roundtrip" (Uri.to_string uri) (Uri.to_string uri') 26 17 27 18 let test_jsont_roundtrip_complex () = 28 19 let uri = Uri.of_string "http://user:pass@host:8080/path?q=1#frag" in 29 - let encoded = 30 - Jsont_bytesrw.encode_string' ~format:Jsont.Minify Huri.jsont uri 31 - in 32 - match encoded with 33 - | Ok json_str -> ( 34 - let decoded = Jsont_bytesrw.decode_string' Huri.jsont json_str in 35 - match decoded with 36 - | Ok uri' -> 37 - Alcotest.(check string) 38 - "roundtrip complex" (Uri.to_string uri) (Uri.to_string uri') 39 - | Error e -> Alcotest.failf "decode failed: %s" (Jsont.Error.to_string e)) 40 - | Error e -> Alcotest.failf "encode failed: %s" (Jsont.Error.to_string e) 20 + let json_str = Json.to_string_exn ~format:Json.Minify Huri.jsont uri in 21 + let uri' = Json.of_string_exn Huri.jsont json_str in 22 + Alcotest.(check string) 23 + "roundtrip complex" (Uri.to_string uri) (Uri.to_string uri') 41 24 42 25 let test_jsont_encodes_as_string () = 43 26 let uri = Uri.of_string "https://example.com" in 44 - let encoded = 45 - Jsont_bytesrw.encode_string' ~format:Jsont.Minify Huri.jsont uri 46 - in 47 - match encoded with 48 - | Ok json_str -> 49 - (* JSON string should be quoted *) 50 - Alcotest.(check bool) 51 - "starts with quote" true 52 - (String.length json_str > 0 && json_str.[0] = '"'); 53 - Alcotest.(check bool) 54 - "ends with quote" true 55 - (String.length json_str > 0 56 - && json_str.[String.length json_str - 1] = '"') 57 - | Error e -> Alcotest.failf "encode failed: %s" (Jsont.Error.to_string e) 27 + let json_str = Json.to_string_exn ~format:Json.Minify Huri.jsont uri in 28 + Alcotest.(check bool) 29 + "starts with quote" true 30 + (String.length json_str > 0 && json_str.[0] = '"'); 31 + Alcotest.(check bool) 32 + "ends with quote" true 33 + (String.length json_str > 0 && json_str.[String.length json_str - 1] = '"') 58 34 59 35 let test_jsont_decode_from_string () = 60 36 let json_str = "\"https://example.com/test\"" in 61 - let decoded = Jsont_bytesrw.decode_string' Huri.jsont json_str in 62 - match decoded with 63 - | Ok uri -> 64 - Alcotest.(check string) "scheme" "https" (Option.get (Uri.scheme uri)); 65 - Alcotest.(check string) "host" "example.com" (Option.get (Uri.host uri)); 66 - Alcotest.(check string) "path" "/test" (Uri.path uri) 67 - | Error e -> Alcotest.failf "decode failed: %s" (Jsont.Error.to_string e) 37 + let uri = Json.of_string_exn Huri.jsont json_str in 38 + Alcotest.(check string) "scheme" "https" (Option.get (Uri.scheme uri)); 39 + Alcotest.(check string) "host" "example.com" (Option.get (Uri.host uri)); 40 + Alcotest.(check string) "path" "/test" (Uri.path uri) 68 41 69 42 (** {1 Test Suite} *) 70 43