add parse json
This commit is contained in:
parent
07fd43fb5f
commit
7ba45ed0c3
@ -9,6 +9,8 @@ edition = "2021"
|
||||
env_logger = "0.11.3"
|
||||
log = "0.4.22"
|
||||
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_json = "1.0.120"
|
||||
tokio = {version = "1.38.1",features = ["full"]}
|
||||
|
24
src/main.rs
24
src/main.rs
@ -1,11 +1,14 @@
|
||||
use std::error::Error;
|
||||
use std::{error::Error, process::exit};
|
||||
use start_db::Starter;
|
||||
use log::{error, info};
|
||||
use word_entity::Model as WordModel;
|
||||
use parse_json::json_object::Word as WordJsonObject;
|
||||
// use serde_json::json;
|
||||
use tokio;
|
||||
|
||||
pub mod word_entity;
|
||||
pub mod start_db;
|
||||
pub mod parse_json;
|
||||
|
||||
#[tokio::main]
|
||||
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>|{
|
||||
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);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
info!("parsed object Ok!");
|
||||
info!("object[0]={:?}",parsed_object[0]);
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
17
src/parse_json.rs
Normal file
17
src/parse_json.rs
Normal 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)
|
||||
}
|
11
src/parse_json/json_object.rs
Normal file
11
src/parse_json/json_object.rs
Normal 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>,
|
||||
}
|
@ -1,13 +1,16 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize,Serialize};
|
||||
pub mod tests;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel,Serialize,Deserialize)]
|
||||
#[sea_orm(table_name = "words")]
|
||||
pub struct Model{
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
pub bsm: String,
|
||||
pub hz:String,
|
||||
#[serde(alias="pyJs")]
|
||||
pub py_js: String,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user