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

#575 in Web programming

Download history 10384/week @ 2024-07-01 11363/week @ 2024-07-08 7408/week @ 2024-07-15 9067/week @ 2024-07-22 8073/week @ 2024-07-29 9613/week @ 2024-08-05 13619/week @ 2024-08-12 14724/week @ 2024-08-19 17807/week @ 2024-08-26 11805/week @ 2024-09-02 14617/week @ 2024-09-09 12164/week @ 2024-09-16 14257/week @ 2024-09-23 25688/week @ 2024-09-30 12197/week @ 2024-10-07 26113/week @ 2024-10-14

78,506 downloads per month
Used in 22 crates (13 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.5–1.2MB
~25K SLoC