charslaw / Pando

Pando serializes and tracks the history of a state tree
MIT License
2 stars 0 forks source link

Add truncating int primitive serializers #47

Open schultzcole opened 2 years ago

schultzcole commented 2 years ago

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