Newtonsoft.Msgpack
Newtonsoft.Msgpack copied to clipboard
Serialize numeric types with smallest lossless format
What do you think about serializing numeric types with the smallest lossless format? For example, if I have a double[]
of length N containing 0's and 1's, its contents (after the header) would be stored using N bytes, because 0 and 1 fit losslessly into a positive fixnum. If I try to then deserialize to a double[]
, that would work.
msgpack-cli is already doing this for ints and uints internally.
The implementation of DoubleMsgPackValue.Pack
would become something logically similar to this:
public override void Pack(Packer packer)
{
if (this.Value == (int)this.Value)
{
packer.Pack(this.Value < 0 ? (int)this.Value : (uint)this.Value);
}
else if (this.Value == (float)this.Value)
{
packer.Pack((float)this.Value);
}
else
{
packer.Pack(this.Value);
}
}
What are your thoughts? This could potentially be an optional configuration on the MessagePackWriter. I'd be willing to help out with this.
I've actually tried this locally now, and it's working great. Deserialization appears to already handle this without any changes. That is, if you try to deserialize a uint fixnum
containing the value 0 (hex 00) into a C# double
that works correctly.
Hi,
The feature sounds nice, You are welcome to propose a pull request with your changes. I think it is preferred this feature can be configurable.
I encourage you to propose further changes/improvements to this library. This project could use a little love.