Browse Source

feat: add toml decode demo

dev
Dnomd343 2 years ago
parent
commit
aeece10839
  1. 10
      to-json/Cargo.lock
  2. 1
      to-json/Cargo.toml
  3. 27
      to-json/src/main.rs

10
to-json/Cargo.lock

@ -73,6 +73,16 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"toml",
]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]]

1
to-json/Cargo.toml

@ -9,3 +9,4 @@ edition = "2021"
serde = "1.0.145"
serde_json = "1.0.86"
serde_yaml = "0.9.14"
toml = "0.5.9"

27
to-json/src/main.rs

@ -1,7 +1,9 @@
use serde_json::Value as JsonValue;
use serde_yaml::Value as YamlValue;
use toml::Value as TomlValue;
const JSON_STR: &str = r#"{
const JSON_STR: &str = r#"
{
"demo": "key_1",
"author": "dnomd343",
"test": [
@ -9,7 +11,8 @@ const JSON_STR: &str = r#"{
"234",
"345"
]
}"#;
}
"#;
const YAML_STR: &str = r#"
demo: key_1
@ -20,16 +23,28 @@ test:
- 345
"#;
const TOML_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);
println!("---------------------------------");
println!("JSON raw content:\n{}", JSON_STR);
println!("---------------------------------");
println!("YAML raw content:\n{}", YAML_STR);
println!("---------------------------------");
println!("TOML raw content:\n{}", TOML_STR);
println!("---------------------------------");
// let data: JsonValue = serde_json::from_str(JSON_STR).unwrap();
// println!("{:#?}", data);
let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap();
// let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap();
// println!("{:#?}", data);
let data: TomlValue = toml::from_str(TOML_STR).unwrap();
println!("{:#?}", data);
let ret = serde_json::to_string(&data).unwrap();
println!("JSON output ->\n{}", ret);
}

Loading…
Cancel
Save