Serialization / Deserialization
You probably don't want to hand-type your list of users and recompile every time users change! You might use a local passwords file, or even a database. In this section, we'll look at how to serialize and deserialize data to and from a file.
The code for this is in
login_lib_json
andlogin_json
.
Dependencies
Serde is the de-facto standard serialization/deserialization library. It's very flexible, and can be used to serialize to and from JSON, XML, YAML, and more. We'll use JSON here.
The first thing to do is to add some dependencies to your auth
project.
You need the serde
crate, with the feature derive
. Run:
cargo add serde -F derive
You also need serde_json
:
cargo add serde_json
These commands make your Cargo.toml
file look like this:
[package]
name = "login_lib_json"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
Making Data Serializable
Import the Serialize
and Deserialize
macros:
Then decorate your types with #[derive(Serialize, Deserialize)]
:
The macros write all the hard code for you. The only requirement is that every type you are including must also support Serialize and Deserialize. You can implement traits and write the serialization by hand if you prefer - but it's very verbose.
Now let's change our "get_users" system to work with a JSON file.
Serializing to JSON
First, we add a get_default_users
function. If there isn't a users file, we'll use this to make one:
Next, let's change the get_users
function to look for a users.json
file and see if it exists:
That's all there is to creating a JSON file! We use serde_json::to_string
to convert our users
HashMap into a JSON string, and then write it to the file. Run the program, and users.json
will appear:
{"bob":{"username":"bob","password":"password","role":"User"},"admin":{"username":"admin","password":"password","role":"Admin"}}
Deserializing from JSON
Let's extend the get_users
function to read from users.json
if it exists:
Equally simple - you load the file, deserialize it with serde_json::from_str
, and you're done! You can now edit the JSON file, and your changes will be loaded when a user tries to login.
Let's change admin's password to password2
and test it.