Skills2026年5月4日·1 分钟阅读

Serde — High-Performance Serialization Framework for Rust

The standard Rust framework for serializing and deserializing data structures efficiently and generically across formats like JSON, TOML, YAML, MessagePack, and more.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Serde Overview
通用 CLI 安装命令
npx tokrepo install 9ca9871a-47d7-11f1-9bc6-00163e2b0d79

Introduction

Serde is the de facto serialization framework for Rust, used by virtually every Rust project that handles structured data. Its derive macros generate zero-overhead serialization code at compile time, and its generic data model lets a single struct work with JSON, TOML, YAML, bincode, and dozens of other formats without code changes.

What Serde Does

  • Generates Serialize and Deserialize implementations via derive macros with no runtime reflection
  • Supports a wide range of formats through independent crate adapters (serde_json, toml, serde_yaml, etc.)
  • Handles complex types including enums with data, nested structs, maps, optional fields, and custom types
  • Provides field-level attributes for renaming, skipping, flattening, and defaulting during deserialization
  • Achieves zero-copy deserialization for formats that support borrowing from the input buffer

Architecture Overview

Serde separates the data model from the format implementation. The Serialize and Deserialize traits define how a type maps to Serde's abstract data model of 29 types. Format crates implement Serializer and Deserializer traits to convert between the data model and a specific wire format. The derive macro generates trait implementations at compile time, producing code that the compiler can fully inline and optimize, resulting in performance comparable to hand-written serialization.

Self-Hosting & Configuration

  • Add serde with the derive feature to your Cargo.toml
  • Add a format crate like serde_json, toml, or serde_yaml as needed
  • Annotate structs and enums with #[derive(Serialize, Deserialize)]
  • Use #[serde(...)] attributes to customize field names, defaults, and skip conditions
  • For no-std environments, disable the default std feature in Cargo.toml

Key Features

  • Compile-time code generation with zero runtime overhead
  • Format-agnostic: one struct definition works with any supported format
  • Rich attribute system for field renaming, flattening, tagging, and custom logic
  • Zero-copy deserialization for high-performance parsing of large payloads
  • Ecosystem of 100+ community format crates from CBOR to Avro

Comparison with Similar Tools

  • encoding/json (Go) — uses runtime reflection; Serde generates code at compile time for better performance
  • Jackson (Java) — annotation-driven with runtime processing; Serde achieves similar flexibility at zero runtime cost
  • Protobuf (prost) — schema-first binary format; Serde is format-agnostic and works with human-readable formats
  • rkyv — Rust zero-copy archive format; Serde supports more formats and is the ecosystem standard
  • bincode — binary format crate that uses Serde; it is a format backend, not a competing framework

FAQ

Q: Does Serde work in no-std environments? A: Yes. Disable the std feature in Cargo.toml and use alloc or fully no-alloc deserializers.

Q: How do I rename fields for JSON output? A: Use #[serde(rename = "fieldName")] on individual fields or #[serde(rename_all = "camelCase")] on the struct.

Q: Can I deserialize untagged enums? A: Yes. Use #[serde(untagged)] and Serde will try each variant in order until one succeeds.

Q: What is the performance overhead? A: Near zero. The derive macro generates specialized code that the compiler inlines, matching hand-written serialization in benchmarks.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产