Merge branch 'feature/encoder' of github.com:derui/json-ocaml into feature/encoder
[from now] 2013/04/30 09:03:11

diff --git a/src/json_encoder.ml b/src/json_encoder.ml
index 758a4af..131f06b 100644
--- a/src/json_encoder.ml
+++ b/src/json_encoder.ml
@@ -11,7 +11,10 @@ let bool_to_json = function
 let string_to_json (s:string) : string = "\"" ^ s ^ "\""
 ;;

-let number_to_json (s:float) :string = string_of_float s
+let number_to_json (s:float) :string =
+  let f = string_of_float s in
+  let rindex = String.rindex f '.' in
+  if rindex = (pred |< String.length f) then f ^ "0" else f
 ;;

 let rec (encode_per_type : T.t -> string) = function
diff --git a/test/test_json_decode.ml b/test/test_json_decode.ml
index 479408e..ede1881 100644
--- a/test/test_json_decode.ml
+++ b/test/test_json_decode.ml
@@ -121,6 +121,8 @@ let test_float_digit _ =
   assert_float o 1.4e9;
   let o = J.decode "-1.5" in
   assert_float o (-1.5);
+  let o = J.decode "1." in
+  assert_float o 1.0
 ;;

 let test_constants _ =
diff --git a/test/test_json_encode.ml b/test/test_json_encode.ml
index ae87df9..67ef48a 100644
--- a/test/test_json_encode.ml
+++ b/test/test_json_encode.ml
@@ -19,9 +19,7 @@ let test_constants _ =
   let json = T.Bool(false) in
   assert_equal "false" (J.encode json);
   let json = T.Null in
-  assert_equal "null" (J.encode json);
-  let json = T.Number(1.45) in
-  assert_equal "1.45" (J.encode json)
+  assert_equal "null" (J.encode json)
 ;;

 let test_encode_number _ =
@@ -30,7 +28,7 @@ let test_encode_number _ =
   let json = T.Number(100.0) in
   assert_equal "100.0" (J.encode json);
   let json = T.Number(0.1234) in
-  assert_equal ".1234" (J.encode json);
+  assert_equal "0.1234" (J.encode json);
 ;;

 let test_encode_string _ =