fix(export): omit BOM from JSON files

Closes #5231
This commit is contained in:
t8y2 committed 2026-08-03 22:34:03 +08:00
1 parent feaf914dcf
commit 05841277d1
1 file changed
+33 -7
+33 -7
View File
@@ -16,15 +16,17 @@ impl QueryResultTextExportRequest {
}
}
fn write_query_result_json(request: QueryResultTextExportRequest) -> Result<(), String> {
let file_path = request.file_path.clone();
let content = format_json(&request.into_data())?;
std::fs::write(file_path, content).map_err(|err| err.to_string())
}
#[tauri::command]
pub async fn export_query_result_json(request: QueryResultTextExportRequest) -> Result<(), String> {
tauri::async_runtime::spawn_blocking(move || {
let file_path = request.file_path.clone();
let content = format_json(&request.into_data())?;
std::fs::write(file_path, format!("\u{FEFF}{content}")).map_err(|err| err.to_string())
})
.await
.map_err(|err| err.to_string())?
tauri::async_runtime::spawn_blocking(move || write_query_result_json(request))
.await
.map_err(|err| err.to_string())?
}
#[tauri::command]
@@ -37,3 +39,27 @@ pub async fn export_query_result_markdown(request: QueryResultTextExportRequest)
.await
.map_err(|err| err.to_string())?
}
#[cfg(test)]
mod tests {
use serde_json::{json, Value};
use super::{write_query_result_json, QueryResultTextExportRequest};
#[test]
fn writes_json_without_utf8_bom() {
let path = std::env::temp_dir().join(format!("dbx-json-export-{}.json", uuid::Uuid::new_v4()));
write_query_result_json(QueryResultTextExportRequest {
file_path: path.to_string_lossy().into_owned(),
columns: vec!["id".to_string(), "name".to_string()],
rows: vec![vec![json!(1), json!("Ada")]],
})
.unwrap();
let bytes = std::fs::read(&path).unwrap();
let _ = std::fs::remove_file(&path);
assert!(!bytes.starts_with(b"\xEF\xBB\xBF"));
assert_eq!(serde_json::from_slice::<Value>(&bytes).unwrap(), json!([{ "id": 1, "name": "Ada" }]));
}
}