API Reference

serde_json

JSON serialization for Rust built on serde, converting typed values and dynamic Value trees.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

cargocargo add serde_json

Methods

JSON serialization for Rust built on serde, converting typed values and dynamic Value trees.

Overview

serde_json is the serde implementation for JSON: any type implementing Deserialize parses from strings, bytes, or readers, and Serialize writes compact or pretty JSON back. Errors carry line and column for pinpointing malformed input.

The Value enum handles untyped JSON, with the json! macro building trees from Rust expressions, and to_writer streams serialization without intermediate buffers. It pairs with serde derives for round-tripping typed APIs.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • from_str / from_slice / from_reader — Deserializes JSON into any serde type, reporting line and column on syntax errors.
  • to_string / to_vec / to_writer — Serializes serde types to a string, bytes, or directly into an io writer.
  • Value / json! macro — Dynamic JSON tree for untyped payloads; json! builds literals from Rust expressions.
  • Error details — Parse errors expose location and category for precise API diagnostics.

Usage Notes

  • Prefer typed structs at API boundaries; Value only for genuinely unshaped data.
  • Use to_writer for large responses to avoid buffering whole documents in memory.
  • Report error.line and column to clients when echoing their own payloads.

Common Pitfalls

  • Value indexing returns Null silently on missing keys; type checks, as_u64, are still needed.
  • Deny unknown fields via serde defaults to acceptance; opt in deliberately when strictness matters.
  • Parsing from_reader consumes the stream; re-reading requires buffering it yourself.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://docs.rs/serde_json · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:27.155Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Error handling is verbose to implement correctly, for example imagine trying to detect the presence of unrecognized fields in the input data. (https://docs.rs/serde_json)
  • Error This type represents all possible errors that can occur when serializing or deserializing JSON data. (https://docs.rs/serde_json)
  • Type Aliases § Result Alias for a Result with the error type serde_json::Error . (https://docs.rs/serde_json)

FAQ

Value or typed structs, which should I use?
Typed structs validate shape and convert types once, and are the right default for payloads you own. Value suits probing unknown JSON, partial reads, or schema-flexible tools; you pay with manual null and type handling everywhere.
How do I serialize maps with non-string keys?
serde_json requires string keys per the JSON spec; serialize HashMap<K, V> by enabling map keys that implement Display through serde_with, or convert keys to strings before serialization.
Why does my number become i64 versus f64?
JSON numbers without a decimal point deserialize as integers, matching i64 or u64, and those with a dot become f64. Typed fields force the conversion; untyped Value picks the same rules, so 1 parses differently from 1.0.