]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
fix missing support for floats in json_conv
authorPrashanth Mundkur <prashanth.mundkur@citrix.com>
Fri, 15 May 2009 17:06:41 +0000 (10:06 -0700)
committerPrashanth Mundkur <prashanth.mundkur@citrix.com>
Fri, 15 May 2009 17:06:41 +0000 (10:06 -0700)
libs/json/json_conv.ml
libs/json/json_conv.mli

index 991d3d8dad3f77251ebade93087f8e0ae5f33290..78db876356c2b505ec0a6e5b76bd054e76d0af54 100644 (file)
@@ -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"
index 921d2210babbf3c4b1e195ed1c266e8699b594d5..8f2c74d75e9244a6d4ed4cecfa3c80d7ac60ad68 100644 (file)
@@ -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