mulit line json

This commit is contained in:
Elijah McMorris 2023-12-13 15:20:48 -08:00
parent b838a5c326
commit f0716081fe
Signed by: NexVeridian
SSH key fingerprint: SHA256:bsA1SKZxuEcEVHAy3gY1HUeM5ykRJl0U0kQHQn0hMg8
3 changed files with 27 additions and 19 deletions

View file

@ -1,4 +1,10 @@
{
"json.format.enable": false,
"[json]": {
"editor.wordWrap": "off",
"editor.formatOnSave": false,
"editor.formatOnPaste": false,
},
"[Rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true,

View file

@ -1,18 +1,18 @@
use anyhow::Ok;
use anyhow::{Error, Result};
use dotenv_codegen::dotenv;
use serde_json::{from_str, Value};
use std::fs::File;
use std::io::{BufRead, BufReader};
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::Surreal;
mod utils;
use utils::*;
use wikidata::Entity;
#[tokio::main]
async fn main() -> Result<(), Error> {
let data = get_entity("data/e.json").await?;
let (id, data) = EntityMini::from_entity(data);
let db = Surreal::new::<Ws>("0.0.0.0:8000").await?;
db.signin(Root {
@ -23,9 +23,23 @@ async fn main() -> Result<(), Error> {
db.use_ns("wikidata").use_db("wikidata").await?;
let file = File::open("data/w.json")?;
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line?.trim().trim_end_matches(',').to_string();
if line == "[" || line == "]" {
continue;
}
let json: Value = from_str(&line)?;
let data = Entity::from_json(json).expect("Failed to parse JSON");
let (id, data) = EntityMini::from_entity(data);
let _: Option<EntityMini> = db.delete(&id).await?;
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
}
// println!("{:#?}", data);
Ok(())
}

View file

@ -1,20 +1,8 @@
use anyhow::{Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::from_reader;
use serde_json::Value;
use std::fs::File;
use surrealdb::sql::Thing;
use wikidata::ClaimValueData;
use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId};
pub async fn get_entity(path: &str) -> Result<Entity, Error> {
// From here - https://www.wikidata.org/wiki/Special:EntityData/P1476.json
let mut file = File::open(path)?;
let json: Value = from_reader(&mut file)?;
let data = Entity::from_json(json).expect("Failed to parse JSON");
Ok(data)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityMini {
// In English