+0
日增长
5,672
stars
737
forks
36
watchers
562
prs
799
issues
184
releases
README
Serde JSON   [![Build Status]][actions] [![Latest Version]][crates.io]
[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/json/ci.yml?branch=master
[actions]: https://github.com/serde-rs/json/actions?query=branch%3Amaster
[Latest Version]: https://img.shields.io/crates/v/serde_json.svg
[crates.io]: https://crates.io/crates/serde\_json
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
---
toml
[dependencies]
serde_json = "1.0"
You may be looking for:
- JSON API documentation
- Serde API documentation
- Detailed documentation about Serde
- [Setting up [derive(Serialize, Deserialize)]](https://serde.rs/derive.html)
- Release notes
JSON is a ubiquitous open-standard format that uses human-readable text to
transmit data objects consisting of key-value pairs.
json
{
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
}
There are three common ways that you might find yourself needing to work with
JSON data in Rust.
- As text data. An unprocessed string of JSON data that you receive on an
HTTP endpoint, read from a file, or prepare to send to a remote server.
- As an untyped or loosely typed representation. Maybe you want to check
that some JSON data is valid before passing it on, but without knowing the
structure of what it contains. Or you want to do very basic manipulations
like insert a key in a particular spot.
- As a strongly typed Rust data structure. When you expect all or most of
your data to conform to a particular structure and want to get real work done
without JSON's loosey-goosey nature tripping you up.
Serde JSON provides efficient, flexible, safe ways of converting data between
each of these representations.
Operating on untyped JSON values
Any valid JSON data can be manipulated in the following recursive enum
representation. This data structure is [serde_json::Value][value].
rust
enum Value {
Null,
Bool(bool),
Number(Number),
String(String),
Array(Vec),
Object(Map),
}
A string of JSON data can be parsed into a serde_json::Value by the
[serde_json::from_str][from_str] function. There is also
[from_slice][from_slice] for parsing from a byte slice &[u8] and
[from_reader][from_reader] for parsing from any io::Read like a File or a
TCP stream.
rust
use serde_json::{Result, Value};
fn untyped_example() - Result {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}";
// Parse the string of data into serde_json::Value.
let v: Value = serde_json::from_str(data)?;
// Access parts of the data by indexing with square brackets.
pTop issues
#120SIMD vectorization behind a feature flag17 comments · 2016-07-18#313Serialize/Deserialize implementations are very slow to compile15 comments · 2017-05-02#239Generalize JSON macro to any serializer15 comments · 2017-02-01#404Provide a way for StreamDeserializer to stream an array of values13 comments · 2018-01-12#202Support deserializing f32 and f64 from null13 comments · 2017-01-19#537Document that serde_json doesn't escape forward slashes12 comments · 2019-04-27#740Deserialization of 128 bit integers fail when used with untagged variants10 comments · 2021-01-02#465PathBuf serialization can fail (non-streaming) or induce corruption (streaming) on encountering non-UTF8-encodable paths10 comments · 2018-07-18
Latest release
v1.0.154 v1.0.154 2026-10-11- Support preserve_order in no_std builds (1333, thanks @razmser)