Yaml encoder/decoder for OCaml jsont codecs

fix integer encoding

Changed files
+21 -22
lib
tests
+8 -9
lib/yamlt.ml
···
(* Encode number *)
let encode_number e _meta f =
let value =
-
if Float.is_nan f then ".nan"
-
else if f = Float.infinity then ".inf"
-
else if f = Float.neg_infinity then "-.inf"
-
else
-
let s = Printf.sprintf "%.17g" f in
-
(* Ensure it looks like a number *)
-
if String.contains s '.' || String.contains s 'e' || String.contains s 'E'
-
then s
-
else s ^ ".0"
+
match Float.classify_float f with
+
| FP_nan -> ".nan"
+
| FP_infinite -> if f > 0.0 then ".inf" else "-.inf"
+
| _ ->
+
if Float.is_integer f && Float.abs f < 1e15 then
+
Printf.sprintf "%.0f" f
+
else
+
Printf.sprintf "%g" f
in
Emitter.emit e.emitter (Event.Scalar {
anchor = None;
+6 -6
tests/cram/arrays_codec.t
···
JSON: {"numbers":[1,2,3,4,5],"strings":["hello","world"]}
YAML Block:
numbers:
-
- 1.0
-
- 2.0
-
- 3.0
-
- 4.0
-
- 5.0
+
- 1
+
- 2
+
- 3
+
- 4
+
- 5
strings:
- hello
- world
-
YAML Flow: {numbers: [1.0, 2.0, 3.0, 4.0, 5.0], strings: [hello, world]}
+
YAML Flow: {numbers: [1, 2, 3, 4, 5], strings: [hello, world]}
================================================================================
NEGATIVE TESTS - Wrong File Types
+5 -5
tests/cram/formats_codec.t
···
YAML Block:
name: test
values:
-
- 1.0
-
- 2.0
-
- 3.0
+
- 1
+
- 2
+
- 3
nested:
enabled: true
-
count: 5.0
+
count: 5
YAML Flow:
-
{name: test, values: [1.0, 2.0, 3.0], nested: {enabled: true, count: 5.0}}
+
{name: test, values: [1, 2, 3], nested: {enabled: true, count: 5}}
================================================================================
+2 -2
tests/cram/objects_codec.t
···
JSON: {"name":"Alice","age":30,"active":true}
YAML Block:
name: Alice
-
age: 30.0
+
age: 30
active: true
-
YAML Flow: {name: Alice, age: 30.0, active: true}
+
YAML Flow: {name: Alice, age: 30, active: true}
================================================================================
NEGATIVE TESTS - Wrong File Types