It might sometimes be useful to have a serializer that serializes an int to fewer than 4 bytes.
As an example, StringSerializer takes in an int serializer to serialize the length of the string, but the length of the string is quite unlikely to actually reach int.MaxValue bytes. You could save bytes by using an int serializer that ignores the most significant bytes. For many cases, 1 or 2 bytes would be sufficient.
I think it would be implemented as a single class TruncatingIntSerializer, which can take a parameter of the number of bytes to use to serialize the value, and if an incoming value to serialize is larger than can be stored in that number of bytes, it throws an exception
It might sometimes be useful to have a serializer that serializes an
int
to fewer than 4 bytes.As an example,
StringSerializer
takes in an int serializer to serialize the length of the string, but the length of the string is quite unlikely to actually reachint.MaxValue
bytes. You could save bytes by using anint
serializer that ignores the most significant bytes. For many cases, 1 or 2 bytes would be sufficient.I think it would be implemented as a single class
TruncatingIntSerializer
, which can take a parameter of the number of bytes to use to serialize the value, and if an incoming value to serialize is larger than can be stored in that number of bytes, it throws an exception