Hitchhiker's Guide to JSON Data in Rust 🦀 Serialize and Deserialize with Serde 🗺️ Rust Tutorial

preview_player
Показать описание
The Rust ecosystem has developed a crate called "serde" that enables you to generate JSON strings from your Rust data structures, and vice versa. Serde works with many different data formats besides JSON as well, depending on what kind of services you're developing, or interacting with from the client side. Using Serde is very easy, by annotating your Rust structs with the Serialize and Deserialize macros, and then calling the appropriate conversion function from the data format's crate. If there's a data format not supported by Serde, you can develop your own implementation for that format, on top of Serde core. In this video, we'll take a look at some of the fundamental concepts behind Serde and working with JSON data.

Please follow me on these other social channels!

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.

#rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer
Рекомендации по теме
Комментарии
Автор

Great tutorial, @Trevor. Thank you for making videos!

I think it's helpful to mention we can access JSON in a similar fashion as python dictionaries (as key-value pairs). We can do untyped serde where we don't have to create structs that match our JSON.

Example JSON:
{
"artists": {"names": ["x", "y"]}
}

parsing function:
use serde_json::Value;

fn untyped_serde() {

let data = r#"
{
"artists": {"names": ["x", "y"]}
}
"#;

// Parse the string of data into serde_json::Value.
let value: Value =

// Access parts of the data by indexing with square brackets.
let artist_names = &value["artists"]["names"];
println!("artist_names: {}", artist_names[0]);

// to convert the data for "names" key to a vec
let names_deser = artist_names
.as_array().unwrap()
.iter()
.map(|x|
.collect::<Vec<_>>();

assert_eq!(names_deser, vec!["x".to_string(), "y".to_string()]);
println!("names_deser: {:#?}", names_deser);
}

fn main() {
untyped_serde();
}

I think this was feels more familiar to how we have json in other languages and we also don't have to create several structs as json files can be very nested :)

natnaelberhane
Автор

Thanks for your content🥰
It was very helpful and FUN.
I want to dive in Rust badly.

Kite
Автор

Great content, -put me on the right track, thanks!

mutantthegreat
Автор

please make a video over the Super, Self, Crate keyword?

ImranKhan-brdv
Автор

One of the issues Im facing with rust is trying new crates. Once I open a crate, there are many options and things, most of the time I dont even find what I want. Im not sure if you would be able to support with that, but how would you recommend to do, if I want to learn a new library (assuming no tutorials are there for it).

ExidifulCrypto
Автор

I have structure:

#[derive(Serialize, Deserialize)]
#[allow(dead_code)]
pub struct KDTree <'a>{
root: KDTreeNode,
state: HashMap<&'a str, usize>
}

and believe me, deserialization became a nightmare there, because it asks for 'de special lifetime that must be associated with 'a for state lifetime representation in order to work properly.

Автор

Following this example seems to not work anymore. I get `cannot find type Error in this scope` when I tried. The error message suggests importing core::error:Error, core::fmt::Error, serde::__private::Error, or serde::__private::fmt::Error. I've tried all 4 and still can't get it to work. I'm very new to Rust, not sure how to fix this. EDIT: I fixed it. Just had to add "use serde_json::Error;"

mcfincher
Автор

Hello. What's the add-on that let's you show the errors at the end of the line like in the video?
14:25

laifsyn
Автор

Thanks for the reply Trevor! Unfortunately my original message does not appear to be visible here any more. The email notification had the message all squashed up, so it was difficult to check what you meant!

carlcaulkett
Автор

please create some rust project videos with tauri, react or tauri, nextjs.

dipankarpaul
Автор

i32 for birth year is triggering me for some reason.

smoothemoveexlax
Автор

Hey Trevor your content is top notch, no doubt.

But please keep the theme consistent. Switch from dark theme in vs code to light thme website really hurts eyes of viewers.
Please consider this because I have to watch your every future videos and I would really want them in dark theme.
Thank you 😊

pathakvivek