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

#599 in Web programming

Download history 14133/week @ 2024-08-08 11441/week @ 2024-08-15 18497/week @ 2024-08-22 12499/week @ 2024-08-29 14943/week @ 2024-09-05 11270/week @ 2024-09-12 15156/week @ 2024-09-19 24038/week @ 2024-09-26 13204/week @ 2024-10-03 21259/week @ 2024-10-10 24717/week @ 2024-10-17 18851/week @ 2024-10-24 20212/week @ 2024-10-31 16676/week @ 2024-11-07 52291/week @ 2024-11-14 15169/week @ 2024-11-21

108,953 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
~27K SLoC