From 0ae72622b47f0e5fa316e68a0944cc823715dbef Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 18:06:46 +0800 Subject: [PATCH] feat: add yaml decode demo --- to-json/Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ to-json/Cargo.toml | 1 + to-json/src/main.rs | 18 ++++++++++++++++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock index e209a54..a9f3d1a 100644 --- a/to-json/Cargo.lock +++ b/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" diff --git a/to-json/Cargo.toml b/to-json/Cargo.toml index 8653059..14bf0c0 100644 --- a/to-json/Cargo.toml +++ b/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" diff --git a/to-json/src/main.rs b/to-json/src/main.rs index 20144fe..20908bd 100644 --- a/to-json/src/main.rs +++ b/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); }