add serde-json-test
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,7 @@
|
|||||||
# ---> Rust
|
# ---> Rust
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
/target/
|
target/
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
|||||||
11
serde-json-test/Cargo.toml
Normal file
11
serde-json-test/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "serde-json-test"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
53
serde-json-test/src/main.rs
Normal file
53
serde-json-test/src/main.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{Result, Value};
|
||||||
|
|
||||||
|
// https://serde.rs/container-attrs.html
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||||
|
enum TestEnum {
|
||||||
|
|
||||||
|
#[serde(rename = "A")]
|
||||||
|
TypeA,
|
||||||
|
|
||||||
|
#[serde(rename = "B")]
|
||||||
|
TypeB,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
struct TestStruct {
|
||||||
|
ty: TestEnum,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let serialized = serde_json::to_string(&TestStruct{
|
||||||
|
ty: TestEnum::TypeA,
|
||||||
|
}).unwrap();
|
||||||
|
|
||||||
|
println!("{}", serialized);
|
||||||
|
|
||||||
|
let data = r#"
|
||||||
|
{
|
||||||
|
"name": "John Doe",
|
||||||
|
"age": 43,
|
||||||
|
"phones": [
|
||||||
|
"+44 1234567",
|
||||||
|
"+44 2345678"
|
||||||
|
]
|
||||||
|
}"#;
|
||||||
|
let v: Value = serde_json::from_str(data)?;
|
||||||
|
println!("{}", v);
|
||||||
|
|
||||||
|
let john = json!({
|
||||||
|
"name": "John Doe",
|
||||||
|
"age": 43,
|
||||||
|
"phones": [
|
||||||
|
"+44 1234567",
|
||||||
|
"+44 2345678"
|
||||||
|
]
|
||||||
|
});
|
||||||
|
println!("{}", john);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user