Expand description
JSON serialization and deserialization.
- Crate
::serde_json. - docs.rs
- crates.io
- GitHub
serde_json provides JSON serialization and deserialization
for Rust data structures using the serde framework.
It supports converting between Rust types and JSON text,
with both strongly-typed and loosely-typed approaches.
The main functions are to_string and from_str
for basic JSON serialization and deserialization.
For more control, use to_writer and from_reader
to work with I/O streams,
or to_value and from_value to work with
the generic Value type that can represent any JSON data.
The Value enum can hold any JSON value
and is useful for dynamic JSON manipulation
when you don’t know the structure at compile time.
§Examples
Serializing and deserializing structured data:
use serde::{Deserialize, Serialize};
use serde_json::{to_string, from_str};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let person = Person {
name: "Alice".to_string(),
age: 30,
};
// Serialize to JSON string
let json = to_string(&person).unwrap();
println!("JSON: {}", json); // {"name":"Alice","age":30}
// Deserialize back from JSON
let parsed: Person = from_str(&json).unwrap();
assert_eq!(person, parsed);Working with dynamic JSON using Value:
use serde_json::{Value, json};
// Create JSON using the json! macro
let data = json!({
"name": "Bob",
"hobbies": ["reading", "coding"],
"active": true
});
// Access values dynamically
if let Some(name) = data["name"].as_str() {
println!("Name: {}", name);
}
if let Some(hobbies) = data["hobbies"].as_array() {
println!("Has {} hobbies", hobbies.len());
}Modules§
- de
- Deserialize JSON data to a Rust data structure.
- error
- When serializing or deserializing JSON goes wrong.
- map
- A map of String to serde_json::Value.
- ser
- Serialize a Rust data structure into JSON data.
- value
- The Value enum, a loosely typed way of representing any valid JSON value.
Macros§
- json
- Construct a
serde_json::Valuefrom a JSON literal.
Structs§
- Deserializer
- A structure that deserializes JSON into Rust values.
- Error
- This type represents all possible errors that can occur when serializing or deserializing JSON data.
- Map
- Represents a JSON key/value type.
- Number
- Represents a JSON number, whether integer or floating point.
- Serializer
- A structure for serializing Rust values into JSON.
- Stream
Deserializer - Iterator that deserializes a stream into multiple JSON values.
Enums§
- Value
- Represents any valid JSON value.
Functions§
- from_
reader - Deserialize an instance of type
Tfrom an I/O stream of JSON. - from_
slice - Deserialize an instance of type
Tfrom bytes of JSON text. - from_
str - Deserialize an instance of type
Tfrom a string of JSON text. - from_
value - Interpret a
serde_json::Valueas an instance of typeT. - to_
string - Serialize the given data structure as a String of JSON.
- to_
string_ pretty - Serialize the given data structure as a pretty-printed String of JSON.
- to_
value - Convert a
Tintoserde_json::Valuewhich is an enum that can represent any valid JSON data. - to_vec
- Serialize the given data structure as a JSON byte vector.
- to_
vec_ pretty - Serialize the given data structure as a pretty-printed JSON byte vector.
- to_
writer - Serialize the given data structure as JSON into the I/O stream.
- to_
writer_ pretty - Serialize the given data structure as pretty-printed JSON into the I/O stream.
Type Aliases§
- Result
- Alias for a
Resultwith the error typeserde_json::Error.