add parse json

This commit is contained in:
Zengtudor 2024-07-21 12:52:14 +08:00
parent 07fd43fb5f
commit 7ba45ed0c3
5 changed files with 53 additions and 4 deletions

View File

@ -9,6 +9,8 @@ edition = "2021"
env_logger = "0.11.3" env_logger = "0.11.3"
log = "0.4.22" log = "0.4.22"
sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] } sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] }
serde = {version="1.0.204",features=["derive"]}
serde_json = "1.0.120"
# serde = "1.0.204" # serde = "1.0.204"
# serde_json = "1.0.120" # serde_json = "1.0.120"
tokio = {version = "1.38.1",features = ["full"]} tokio = {version = "1.38.1",features = ["full"]}

View File

@ -1,11 +1,14 @@
use std::error::Error; use std::{error::Error, process::exit};
use start_db::Starter; use start_db::Starter;
use log::{error, info}; use log::{error, info};
use word_entity::Model as WordModel;
use parse_json::json_object::Word as WordJsonObject;
// use serde_json::json; // use serde_json::json;
use tokio; use tokio;
pub mod word_entity; pub mod word_entity;
pub mod start_db; pub mod start_db;
pub mod parse_json;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
@ -17,9 +20,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{ db_info.create_table().await.unwrap_or_else(|e: Box<dyn Error>|{
info!("The table have been created. Don't care this Error!!!!!:{}",e); info!("The table have been created. Don't care this Error!!!!!:{}",e);
}); });
word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e|{ word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e: Box<dyn Error>|{
error!("Cannot write data into database.The details are as follows:\n{}",e); error!("Cannot write data into database.The details are as follows:\n{}",e);
}); });
let parsed_object: Vec<WordJsonObject> = match parse_json::parse("word.json").await {
Ok(o)=>{
o
}
Err(e)=>{
error!("Error to parse the object");
error!("{}",e);
exit(1);
}
};
Ok(()) info!("parsed object Ok!");
info!("object[0]={:?}",parsed_object[0]);
return Ok(());
} }

17
src/parse_json.rs Normal file
View File

@ -0,0 +1,17 @@
use std::error::Error;
use log::info;
use serde::de::DeserializeOwned;
use tokio::{fs::File, io::AsyncReadExt};
pub mod json_object;
pub async fn parse<T>(path: &str) -> Result<T, Box<dyn Error>>
where
T: DeserializeOwned,
{
let mut json_string: String = String::new();
let json_size: usize = File::open(path).await?.read_to_string(&mut json_string).await?;
info!("From file {path} read {json_size} bytes");
let json_object: T = serde_json::from_str(&json_string)?;
Ok(json_object)
}

View File

@ -0,0 +1,11 @@
use serde::{Serialize,Deserialize};
#[derive(Clone, Debug,Serialize,Deserialize)]
pub struct Word{
#[serde(skip)]
pub id: i32,
pub bsm: String,
pub hz:String,
#[serde(alias="pyJs")]
pub py_js: Vec<String>,
}

View File

@ -1,13 +1,16 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use serde::{Deserialize,Serialize};
pub mod tests; pub mod tests;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel,Serialize,Deserialize)]
#[sea_orm(table_name = "words")] #[sea_orm(table_name = "words")]
pub struct Model{ pub struct Model{
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
#[serde(skip)]
pub id: i32, pub id: i32,
pub bsm: String, pub bsm: String,
pub hz:String, pub hz:String,
#[serde(alias="pyJs")]
pub py_js: String, pub py_js: String,
} }