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

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024
  • 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.
    🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
    📖 Clap Docs 📦➡️ docs.rs/clap/l...
    Visual Studio Code ➡️ code.visualstu...
    Rust Website ➡️ rust-lang.org
    Rustup Installer ➡️ rustup.rs
    Rust Docs ➡️ doc.rust-lang....
    Please follow me on these other social channels!
    ➡️ trevorsullivan...
    ➡️ github.com/pcg...
    ➡️ / pcgeek86
    ➡️ / trevorsullivan
    ➡️ / trevorsoftware
    ➡️ tiktok.com/pcg...
    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

ความคิดเห็น • 42

  • @natnaelberhane3141
    @natnaelberhane3141 5 หลายเดือนก่อน +4

    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 = serde_json::from_str(data).unwrap();
    // 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| x.as_str().unwrap().to_string())
    .collect::();
    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 :)

  • @ExidifulCrypto
    @ExidifulCrypto 11 หลายเดือนก่อน

    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).

    • @TrevorSullivan
      @TrevorSullivan  11 หลายเดือนก่อน

      I think that's something every developer struggles with. Each open source library out there implements things differently, so you have to learn the nuances of each one.
      That's why having solid documentation is so important for open source libraries. Some reference documentation, some example-driven, some conceptual, etc.

    • @TrevorSullivan
      @TrevorSullivan  11 หลายเดือนก่อน

      I definitely plan on having more videos covering some common third party crates. Thanks for sharing your feedback!

  • @736939
    @736939 3 หลายเดือนก่อน +1

    I have structure:
    #[derive(Serialize, Deserialize)]
    #[allow(dead_code)]
    pub struct KDTree
    }
    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.

    • @dschledermann
      @dschledermann 2 หลายเดือนก่อน

      It breaks if serde has to manipulate the string in any way. I tried it with some XML deserializing where I got an error similar to yours. The two solutions that worked was either using String (which of course is a complete abandonment of the no-copy approach you seem to be trying) or Cow('a, str).

  • @glq-xz9de
    @glq-xz9de 9 หลายเดือนก่อน +3

    This is helpful and clear! Thanks!

  • @amanfreecs
    @amanfreecs 2 หลายเดือนก่อน +1

    Great Brother!!!!! Worth channel found for Rust

  • @777wsu
    @777wsu ปีที่แล้ว +3

    Thanks Trevor!

  • @mutantthegreat7963
    @mutantthegreat7963 6 หลายเดือนก่อน +1

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

  • @valentinkaminskiy3826
    @valentinkaminskiy3826 2 หลายเดือนก่อน

    if let Ok() {} esle {}

  • @tuliomgui
    @tuliomgui 6 หลายเดือนก่อน +1

    Very very helpfull, thanks a lot

  • @chronxdev
    @chronxdev 9 หลายเดือนก่อน +2

    Awesome stuff!

  • @nhwhn
    @nhwhn 5 หลายเดือนก่อน +1

    awesome content, thank you

  • @_azterisk
    @_azterisk 4 หลายเดือนก่อน +1

    Very clear- thanks

  • @ImranKhan-br5dv
    @ImranKhan-br5dv 5 หลายเดือนก่อน +1

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

    • @TrevorSullivan
      @TrevorSullivan  5 หลายเดือนก่อน

      Great idea for a video!

  • @liminal6823
    @liminal6823 9 หลายเดือนก่อน +1

    Excellent. Thank you.

  • @piyushpatil6874
    @piyushpatil6874 5 หลายเดือนก่อน +1

    Awesomeeeee ❤

  • @hos7012
    @hos7012 11 หลายเดือนก่อน +1

    thx

  • @Scotthutchinsonking
    @Scotthutchinsonking 2 หลายเดือนก่อน

    Great tutorials on Rust , thank you sir

  • @smoothemoveexlax
    @smoothemoveexlax 5 หลายเดือนก่อน

    i32 for birth year is triggering me for some reason.

    • @TrevorSullivan
      @TrevorSullivan  5 หลายเดือนก่อน

      Hmmmmm. What's wrong with a signed integer? I'd probably use negative values for BC. Are you suggesting using an i16 instead?

    • @smoothemoveexlax
      @smoothemoveexlax 5 หลายเดือนก่อน +1

      @@TrevorSullivan It's best to find the optimal data type. i16 is good for years unless you need to represent BC. u8 for month, u8 for day.

  • @Kite70986
    @Kite70986 4 หลายเดือนก่อน

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

    • @TrevorSullivan
      @TrevorSullivan  4 หลายเดือนก่อน +1

      Thanks for your kind comment! Start writing it today! 🦀🦀

  • @mcfincher29
    @mcfincher29 8 หลายเดือนก่อน

    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;"

    • @TrevorSullivan
      @TrevorSullivan  8 หลายเดือนก่อน

      I'm glad you got it sorted out!

  • @laifsyn5347
    @laifsyn5347 7 หลายเดือนก่อน

    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

    • @TrevorSullivan
      @TrevorSullivan  7 หลายเดือนก่อน

      That's called the Rust Analyzer extension. Check out my video that talks about setting up your development environment for Rust! It's covered in there.

    • @laifsyn5347
      @laifsyn5347 7 หลายเดือนก่อน

      I seem unable to find the video @@TrevorSullivan

    • @TrevorSullivan
      @TrevorSullivan  7 หลายเดือนก่อน

      @@laifsyn5347 check out the first video in this playlist th-cam.com/play/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL.html&si=AsdrFx_Vuo9ehrA9

  • @carlcaulkett3050
    @carlcaulkett3050 9 หลายเดือนก่อน

    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!

    • @TrevorSullivan
      @TrevorSullivan  9 หลายเดือนก่อน

      You're welcome! Did my suggestion work?

    • @TrevorSullivan
      @TrevorSullivan  9 หลายเดือนก่อน

      I don't know why your comment is gone. I don't think I deleted it, unless it was an accident.

    • @carlcaulkett3050
      @carlcaulkett3050 9 หลายเดือนก่อน +1

      @@TrevorSullivan I managed to get it working with this...
      use serde::{Deserialize, Serialize};
      use serde_json::to_string;
      #[derive(Serialize, Deserialize)]
      struct Cat {
      name: String,
      year_born: i32,
      }
      fn main() {
      let cat01: Cat = Cat{name: "Cathy".to_string(), year_born: 1976};
      let cat_ser = to_string(&cat01);
      if cat_ser.is_ok() {
      println!("{}", cat_ser.ok().unwrap())
      }
      }

    • @TrevorSullivan
      @TrevorSullivan  9 หลายเดือนก่อน

      @@carlcaulkett3050 yup that looks exactly right! Previously the Result object was wrapping the standard Error type instead of the serde_json error type. Now that it's implied instead of explicit, the code is compiling correctly.

    • @carlcaulkett3050
      @carlcaulkett3050 9 หลายเดือนก่อน +1

      @@TrevorSullivan Thanks Trevor! The problem was that I'm using JetBrains RustRover. Where your copy of VS Code was showing the inlay hints for the return type, I thought that that was code that actually had to be typed in! As a wise man once said, Doh! 😉

  • @dipankarpaul3405
    @dipankarpaul3405 7 หลายเดือนก่อน +1

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

  • @pathakvivek7865
    @pathakvivek7865 9 หลายเดือนก่อน

    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 😊

    • @TrevorSullivan
      @TrevorSullivan  9 หลายเดือนก่อน

      Thank you for your support! I try to use dark themes whenever possible, but unfortunately I don't control the websites. If they don't offer a dark theme, I prefer to use their native colors instead of the Dark Reader extension. I agree with you, dark themes are important!

    • @pathakvivek7865
      @pathakvivek7865 9 หลายเดือนก่อน

      @@TrevorSullivan I would love you to use dark theme, more than that i would love you to be consistent in theme. Instantly changing brightness is the issues for me. Please try to use one theme whether dark or light.
      Thank you again.