From 216bace9d88c25641f11e3a2fee4a9c4cb06b271 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Wed, 11 Feb 2015 20:51:57 -0500 Subject: [PATCH] lib/json: converting JsonValue to float or int casts between numerics MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/json/dynamic.nit | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/json/dynamic.nit b/lib/json/dynamic.nit index 1ee3ce7..d0559e0 100644 --- a/lib/json/dynamic.nit +++ b/lib/json/dynamic.nit @@ -49,11 +49,17 @@ class JsonValue # Get this value as a `Int` # - # require: `self.is_int` + # require: `self.is_numeric` # # assert "-10".to_json_value.to_i == -10 # assert "123".to_json_value.to_i == 123 - fun to_i: Int do return value.as(Int) + # assert "123.456".to_json_value.to_i == 123 + fun to_i: Int + do + var value = value + assert value isa Numeric + return value.to_i + end # Is this value a float? # @@ -64,11 +70,17 @@ class JsonValue # Get this value as a `Float` # - # require: `self.is_float` + # require: `self.is_numeric` # # assert "0.0".to_json_value.to_f == 0.0 # assert "123.456".to_json_value.to_f == 123.456 - fun to_f: Float do return value.as(Float) + # assert "123".to_json_value.to_f == 123.0 + fun to_f: Float + do + var value = value + assert value isa Numeric + return value.to_f + end # Is the value numeric? # -- 1.7.9.5