Parsing JSON in Rust using serde and reqwest

Поділитися
Вставка
  • Опубліковано 22 кві 2022
  • Today we will be discussing how to parse JSON in Rust using serde and reqwest!
    📝Get your FREE Rust cheat sheet :
    www.letsgetrusty.com/cheatsheet
    code: github.com/letsgetrusty/json_...
  • Наука та технологія

КОМЕНТАРІ • 77

  • @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 роки тому +4

      @@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

  • @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!!!

  • @jaysistar2711
    @jaysistar2711 2 роки тому +8

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

  • @sergiuoanes4635
    @sergiuoanes4635 2 роки тому +1

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

  • @AccordionMusicAndMore
    @AccordionMusicAndMore Місяць тому

    Excellent demo!

  • @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!!!

  • @WoodlandPools
    @WoodlandPools Рік тому

    Super helpful. Thanks!

  • @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

  • @daymaker_bybit
    @daymaker_bybit Рік тому

    Thanks so much for this video!

  • @alanrodrigoaxotlapena3363
    @alanrodrigoaxotlapena3363 2 роки тому

    Such a excellent video!

  • @Rohitkumar-xv7uv
    @Rohitkumar-xv7uv Рік тому

    You made this easy peasy for begineers

  • @agnaldopereiradasilva6007
    @agnaldopereiradasilva6007 4 місяці тому

    Great Man! Perfect Video!

  • @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

  • @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

  • @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 5 місяців тому

      Did you mean circuit breaker?

  • @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

  • @GlobalYoung7
    @GlobalYoung7 2 роки тому

    Thank you 😊

  • @KresnaPermana
    @KresnaPermana 2 роки тому +1

    Thank you

  • @mohamadbt4055
    @mohamadbt4055 Рік тому

    good one!

  • @opeyemiadeyemi5048
    @opeyemiadeyemi5048 2 роки тому +1

    Thanks bro..,

  • @erlangparasu6339
    @erlangparasu6339 2 роки тому

    thanks!

  • @tkemaladze
    @tkemaladze 2 роки тому

    awsome video

  • @awlc099
    @awlc099 2 роки тому +2

    Awesome! Can you do a websocket client?

  • @Franx570
    @Franx570 Рік тому

    What if the struct of the get response is variable?

  • @cd-stephen
    @cd-stephen 8 місяців тому

    Excellente

  • @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.

  • @felixfigueroa
    @felixfigueroa 2 роки тому

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

  • @sagarbarai
    @sagarbarai Рік тому

    how to access key values from arbitrary json ?

  • @saeed-ahmed557
    @saeed-ahmed557 Рік тому

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

  • @avalagum7957
    @avalagum7957 5 місяців тому

    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.

  • @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,...)

  • @aryabp
    @aryabp 2 роки тому

    explain about ntix please, it's seems interesting

  • @olayemimichael3426
    @olayemimichael3426 2 роки тому +1

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

  • @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"]

  • @Terroid
    @Terroid 2 роки тому

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

  • @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.

  • @stevenhe3462
    @stevenhe3462 2 роки тому

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

  • @rogeliopanebarcomajr9115
    @rogeliopanebarcomajr9115 2 роки тому

    Json wife is Ruby

  • @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.

  • @andypython
    @andypython 2 роки тому +4

    First, pin comment :^)

  • @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 3 місяці тому

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

  • @CuriousSpy
    @CuriousSpy 2 роки тому +5

    Please fix haircut