Module serde_json

Source
Expand description

JSON serialization and deserialization.


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::Value from 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.
StreamDeserializer
Iterator that deserializes a stream into multiple JSON values.

Enums§

Value
Represents any valid JSON value.

Functions§

from_reader
Deserialize an instance of type T from an I/O stream of JSON.
from_slice
Deserialize an instance of type T from bytes of JSON text.
from_str
Deserialize an instance of type T from a string of JSON text.
from_value
Interpret a serde_json::Value as an instance of type T.
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 T into serde_json::Value which 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 Result with the error type serde_json::Error.