Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Rust JSON

Tags: json rust object

Introduction to Rust JSON

JSON stands for Javascript Object Notation, it works in the same way like any other programming language. Json are very light weight and store anything inside them, mainly JSON in rust or nay other programming language is used to transfer data from server to web pages and vice versa. Inside the JSON in rust we can store anything like objects, array etc. JSON have specific format which we need to follow if we want to parse or deparse our object to Json otherwise we will receive parsing expectation at runtime. JSON always starts with a ‘{‘ curly braces and ends with a ‘}’ curly braces in rust or nay other programming language. In the coming section of tutorial we will learn the internal working of the JSON and how to implement this in program using rust.

Syntax

As we know that while dealing with Json we have to follow some standard define by the JSON library itself. They have some specific format to define Json. Let’s have a look at the syntax to define Json in rust for better understanding see below;

let variable_name = json::parse(your_json obje).unwrap();

As you cans ee in the above lines of syntax we are using json here , inside this we are passing our object to give us the JSON object in return. But to make it work we have to include the crate json dependency into our project. Let’s see one practice syntax for better understating see below;

Example:

let demo = json::parse(r#"
{
"abc" : "123"
}
"#).unwrap();

You can follow the above steps to create the JSON in rust, in the coming section  we will discuss more about the internal working and dependency require for this to work in detail for beginners.

How JSON works in Rust?

As we already discussed that JSON in rust is just like normal JSON in other programming language. It can store anything inside it, in the form of key value pair. JSON can contain anything inside them, object, array etc. Object can also be converted into JSON using libraries available. In this tutorial we are using crate and serialization API to deal with the JSON in rust. In this tutorial we will learn more about its working and implementation in detail see below;

To use json inside the program we have to include the dependency and after that we can use this in the program only. Json have specific format also inside JSON we have objects and their values in the form of key and value pair. To create a json object we have to use serialization dependency in rust, in our program we have to import it at the first place. else it will give us error. Let’s take a look at the steps that we need to take while using JSON in rust see below;

1. In order to use json inside the code we have to include the below-specified dependency into our code.

Example:

extern crate serialize;
use serialize::json;

At the starting of the program we have to include the above mentioned library. After this we can use its method to create our json object in rust. In the coming section of the tutorial we will see the method how to use them.

We also have one more library that can be used to deal with the json in rust. We can have look at this dependency also see below;

1. First add the below dependency into the cargo.toml file. After this only we can deal with json in rust. For more information see below;

Example:

[dependencies] serde = { version = "1", features = ["derive"] }

2. After this we can use below lines of code in any class which we want the josn object. Below mentioning the lines of code which needs to include in the class object see below;

Example:

use serde::{Deserialize, Serialize};
// mandatory lines to use json in rust
#[derive(Debug, Deserialize, Serialize)]

In the above lines we are trying to use them like any other macros in rust.

3. After this configuration, in the main() method we can create the object of our class and pass it inside the json method available by the dependency of serde. In this section we will one practice example for beginners to understand the requirement and implementation in detail see below;

Example:

1. First we can create the struct in rust with the following configuration on it.

Example:

struct DemoObj {
name: String,
city: String,
add: String,
id: usize,
}

2. Code in the main method should be like this see below;

Example:

fn main() {
let myobj = r#"
{
"name": "Amit vyas",
"city": "Mumbai",
"add": "mumbai street",
"id" :001
}
"#;
let demo: DemoObj = serde_json::from_str(myobj).unwrap();
}

In the above lines of code we are first creating the object for the struct that we have created in the first step. To create the object we have followed some standard already define. Inside this {} braces we are trying to define the values for the variable that we have used in the struct. After that we are using serde API to convert the object  into the JSON. we have used unwrap() method for it. But be very sure that we have to use serde dependency into the cargo.toml file otherwise the example will not be work.

Examples

1. In this example we are trying to use serde library to deal with json in rust, also keep in mind that you need to setup the serde dependency in your configure file, then only the below mentioned example will work. otherwise it will not, also it will not work using any only compiler for rust.

Example:

use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)] struct DemoObjectStruct {
rolno:usize,
name: String,
city: String,
salary : usize,
}
fn main() {
println!("demo to show json in rust !!");
println!("creating json object below ::");
let demoobj = r#"
{
"rolno": 001,
"name": "dummy name here",
"city": "mumbai",
"salary" : 1000
}
"#;
let jobject: DemoObjectStruct = serde_json::from_str(demoobj).unwrap();
println!("prinitng value og jsn objetc :: ");
println!("value inside the json objetc is :: {:?}", jobject);
}

Output:

Conclusion

JSON are very light weighted and also they can store any type of object inside them. Mainly they are used to deal with request and response between server and web pages. We can made a JSON object by using any object, array, or list of object inside it, it supports everything and very easy to handle.

Recommended Articles

This is a guide to Rust JSON. Here we discuss How JSON works in Rust and how to implement it with Examples and outputs. You may also have a look at the following articles to learn more –

  1. What is Rust Programming?
  2. JSON Parser
  3. Perl JSON
  4. JSON Pretty

The post Rust JSON appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Rust JSON

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×