Add json extractor

This commit is contained in:
2026-03-16 09:53:32 -07:00
parent 979fbb9b0d
commit 1d90306408
2 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
use pile_config::Label;
use std::{
collections::HashMap,
sync::{Arc, OnceLock},
};
use crate::{
extract::traits::{ExtractState, ObjectExtractor},
value::{AsyncReader, Item, PileValue},
};
fn json_to_pile(value: serde_json::Value) -> PileValue {
match value {
serde_json::Value::Null => PileValue::Null,
serde_json::Value::Bool(b) => PileValue::String(Arc::new(b.to_string().into())),
serde_json::Value::Number(n) => PileValue::String(Arc::new(n.to_string().into())),
serde_json::Value::String(s) => PileValue::String(Arc::new(s.into())),
serde_json::Value::Array(a) => {
PileValue::Array(Arc::new(a.into_iter().map(json_to_pile).collect()))
}
serde_json::Value::Object(_) => PileValue::Null,
}
}
pub struct JsonExtractor {
item: Item,
output: OnceLock<HashMap<Label, PileValue>>,
}
impl JsonExtractor {
pub fn new(item: &Item) -> Self {
Self {
item: item.clone(),
output: OnceLock::new(),
}
}
async fn get_inner(&self) -> Result<&HashMap<Label, PileValue>, std::io::Error> {
if let Some(x) = self.output.get() {
return Ok(x);
}
let mut reader = self.item.read().await?;
let bytes = reader.read_to_end().await?;
let json: serde_json::Value = match serde_json::from_slice(&bytes) {
Ok(x) => x,
Err(_) => return Ok(self.output.get_or_init(HashMap::new)),
};
let output: HashMap<Label, PileValue> = match json {
serde_json::Value::Object(map) => map
.into_iter()
.filter_map(|(k, v)| Label::new(&k).map(|label| (label, json_to_pile(v))))
.collect(),
_ => HashMap::new(),
};
return Ok(self.output.get_or_init(|| output));
}
}
#[async_trait::async_trait]
impl ObjectExtractor for JsonExtractor {
async fn field(
&self,
state: &ExtractState,
name: &Label,
args: Option<&str>,
) -> Result<Option<PileValue>, std::io::Error> {
if args.is_some() {
return Ok(None);
}
if !state.ignore_mime
&& (self.item.mime().type_() != mime::APPLICATION
&& self.item.mime().type_() != mime::TEXT)
{
return Ok(None);
}
Ok(self.get_inner().await?.get(name).cloned())
}
async fn fields(&self) -> Result<Vec<Label>, std::io::Error> {
Ok(self.get_inner().await?.keys().cloned().collect())
}
}

View File

@@ -18,6 +18,9 @@ pub use exif::*;
mod pdf;
pub use pdf::*;
mod json;
pub use json::*;
mod toml;
use pile_config::Label;
pub use toml::*;
@@ -66,6 +69,10 @@ impl ItemExtractor {
Label::new("pdf").unwrap(),
PileValue::ObjectExtractor(Arc::new(PdfExtractor::new(item))),
),
(
Label::new("json").unwrap(),
PileValue::ObjectExtractor(Arc::new(JsonExtractor::new(item))),
),
(
Label::new("toml").unwrap(),
PileValue::ObjectExtractor(Arc::new(TomlExtractor::new(item))),
@@ -101,6 +108,7 @@ impl ObjectExtractor for ItemExtractor {
Label::new("epub").unwrap(),
Label::new("exif").unwrap(),
Label::new("pdf").unwrap(),
Label::new("json").unwrap(),
Label::new("sidecar").unwrap(),
]);
}