From 05841277d143c26917be324f4ac296d3a413b82e Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Mon, 3 Aug 2026 22:34:03 +0800 Subject: [PATCH] fix(export): omit BOM from JSON files Closes #5231 --- src-tauri/src/commands/text_export.rs | 40 ++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/commands/text_export.rs b/src-tauri/src/commands/text_export.rs index 7e3267ea33..2844397fb4 100644 --- a/src-tauri/src/commands/text_export.rs +++ b/src-tauri/src/commands/text_export.rs @@ -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::(&bytes).unwrap(), json!([{ "id": 1, "name": "Ada" }])); + } +}