Skip to content

Latest commit

 

History

History

serde

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Serde crate for serialize/deserialize

https://crates.io/crates/serde

The Rust Serde crate is a widely used library for serialization and deserialization of Rust data structures to and from various data formats, such as JSON, TOML, YAML, and many others.

The Serde derive feature can automatically derive the serialization and deserialization code for Rust data structures, such as:

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u32,
}

This defines a Person struct with two fields: name is a String, and age is a u32. The #[derive(Serialize, Deserialize)] attribute tells Serde to automatically generate the serialization and deserialization code for this struct.

You can then use Serde to serialize an instance of this struct to JSON:

let person = Person { name: "Alice".to_string(), age: 30 };
let json = serde_json::to_string(&person).unwrap();

This creates a Person instance and serializes it to JSON using the serde_json::to_string function. The &person argument is a reference to the Person instance that you want to serialize.

You can also deserialize a JSON string into a Person instance:

let json = r#"{"name":"Bob","age":25}"#;
let person: Person = serde_json::from_str(json).unwrap();

This deserializes the json string into a Person instance using the serde_json::from_str function.