5 releases

0.5.0 Apr 28, 2024
0.1.3 Feb 16, 2023
0.1.2 Nov 11, 2022
0.1.1 Jul 8, 2022
0.1.0 May 19, 2022

#587 in Web programming

Download history 9004/week @ 2024-07-21 8260/week @ 2024-07-28 9624/week @ 2024-08-04 13518/week @ 2024-08-11 14656/week @ 2024-08-18 17896/week @ 2024-08-25 11737/week @ 2024-09-01 14809/week @ 2024-09-08 12154/week @ 2024-09-15 13818/week @ 2024-09-22 25886/week @ 2024-09-29 11521/week @ 2024-10-06 26686/week @ 2024-10-13 20351/week @ 2024-10-20 23870/week @ 2024-10-27 13405/week @ 2024-11-03

85,086 downloads per month
Used in 22 crates (12 directly)

MPL-2.0 license

21KB
415 lines

Base64 Url Safe Serde Wrapper

A wrapper to help inline deserialisation of base64 datatypes.


lib.rs:

Wrappers for Vec<u8> to make Serde serialise and deserialise as URL-safe, non-padded Base64 (per RFC 4648 §5).

Serialisation behaviour

By comparison, Serde's default behaviour is to serialise Vec<u8> as a sequence of integers. This is a problem for many formats:

  • serde_cbor encodes as an array, rather than a bytes. This uses zig-zag encoded integers for values > 0x1F, which averages about 1.88 bytes per byte assuming an equal distribution of values.

  • serde_json encodes as an Array<Number>, which averages 3.55 bytes per byte without whitespace.

Using Base64 encoding averages 1.33 bytes per byte, and most formats pass strings nearly-verbatim.

Deserialisation behaviour

Both types will deserialise multiple formats, provided the format is self-describing (ie: implements deserialize_any):

Migrating from Base64UrlSafeData to HumanBinaryData

Base64UrlSafeData always uses Base64 encoding, which isn't optimal for many binary formats. For that reason, it's a good idea to migrate to HumanBinaryData if you're using a binary format.

However, you'll need to make sure all readers using Base64UrlSafeData are on base64urlsafedata v0.1.4 or later before switching anything to HumanBinaryData. Otherwise, they'll not be able to read any data in the new format!

Once they're all migrated across, you can start issuing writes in the new format. It's a good idea to slowly roll out the change, in case you discover something has been left behind.

Alternatives

Dependencies

~0.6–1.2MB
~26K SLoC