biniou
biniou copied to clipboard
Portability issue with float serialization
Several people have tried to use Yojson, which depends on Biniou, with Js_of_ocaml. However, Biniou uses an unportable hack to serialize floats. In particular, the expression "String.unsafe_get (Obj.magic 1.0) 0" is unconditionnally executed and fails when compiled to Javascript. Maybe the function Int64.of_float could be used instead to access the binary representation of floats?
The reason for this choice was performance, but maybe the gain is not that significant. Some benchmarks are needed.
FYI, I submitted a feature request some time ago without much of a conclusion; feel free to add your insights: http://caml.inria.fr/mantis/view.php?id=5494
I made the following lazification:
let float_endianness = lazy (
match String.unsafe_get (Obj.magic 1.0) 0 with
'\x3f' -> `Big
| '\x00' -> `Little
| _ -> assert false
)
It's the simplest solution I can think of. I hope it solves the Yojson/Js_of_ocaml problem.
Thanks a lot!
Unfortunately, this is still not quite enough, as there is a piece of code a bit later in the same file (around line 256) that calls read_untagged_float64 and write_untagged_float64 unconditionally to test whether they work properly...
Oops. This one is now fixed (tag v.1.0.5). Hopefully it's the last one.