Parsing JSON in Rust using serde and reqwest

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024
  • Today we will be discussing how to parse JSON in Rust using serde and reqwest!
    📝Get your FREE Rust cheat sheet :
    www.letsgetrus...
    code: github.com/let...

Комментарии • 78

  • @letsgetrusty
    @letsgetrusty  2 года назад +1

    📝Get your *FREE Rust cheat sheet* :
    www.letsgetrusty.com/cheatsheet

  • @MasterHigure
    @MasterHigure 2 года назад +21

    It isn't really tokio that lets you use the async await syntax. But it is the most commonly used wrapper around the machinery that rust expects when encountering them.

    • @cosmic2236
      @cosmic2236 2 года назад

      tokio is required for running future, so its fine i guess.

    • @CuriousSpy
      @CuriousSpy 2 года назад +5

      @@cosmic2236 any async runtime can run future

    • @cosmic2236
      @cosmic2236 2 года назад

      @@CuriousSpy yes, you are correct.

  • @alextrotta796
    @alextrotta796 2 года назад +12

    Funny timing that you release this now, I'm starting a project at work to incorporate Rust into my workplace's C++ codebase, and the task I'm doing to prove its worth is replacing (one of) our json format parsers with serde (and binding with cxx).
    So far, with very little work Serde does the same work (or better) than rapidjson in 2/3 the time, and that's with buffered input, if I read the whole file to a string first (no networking stuff, all local) it's 10x as fast. In either case it's looking to be a strong win for Rust.

    • @sledgex9
      @sledgex9 2 года назад

      Any comparison to Boost.Json?

    • @alextrotta796
      @alextrotta796 2 года назад

      @@sledgex9 Sadly I don't have any other comparisons, but I believe rapidjson is generally known to be one of the fastest, if not the fastest C++ json library. You can probably find benchmarks comparing various C++ parsing libraries easily though.

    • @sledgex9
      @sledgex9 2 года назад

      @@alextrotta796 I was asking because Boost.JSON is relatively new, and according to their benchmarks they are faster than rapidjson. I was just wondering if any other type of comparison was done for this project at your work.

    • @alextrotta796
      @alextrotta796 2 года назад +1

      @@sledgex9 Interesting, I actually hadn't heard of it until now, the big players are rapidjson (speed) and nlohmann (modern). We use boost already, so it would be a viable alternative, though we have to do validation via a schema, it doesn't look like boost json supports that, would have to do that manually. Most of the validation is simple stuff like "this is a length 3 array of floats" and "this field is required", stuff that is handled by serde trivially by just specifying a type.
      Static reflection in C++ can't come fast enough - it won't be able to truly compete with Rust in these types of workloads (without lots of hand tuning) until we have it.

  • @johnlombardo
    @johnlombardo 2 года назад +13

    Thank you! I’ve been struggling with this problem for far too long because the examples in the reqwest docs are incomplete fragments 😫

    • @sohn7767
      @sohn7767 2 года назад +2

      I mean you can figure this out yourself by reading the examples and documentation of the functions, but it’s definitely not easy

  • @jaysistar2711
    @jaysistar2711 2 года назад +8

    This is a good video for anybody trying to create a JSON over HTTP (REST) client.

  • @TheRealFallenDemon
    @TheRealFallenDemon 2 года назад +3

    One of the things that's tripped me up using Rust is the concept of unwrapping. Would love to see a video explaining it all

    • @----__---
      @----__--- 2 года назад +3

      thats the fundamental of error handling in rust. you should read the rust book.

    • @n000d13s
      @n000d13s 2 года назад +2

      unwrap just gets you whatever is inside Result and Option enums

    • @n000d13s
      @n000d13s 2 года назад +1

      Otherwise you need a match block.

    • @sohn7767
      @sohn7767 2 года назад

      Error handling. Simply unwrapping is usually not recommended

  • @oxey_
    @oxey_ 2 года назад +1

    I think code reviews similar to what the Cherno does for c++ could be a pretty interesting addition to your current content

  • @tobi5106
    @tobi5106 2 года назад +2

    If it would cover some error handling like running another function if the request fails it would be perfect.

    • @avalagum7957
      @avalagum7957 7 месяцев назад

      Did you mean circuit breaker?

  • @Dygear
    @Dygear 2 года назад +6

    This video is worth it's weight in gold.

    • @svhuwagv2965
      @svhuwagv2965 2 года назад +4

      Assuming one electron stores one bit of information uncompressed it is worth 1,99563 USD/g * 9,05*10^-28 g/e * 1e/bit * 24bit/px * 1920*1080 px/f * (7*60 + 16 ) s * 30 f/s = 4,898*10^-17 USD so basically 0 in any currency. Seems a bit low in my estimation :D

    • @thengakola6217
      @thengakola6217 2 года назад +4

      @@svhuwagv2965 what a chad

  • @kevinpiovezan
    @kevinpiovezan 2 года назад +4

    Awesome video!
    Is there any video linking Rust to a sql database?
    I’m watching the language book series and haven’t finished it yet rs

  • @sergiuoanes4635
    @sergiuoanes4635 2 года назад +1

    Aewsome video! I was looking for something like this. thank you

  • @AccordionMusicAndMore
    @AccordionMusicAndMore 3 месяца назад

    Excellent demo!

  • @ragy1986
    @ragy1986 2 года назад

    Been looking for a video JUST like this. Thank you, very helpful 👍

  • @achuthansajeevan
    @achuthansajeevan 2 года назад +2

    Excellent content and very easy to follow. Thanks for the tutorial!!!

  • @WoodlandPools
    @WoodlandPools Год назад

    Super helpful. Thanks!

  • @bjugdbjk
    @bjugdbjk 2 года назад

    u have an amazing teaching skill buddy !! u made rust so simple !!

  • @_jdfx
    @_jdfx 2 года назад +1

    so good! another great tutorial! thank you!!!

  • @agnaldopereiradasilva6007
    @agnaldopereiradasilva6007 6 месяцев назад

    Great Man! Perfect Video!

  • @daymaker_trading
    @daymaker_trading 2 года назад

    Thanks so much for this video!

  • @alanrodrigoaxotlapena3363
    @alanrodrigoaxotlapena3363 2 года назад

    Such a excellent video!

  • @Rohitkumar-xv7uv
    @Rohitkumar-xv7uv Год назад

    You made this easy peasy for begineers

  • @harrynewton6200
    @harrynewton6200 2 года назад +2

    What VScode extensions do you have? I like the generated text next to some of the code statements showing the result value type.

  • @opeyemiadeyemi5048
    @opeyemiadeyemi5048 2 года назад +1

    Thanks bro..,

  • @awlc099
    @awlc099 2 года назад +2

    Awesome! Can you do a websocket client?

  • @mohamadbt4055
    @mohamadbt4055 Год назад

    good one!

  • @KresnaPermana
    @KresnaPermana 2 года назад +1

    Thank you

  • @olayemimichael3426
    @olayemimichael3426 2 года назад +1

    Hi, instead making it async. Why not try reqwest blocking. I think it will make the code easier

  • @mrlectus
    @mrlectus Месяц назад

    What if i have error response and success response, how would i go about that?

  • @avalagum7957
    @avalagum7957 7 месяцев назад

    How to get the response status code, headers ...?
    If we try to json-deserialize a non-json string to a struct, will the library give us a Result or the app will panic?
    I guess that the issue with a lot of Rust crates are lack of documentation.

  • @GlobalYoung7
    @GlobalYoung7 2 года назад

    Thank you 😊

  • @Franx570
    @Franx570 Год назад

    What if the struct of the get response is variable?

  • @saeed-ahmed557
    @saeed-ahmed557 Год назад

    I have request each return a different response (different variables) how can I make structs for this kind of response ?

  • @felixfigueroa
    @felixfigueroa 2 года назад

    You can teach us how to make a CRUD with an API and database?

  • @tkemaladze
    @tkemaladze 2 года назад

    awsome video

  • @aryabp
    @aryabp 2 года назад

    explain about ntix please, it's seems interesting

    • @aryabp
      @aryabp 2 года назад

      ntex*

  • @sagarbarai
    @sagarbarai Год назад

    how to access key values from arbitrary json ?

  • @cd-stephen
    @cd-stephen 10 месяцев назад

    Excellente

  • @erlangparasu6339
    @erlangparasu6339 2 года назад

    thanks!

  • @rogeliopanebarcomajr9115
    @rogeliopanebarcomajr9115 2 года назад

    Json wife is Ruby

  • @MrMoonCraft
    @MrMoonCraft 2 года назад

    How do you have the inline linter telling you the object names and the syntax errors and stuff? like, at one point after .json() you can see impl Future
    What's the extension?

    • @tawalioualao7444
      @tawalioualao7444 2 года назад +1

      He did a video about tools to help dev in their code journey (rust).
      Check: rust-analyzer (it did many things like prints variables types,...)

  • @hariram3700
    @hariram3700 Год назад

    I tried this code but I am facing error "error: could not compile `futures-util`
    Caused by:
    process didn't exit successfully: `rustc --crate-name futures_util --edition=2018 " pls guys i am beginner help me to solve this issue

  • @sohn7767
    @sohn7767 2 года назад

    Keep in mind that special characters like “ will not be serialized properly

    • @peter9477
      @peter9477 2 года назад

      Why not? Usually things like that just get escaped in the serialized output. Doesn't that happen here?

    • @sohn7767
      @sohn7767 2 года назад

      @@peter9477 nope, they turn into their utf representation \u0203 or smth

    • @peter9477
      @peter9477 2 года назад +1

      @@sohn7767 So it sounds like either they're serialized as UTF-8, which is correct behaviour for JSON, or (for double-quotes specifically, and some other chars) they have their escaped representation with \u and four hex digits. Seems like expected behavior. I don't see the problem.

  • @Terroid
    @Terroid 2 года назад

    Yoo!! Do you have a discord server?? :D I would Love to join it!

  • @stevenhe3462
    @stevenhe3462 2 года назад

    Funny that your spell checker is angry with `reqwest` but fine with `tokio`

  • @ESArnau
    @ESArnau 2 года назад

    Hello Bogden, i made this crate to get nested properties from a json with an easy syntax. What do you think? crates io crates json_extract

  • @robertreimann1846
    @robertreimann1846 2 года назад

    B-but... I just wanted to parse my json, not to make http requests :c

    • @hey7328
      @hey7328 2 года назад

      Use the serde_json crate functions

  • @sheepsushis5720
    @sheepsushis5720 2 года назад

    great tutorial, but 3 days too late, lol.

  • @yassin_eldeeb
    @yassin_eldeeb 2 года назад +1

    It would be awesome if you make a series building a real world project that people can get creative adding features to it and extending it, cause unfortunately currently the videos are categorized as "Tutorial Hell".

  • @ntippy
    @ntippy 5 месяцев назад

    Not a big fan of adding tokio. we should be able to learn the subject without that unrelated complexity.

  • @andypython
    @andypython 2 года назад +4

    First, pin comment :^)

  • @CuriousSpy
    @CuriousSpy 2 года назад +5

    Please fix haircut

  • @ItaloMaiaTM
    @ItaloMaiaTM 2 года назад

    How support to call "json()" is added to reqwest? Just by importing serde?

    • @skyeplus
      @skyeplus 2 года назад +1

      That's a good question. I've done some digging.
      The crate source:
      docs.rs/reqwest/latest/src/reqwest/async_impl/request.rs.html#436-452
      docs.rs/crate/reqwest/latest/source/Cargo.toml
      Serde is imported under a "feature" caluse:
      #[cfg(feature = "json")]
      use serde_json;
      The function is enabled under feature "json" config too:
      #[cfg(feature = "json")]
      #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
      pub fn json(mut self, json: &T) -> RequestBuilder {
      And the config has the feature and dependency defined as:
      [dependencies.serde_json]
      optional = true
      version = "1.0"
      [features]
      ...
      json = ["serde_json"]