I need to deserialize raw binary data (BinaryFormatter), then serialize into JSON (for editing) and then serialize it back into binary again.
Obviously, I lose on floats. Original float value 0xF9FF4FC1
(big endian, roughly -12.9999933) gets rounded to 0xF6FF4FC1
(-12.99999) when I serialize from original binary (correct data and intermediated data is 1:1 in memory) to JSON. I know it is not a big loss and I know floats are problematic but I want to keep the precision as close as possible due to possible incompatibility issues later.
Anyone tackled this problem before with JSON? How can I force it to write float with max precision? I've tried built in option for handling floats as either decimal or double but there is no difference in output, unfortunately, and I cant change target values because they still need to be written as floats when I do binary serialization so there will be rounding regardless during implicit conversion.
The specific type containing floats I am trying to round-trip is Vector2
from https://github.com/FNA-XNA/FNA/blob/master/src/Vector2.cs.
tl:dr have a float, want JsonNET serialize it as precise as possible into final json string.
P.S. I'e read tons of questions here and blog entries elsewhere but haven't found anyone trying to solve the same issue, most of the search hits were with float-reading issues (which I'm gonna need to solve later on too).
UPDATE: As @dbc below pointed out - Jsont.NET respects "TypeConverter" attribute thus I had to make my own converter that overrides it.
https://github.com/FNA-XNA/FNA/blob/c77c82837af89e28e9e71106a3637236c215ada3/src/Vector2.cs
--- is there a way to bypass it/ignore?