Yaml encoder/decoder for OCaml jsont codecs

fix integer encoding

+21 -22
+8 -9
lib/yamlt.ml
··· 650 650 (* Encode number *) 651 651 let encode_number e _meta f = 652 652 let value = 653 - if Float.is_nan f then ".nan" 654 - else if f = Float.infinity then ".inf" 655 - else if f = Float.neg_infinity then "-.inf" 656 - else 657 - let s = Printf.sprintf "%.17g" f in 658 - (* Ensure it looks like a number *) 659 - if String.contains s '.' || String.contains s 'e' || String.contains s 'E' 660 - then s 661 - else s ^ ".0" 653 + match Float.classify_float f with 654 + | FP_nan -> ".nan" 655 + | FP_infinite -> if f > 0.0 then ".inf" else "-.inf" 656 + | _ -> 657 + if Float.is_integer f && Float.abs f < 1e15 then 658 + Printf.sprintf "%.0f" f 659 + else 660 + Printf.sprintf "%g" f 662 661 in 663 662 Emitter.emit e.emitter (Event.Scalar { 664 663 anchor = None;
+6 -6
tests/cram/arrays_codec.t
··· 109 109 JSON: {"numbers":[1,2,3,4,5],"strings":["hello","world"]} 110 110 YAML Block: 111 111 numbers: 112 - - 1.0 113 - - 2.0 114 - - 3.0 115 - - 4.0 116 - - 5.0 112 + - 1 113 + - 2 114 + - 3 115 + - 4 116 + - 5 117 117 strings: 118 118 - hello 119 119 - world 120 - YAML Flow: {numbers: [1.0, 2.0, 3.0, 4.0, 5.0], strings: [hello, world]} 120 + YAML Flow: {numbers: [1, 2, 3, 4, 5], strings: [hello, world]} 121 121 122 122 ================================================================================ 123 123 NEGATIVE TESTS - Wrong File Types
+5 -5
tests/cram/formats_codec.t
··· 77 77 YAML Block: 78 78 name: test 79 79 values: 80 - - 1.0 81 - - 2.0 82 - - 3.0 80 + - 1 81 + - 2 82 + - 3 83 83 nested: 84 84 enabled: true 85 - count: 5.0 85 + count: 5 86 86 87 87 YAML Flow: 88 - {name: test, values: [1.0, 2.0, 3.0], nested: {enabled: true, count: 5.0}} 88 + {name: test, values: [1, 2, 3], nested: {enabled: true, count: 5}} 89 89 90 90 91 91 ================================================================================
+2 -2
tests/cram/objects_codec.t
··· 119 119 JSON: {"name":"Alice","age":30,"active":true} 120 120 YAML Block: 121 121 name: Alice 122 - age: 30.0 122 + age: 30 123 123 active: true 124 - YAML Flow: {name: Alice, age: 30.0, active: true} 124 + YAML Flow: {name: Alice, age: 30, active: true} 125 125 126 126 ================================================================================ 127 127 NEGATIVE TESTS - Wrong File Types