From: Prashanth Mundkur Date: Fri, 15 May 2009 17:06:41 +0000 (-0700) Subject: fix missing support for floats in json_conv X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=eee06e987ff804ac6182c195e111a6720a0fab4d;p=xenclient%2Ftoolstack.git fix missing support for floats in json_conv --- diff --git a/libs/json/json_conv.ml b/libs/json/json_conv.ml index 991d3d8..78db876 100644 --- a/libs/json/json_conv.ml +++ b/libs/json/json_conv.ml @@ -69,6 +69,7 @@ let int_of_json ?(permissive=false) j = | Json.Null -> 0 | Json.Bool b -> if b then 1 else 0 | Json.Int i -> Int64.to_int i + | Json.Float f -> int_of_float f | Json.Array a -> if Array.length a = 0 then raise_unexpected_json_type "array" "int" @@ -88,6 +89,7 @@ let int64_of_json ?(permissive=false) j = | Json.Null -> 0L | Json.Bool b -> if b then 1L else 0L | Json.Int i -> i + | Json.Float f -> Int64.of_float f | Json.Array a -> if Array.length a = 0 then raise_unexpected_json_type "array" "int64" @@ -98,6 +100,25 @@ let int64_of_json ?(permissive=false) j = let int64_to_json i = Json.Int i +let float_of_json ?(permissive=false) j = + let strict = function + | Json.Float f -> f + | _ -> raise_unexpected_json_type (Json.string_of_type j) "float" in + let lenient = function + | Json.Null -> 0.0 + | Json.Bool b -> if b then 1.0 else 0.0 + | Json.Int i -> Int64.to_float i + | Json.Float f -> f + | Json.Array a -> + if Array.length a = 0 then + raise_unexpected_json_type "array" "float" + else + strict a.(0) + | _ -> raise_unexpected_json_type (Json.string_of_type j) "float" in + if not permissive then strict j else lenient j + +let float_to_json f = Json.Float f + let bool_of_json ?(permissive=false) j = let strict = function | Json.Bool b -> b @@ -106,6 +127,7 @@ let bool_of_json ?(permissive=false) j = | Json.Null -> false | Json.Bool b -> b | Json.Int i -> i <> 0L + | Json.Float f -> f <> 0.0 | Json.Array a -> if Array.length a = 0 then raise_unexpected_json_type "array" "bool" diff --git a/libs/json/json_conv.mli b/libs/json/json_conv.mli index 921d221..8f2c74d 100644 --- a/libs/json/json_conv.mli +++ b/libs/json/json_conv.mli @@ -35,6 +35,9 @@ val int_to_json: int -> Json.t val int64_of_json: ?permissive:bool -> Json.t -> int64 val int64_to_json: int64 -> Json.t +val float_of_json: ?permissive:bool -> Json.t -> float +val float_to_json: float -> Json.t + val bool_of_json: ?permissive:bool -> Json.t -> bool val bool_to_json: bool -> Json.t