diff --git a/ppx/browser/ppx_deriving_json_runtime.ml b/ppx/browser/ppx_deriving_json_runtime.ml index d8a9df6..c8a031a 100644 --- a/ppx/browser/ppx_deriving_json_runtime.ml +++ b/ppx/browser/ppx_deriving_json_runtime.ml @@ -22,14 +22,14 @@ let of_string s = type error = Json.Decode.error = | Json_error of string - | Unpexpected_variant of string + | Unexpected_variant of string exception Of_json_error = Json.Decode.DecodeError let of_json_error msg = raise (Of_json_error (Json_error msg)) let unexpected_variant_error tag = - raise (Of_json_error (Unpexpected_variant tag)) + raise (Of_json_error (Unexpected_variant tag)) module To_json = struct external string_to_json : string -> t = "%identity" diff --git a/ppx/native/ppx_deriving_json_runtime.ml b/ppx/native/ppx_deriving_json_runtime.ml index 3c69bae..2243554 100644 --- a/ppx/native/ppx_deriving_json_runtime.ml +++ b/ppx/native/ppx_deriving_json_runtime.ml @@ -10,7 +10,7 @@ let of_string s = try Yojson.Basic.from_string s with Yojson.Json_error msg -> raise (Of_string_error msg) -type error = Json_error of string | Unpexpected_variant of string +type error = Json_error of string | Unexpected_variant of string exception Of_json_error of error diff --git a/ppx/test/ppx_deriving_json_js.t b/ppx/test/ppx_deriving_json_js.t index 83802cd..220ba76 100644 --- a/ppx/test/ppx_deriving_json_js.t +++ b/ppx/test/ppx_deriving_json_js.t @@ -507,7 +507,7 @@ else raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a \ @@ -564,10 +564,10 @@ | e -> (e :> [ `A | `B of int | other ]) | exception Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant _) -> + (Unexpected_variant _) -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a \ @@ -626,7 +626,7 @@ else raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a \ @@ -684,7 +684,7 @@ else raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a string" @@ -798,7 +798,7 @@ else raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a \ @@ -915,7 +915,7 @@ else raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) else Ppx_deriving_json_runtime.of_json_error "expected a non empty JSON array with element being a \ diff --git a/ppx/test/ppx_deriving_json_native.t b/ppx/test/ppx_deriving_json_native.t index 8e58d0d..5de23c9 100644 --- a/ppx/test/ppx_deriving_json_native.t +++ b/ppx/test/ppx_deriving_json_native.t @@ -384,7 +384,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> sum) let _ = sum_of_json @@ -430,7 +430,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> sum2) let _ = sum2_of_json @@ -464,7 +464,7 @@ | x -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> other) let _ = other_of_json @@ -495,10 +495,10 @@ | x -> (x :> [ `A | `B of int | other ]) | exception Ppx_deriving_json_runtime.Of_json_error - (Ppx_deriving_json_runtime.Unpexpected_variant _) -> + (Ppx_deriving_json_runtime.Unexpected_variant _) -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant"))) + (Unexpected_variant "unexpected variant"))) : Yojson.Basic.t -> poly) let _ = poly_of_json @@ -534,7 +534,7 @@ | x -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> poly2) let _ = poly2_of_json @@ -568,7 +568,7 @@ | x -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) let _ = c_of_json @@ -598,7 +598,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> recur) let _ = recur_of_json @@ -633,7 +633,7 @@ | x -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> polyrecur) let _ = polyrecur_of_json @@ -668,7 +668,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> evar) let _ = evar_of_json @@ -703,7 +703,7 @@ | x -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> epoly) let _ = epoly_of_json @@ -738,7 +738,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) let _ = p2_of_json @@ -845,7 +845,7 @@ | _ -> raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant")) + (Unexpected_variant "unexpected variant")) : Yojson.Basic.t -> allow_extra_fields2) let _ = allow_extra_fields2_of_json diff --git a/ppx/tools/ppx_deriving_tools.ml b/ppx/tools/ppx_deriving_tools.ml index 673425e..c79babc 100644 --- a/ppx/tools/ppx_deriving_tools.ml +++ b/ppx/tools/ppx_deriving_tools.ml @@ -437,7 +437,7 @@ module Conv = struct ( [%expr raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant"))], + (Unexpected_variant "unexpected variant"))], [] ) ~f:(fun (next, cases) (c, r) -> let ctx = Vcs_ctx_polyvariant c in @@ -466,7 +466,7 @@ module Conv = struct | e -> (e :> [%t t]) | exception Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant _) -> + (Unexpected_variant _) -> [%e next]] in next, cases) @@ -510,7 +510,7 @@ module Conv = struct --> [%expr raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant"))]; + (Unexpected_variant "unexpected variant"))]; ] ~f:(fun next (c : constructor_declaration) -> let ctx = Vcs_ctx_variant c in @@ -563,7 +563,7 @@ module Conv = struct [%expr raise (Ppx_deriving_json_runtime.Of_json_error - (Unpexpected_variant "unexpected variant"))] + (Unexpected_variant "unexpected variant"))] ~f:(fun next (n, ts) -> let maybe = self#derive_type_ref ~loc self#name n ts x @@ -574,7 +574,7 @@ module Conv = struct | x -> (x :> [%t t]) | exception Ppx_deriving_json_runtime.Of_json_error - (Ppx_deriving_json_runtime.Unpexpected_variant + (Ppx_deriving_json_runtime.Unexpected_variant _) -> [%e next]]) in diff --git a/src/Json_decode.ml b/src/Json_decode.ml index 159f3a0..5abdf9b 100644 --- a/src/Json_decode.ml +++ b/src/Json_decode.ml @@ -1,21 +1,21 @@ -external _unsafeCreateUninitializedArray : int -> 'a array = "Array" [@@mel.new] +external _unsafeCreateUninitializedArray : int -> 'a array = "Array" +[@@mel.new] + external _stringify : Js.Json.t -> string = "JSON.stringify" let _isInteger value = Js.Float.isFinite value && Js.Math.floor_float value == value type 'a decoder = Js.Json.t -> 'a - -type error = Json_error of string | Unpexpected_variant of string +type error = Json_error of string | Unexpected_variant of string let error_to_string = function | Json_error msg -> msg - | Unpexpected_variant tag -> "unexpected variant: " ^ tag + | Unexpected_variant tag -> "unexpected variant: " ^ tag exception DecodeError of error let error msg = raise (DecodeError (Json_error msg)) - let id json = json let bool json = @@ -32,7 +32,8 @@ let int json = else error ("Expected integer, got " ^ _stringify json) let string json = - if Js.typeof json = "string" then (Obj.magic (json : Js.Json.t) : string) + if Js.typeof json = "string" then + (Obj.magic (json : Js.Json.t) : string) else error ("Expected string, got " ^ _stringify json) let char json = @@ -60,7 +61,10 @@ let array decode json = let value = try decode (Array.unsafe_get source i) with DecodeError err -> - error (error_to_string err ^ "\n\tin array at index " ^ string_of_int i) + error + (error_to_string err + ^ "\n\tin array at index " + ^ string_of_int i) in Array.unsafe_set target i value done; @@ -77,7 +81,8 @@ let pair decodeA decodeB json = try ( decodeA (Array.unsafe_get source 0), decodeB (Array.unsafe_get source 1) ) - with DecodeError err -> error (error_to_string err ^ "\n\tin pair/tuple2") + with DecodeError err -> + error (error_to_string err ^ "\n\tin pair/tuple2") else let length = Js.String.make length in error {j|Expected array of length 2, got array of length $length|j} @@ -94,7 +99,8 @@ let tuple3 decodeA decodeB decodeC json = ( decodeA (Array.unsafe_get source 0), decodeB (Array.unsafe_get source 1), decodeC (Array.unsafe_get source 2) ) - with DecodeError err -> error (error_to_string err ^ "\n\tin tuple3") + with DecodeError err -> + error (error_to_string err ^ "\n\tin tuple3") else let length = Js.String.make length in error {j|Expected array of length 3, got array of length $length|j} @@ -110,7 +116,8 @@ let tuple4 decodeA decodeB decodeC decodeD json = decodeB (Array.unsafe_get source 1), decodeC (Array.unsafe_get source 2), decodeD (Array.unsafe_get source 3) ) - with DecodeError err -> error (error_to_string err ^ "\n\tin tuple4") + with DecodeError err -> + error (error_to_string err ^ "\n\tin tuple4") else let length = Js.String.make length in error {j|Expected array of length 4, got array of length $length|j} @@ -130,7 +137,8 @@ let dict decode json = let key = Array.unsafe_get keys i in let value = try decode (Js.Dict.unsafeGet source key) - with DecodeError err -> error (error_to_string err ^ "\n\tin dict") + with DecodeError err -> + error (error_to_string err ^ "\n\tin dict") in Js.Dict.set target key value done; @@ -158,19 +166,23 @@ let rec at key_path decoder = | first :: rest -> field first (at rest decoder) | [] -> raise - @@ Invalid_argument "Expected key_path to contain at least one element" + @@ Invalid_argument + "Expected key_path to contain at least one element" -let optional decode json = try Some (decode json) with DecodeError _ -> None +let optional decode json = + try Some (decode json) with DecodeError _ -> None let oneOf decoders json = let rec inner decoders errors = match decoders with | [] -> let formattedErrors = - "\n- " ^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors)) + "\n- " + ^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors)) in - error ({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j} - ^ _stringify json) + error + ({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j} + ^ _stringify json) | decode :: rest -> ( try decode json with DecodeError e -> inner rest (e :: errors)) in diff --git a/src/Json_decode.mli b/src/Json_decode.mli index 2e8d59d..e2dc3e1 100644 --- a/src/Json_decode.mli +++ b/src/Json_decode.mli @@ -13,7 +13,7 @@ third-party libraries. type 'a decoder = Js.Json.t -> 'a (** The type of a decoder combinator *) -type error = Json_error of string | Unpexpected_variant of string +type error = Json_error of string | Unexpected_variant of string val error_to_string : error -> string