json parser
This commit is contained in:
parent
79e714d2d8
commit
849cb35eca
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1635,6 +1635,7 @@ dependencies = [
|
||||||
"rand",
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"zip",
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
|
@ -13,5 +13,6 @@ once_cell = "1.19.0"
|
||||||
rand = { version = "0.8.4", features = ["small_rng"] }
|
rand = { version = "0.8.4", features = ["small_rng"] }
|
||||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||||
rust-embed = "8.3.0"
|
rust-embed = "8.3.0"
|
||||||
serde_json = "1.0.115"
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
zip = "0.6.6"
|
zip = "0.6.6"
|
||||||
|
|
4527
output_pokemon.json
Normal file
4527
output_pokemon.json
Normal file
File diff suppressed because it is too large
Load diff
34955
pokemon.json
34955
pokemon.json
File diff suppressed because it is too large
Load diff
5856
pokemon.json.bak
Normal file
5856
pokemon.json.bak
Normal file
File diff suppressed because it is too large
Load diff
68
src/bin/serde.rs
Normal file
68
src/bin/serde.rs
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{Result, Value};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Input {
|
||||||
|
#[serde(flatten)]
|
||||||
|
other: HashMap<String, Value>, // Capturing the rest of the data
|
||||||
|
gen_8: Option<Generation8>, // Making gen_8 optional
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Generation8 {
|
||||||
|
forms: HashMap<String, Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Pokemon {
|
||||||
|
name: String,
|
||||||
|
id: String,
|
||||||
|
forms: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let data = fs::read_to_string("pokemon.json").expect("Unable to read file");
|
||||||
|
let input_pokemons: HashMap<String, Input> = serde_json::from_str(&data)?;
|
||||||
|
|
||||||
|
let mut output_pokemons: Vec<Pokemon> = Vec::new();
|
||||||
|
|
||||||
|
for (id, pokemon) in input_pokemons {
|
||||||
|
// Check if gen_8 data exists
|
||||||
|
let forms: Vec<String> = if let Some(gen_8) = pokemon.gen_8 {
|
||||||
|
gen_8
|
||||||
|
.forms
|
||||||
|
.keys()
|
||||||
|
.map(|key| {
|
||||||
|
if key == "$" {
|
||||||
|
"regular".to_string()
|
||||||
|
} else {
|
||||||
|
key.clone()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
// If there's no gen_8 data, you might want to handle it differently
|
||||||
|
// For now, let's just use an empty Vec
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Proceed as before
|
||||||
|
output_pokemons.push(Pokemon {
|
||||||
|
name: pokemon.other["name"]["eng"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap_or_default()
|
||||||
|
.to_string(),
|
||||||
|
id,
|
||||||
|
forms,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let output_json = serde_json::to_string_pretty(&output_pokemons)?;
|
||||||
|
println!("{}", output_json);
|
||||||
|
|
||||||
|
fs::write("output_pokemon.json", output_json).expect("Unable to write file");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -299,7 +299,8 @@ fn convert_image_to_unicode_small(img: &image::DynamicImage) -> String {
|
||||||
let lower_pixel = if y + 1 < height {
|
let lower_pixel = if y + 1 < height {
|
||||||
img.get_pixel(x, y + 1)
|
img.get_pixel(x, y + 1)
|
||||||
} else {
|
} else {
|
||||||
upper_pixel // Fallback to upper pixel if there's no lower pixel.
|
// fallback to upper pixel if there's no lower pixel.
|
||||||
|
upper_pixel
|
||||||
};
|
};
|
||||||
|
|
||||||
if upper_pixel[3] == 0 && lower_pixel[3] == 0 {
|
if upper_pixel[3] == 0 && lower_pixel[3] == 0 {
|
||||||
|
|
3
src/print.rs
Normal file
3
src/print.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub fn print() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
Loading…
Reference in a new issue