You are confusing serde with serde-json. Serde is not a JSON serialization library. It is a generic, i.e. format-agnostic and data structure-agnostic serialization library. Here's what a traditional JSON library looks like:
+-------------------+
| Dynamically-typed |
| JSON value tree: | +--------------+ +----------------------+
| null, boolean, | -> | JSON encoder | -> | JSON string |
| numbers, string, | | (black box) | | {"foo": [42, "bar"]} |
| array, object | +--------------+ +----------------------+
+-------------------+
In contrast, here's what Serde wants to be:
+-------------------------------------------------+ Nope, still
Not Serde | Serde | Also not Serde not Serde
+-----------------------+ |+-----------------------+ +------------------+| +-------------------+ +----------+
| Strongly-typed custom | || Generic value tree | | || | Data Format | | |
| data structure, | -> || (optionals, booleans, | -> | Serializer trait || -> | (impl Serializer) | -> | Whatever |
| impl Serialize | || strings, sequences, … | | || | | | |
+-----------------------+ |+-----------------------+ +------------------+| +-------------------+ +----------+
+-------------------------------------------------+
Serde doesn't know how to serialize your own struct
s and enum
s and it also doesn't know what to serialize them to. Serde has no support for MyAwesomeCustomType
and it doesn't have support for JSON or YAML or Bincode or XML or BSON or MessagePack or ProtoBuf or ASN.1.
Serde is merely an abstraction layer that provides a unified interface (Serialize
and Deserialize
for data structures, and Serializer
and Deserializer
for data formats) and a simple tree-like type system which in turn allows arbitrary data structures and serialization formats to communicate. (It also provides derive macros for implementing Serialize
and Deserialize
on your own data types.) It doesn't do any actual serialization or deserialization itself.
The actual serialization and deserialization happens by data formats, which are implemented in separate crates. There is serde-json
for JSON, of course, but the other formats have their own crates, too. Bincode is one of them.
Bincode is not a less featureful version of serde, just like a drill bit is not a less featureful version of a drill. It is part of the drill-as-a-whole-equipment. The drill bit is useless in itself, because it needs an electric motor to turn it, but the motor is useless without the drill bit, because it can't do the actual drilling without the bit. Serde is the motor of the drill, and JSON and Bincode are two differently-shaped and differently-sized drill bits.
Serde can't do serialization in itself. Nor can bincode, nor can serde-json, nor can your own data structures by themselves. The data structures tell serde what to serialize (i.e. the tree of sequences, maps, and primitives, which are universally-recognized data types), and then serde passes on this knowledge to the particular serialization format, which then knows how to serialize it. Neither counts as complete serialization on its own, and all three parts of the system must work together in order to have useful serialized data come out at the end.