#serialization #deserialize #serde #unit-testing #test

no-std dev serde_assert

Testing library for serde Serialize and Deserialize implementations

8 releases (breaking)

0.7.1 Dec 26, 2023
0.7.0 Dec 23, 2023
0.6.0 Nov 19, 2023
0.5.0 May 17, 2023
0.1.0 Jan 14, 2023

#157 in Encoding

Download history 1622/week @ 2024-02-25 1271/week @ 2024-03-03 1664/week @ 2024-03-10 1583/week @ 2024-03-17 1356/week @ 2024-03-24 1500/week @ 2024-03-31 1501/week @ 2024-04-07 3639/week @ 2024-04-14 6778/week @ 2024-04-21 7720/week @ 2024-04-28 6884/week @ 2024-05-05 6328/week @ 2024-05-12 7659/week @ 2024-05-19 6346/week @ 2024-05-26 7255/week @ 2024-06-02 9689/week @ 2024-06-09

31,185 downloads per month
Used in 4 crates




GitHub Workflow Status codecov.io crates.io docs.rs MSRV License

Testing library for serde Serialize and Deserialize implementations.

This library provides a Serializer and Deserializer to be used in writing unit tests to assert the behavior of manual Serialize and Deserialize implementations, respectively. The implementation behavior can be verified using a sequence of Tokens representing a generic serialized state.


The examples below use the claims crate for convenient assertions.

Testing Serialization

The Serializer returns a sequence of Tokens representing the serialization of a value. The returned Tokens can be checked to be equal to an expected value.

use claims::assert_ok_eq;
use serde::Serialize;
use serde_assert::{

let serializer = Serializer::builder().build();

assert_ok_eq!(true.serialize(&serializer), [Token::Bool(true)]);

Testing Deserialization

A Deserializer is constructed by providing a sequence of Tokens to be deserialized into a value.

use claims::assert_ok_eq;
use serde::Deserialize;
use serde_assert::{

let mut deserializer = Deserializer::builder([Token::Bool(true)]).build();

assert_ok_eq!(bool::deserialize(&mut deserializer), true);

Comparison with serde_test

This crate provides more flexibility than serde_test at the expense of more verbosity. While serde_test provides a small API of simple assertion functions, this crate will require you to call serialize() and deserialize() and assert yourself that the results are as expected.

While some users may find that the smaller API of serde_test is sufficient for their use-case, others will find that the flexibility of this crate makes testing more complicated Serailize and Deserialize implementations easier. Among other things, this crate's API provides these advantages:

  • Direct access to the Serializer and Deserializer, allowing use of all parts of the serde Serializer and Deserializer APIs, such as deserializing types that implement DeserializeSeed.
  • Customization of Serializers and Deserializers, allowing configuration of things like human-readability, whether the Deserializer should interpret sequences of Tokens as self-describing, and zero-copy deserialization.
  • Sophisticated comparison of serialized Token sequences, including allowing testing of types whose serialized form can include items in arbitrary order, such as when serializing a HashSet.

Minimum Supported Rust Version

This crate is guaranteed to compile on stable rustc 1.56.0 and up.


This project is licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.