Browse Source

feat: add yaml decode demo

dev
Dnomd343 2 years ago
parent
commit
0ae72622b4
  1. 42
      to-json/Cargo.lock
  2. 1
      to-json/Cargo.toml
  3. 18
      to-json/src/main.rs

42
to-json/Cargo.lock

@ -2,6 +2,28 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "indexmap"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.4"
@ -31,10 +53,30 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "to-json"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"serde_yaml",
]
[[package]]
name = "unsafe-libyaml"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"

1
to-json/Cargo.toml

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
serde = "1.0.145"
serde_json = "1.0.86"
serde_yaml = "0.9.14"

18
to-json/src/main.rs

@ -1,4 +1,5 @@
use serde_json::{Value};
use serde_json::Value as JsonValue;
use serde_yaml::Value as YamlValue;
const JSON_STR: &str = r#"{
"demo": "key_1",
@ -10,12 +11,25 @@ const JSON_STR: &str = r#"{
]
}"#;
const YAML_STR: &str = r#"
demo: key_1
author: dnomd343
test:
- 123
- 234
- 345
"#;
fn main() {
println!("JSON raw content ->\n{}", JSON_STR);
println!("YAML raw content ->\n{}", YAML_STR);
let data: Value = serde_json::from_str(JSON_STR).unwrap();
// let data: JsonValue = serde_json::from_str(JSON_STR).unwrap();
// println!("{:#?}", data);
let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap();
println!("{:#?}", data);
let ret = serde_json::to_string(&data).unwrap();
println!("JSON output ->\n{}", ret);
}

Loading…
Cancel
Save