Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/JSON/Rust/json-1.rust
Normal file
3
Task/JSON/Rust/json-1.rust
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
7
Task/JSON/Rust/json-2.rust
Normal file
7
Task/JSON/Rust/json-2.rust
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
9
Task/JSON/Rust/json-3.rust
Normal file
9
Task/JSON/Rust/json-3.rust
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fn main() {
|
||||
let point = Point { x: 1, y: 2 };
|
||||
|
||||
let serialized = serde_json::to_string(&point).unwrap();
|
||||
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
println!("serialized = {}", serialized);
|
||||
println!("deserialized = {:?}", deserialized);
|
||||
}
|
||||
19
Task/JSON/Rust/json-4.rust
Normal file
19
Task/JSON/Rust/json-4.rust
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#[derive(Serialize, Deserialize)]
|
||||
struct W { a: i32, b: i32 } // => { "a": 0, "b": 0 }
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct X(i32, i32); // => [0, 0]
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Y(i32); // => 0
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Z; // => null
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
enum E {
|
||||
W { a: i32, b: i32 }, // => { "W": { "a": 0, "b": 0 } }
|
||||
X(i32, i32), // => { "X": [0, 0] }
|
||||
Y(i32), // => { "Y": 0 }
|
||||
Z, // => { "Z" }
|
||||
}
|
||||
10
Task/JSON/Rust/json-5.rust
Normal file
10
Task/JSON/Rust/json-5.rust
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use std::collections::HashMap;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Data {
|
||||
points: Vec<Points>,
|
||||
|
||||
#[serde(flatten)]
|
||||
metadata: HashMap<String, Value>,
|
||||
}
|
||||
18
Task/JSON/Rust/json-6.rust
Normal file
18
Task/JSON/Rust/json-6.rust
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fn main() {
|
||||
let data = {
|
||||
let mut metadata = HashMap::new();
|
||||
metadata.insert("triangle".to_string(), Value::Number(3.into()));
|
||||
metadata.insert("square".to_string(), Value::Bool(false));
|
||||
|
||||
Data {
|
||||
points: vec![Point { x: 1, y: 2 }, Point { x: 15, y: 32 }],
|
||||
metadata,
|
||||
}
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&data).unwrap();
|
||||
let deserialized: Data = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
println!("serialized = {}", serialized);
|
||||
println!("deserialized = {:?}", deserialized);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue