Module serde_json
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 ;
use ;
let person = Person ;
// Serialize to JSON string
let json = to_string.unwrap;
println!; // {"name":"Alice","age":30}
// Deserialize back from JSON
let parsed: Person = from_str.unwrap;
assert_eq!;
Working with dynamic JSON using Value:
use ;
// Create JSON using the json! macro
let data = json!;
// Access values dynamically
if let Some = data.as_str
if let Some = data.as_array
Modules
Macros
-
json
Construct a
serde_json::Valuefrom a JSON literal.