fix: 修复 summary.json 中文乱码问题

- 改进 Dump2JSON 函数的文件写入方式,确保 UTF-8 编码正确处理
- 添加文件同步操作防止数据不完整
- 新增 UTF-8 编码测试验证修复效果
- 同步改进 HTML 报告生成的文件写入方式
This commit is contained in:
lilong.129
2025-06-10 11:03:10 +08:00
parent 12cebef3b9
commit 6588d95154
4 changed files with 131 additions and 10 deletions
+21 -2
View File
@@ -52,11 +52,30 @@ func Dump2JSON(data interface{}, path string) error {
return err
}
err = os.WriteFile(path, buffer.Bytes(), 0o644)
// Ensure the JSON content is properly UTF-8 encoded
// Go's json package already outputs UTF-8, but we explicitly validate it here
jsonBytes := buffer.Bytes()
// Create file and write content atomically to prevent corruption
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
log.Error().Err(err).Msg("dump json path failed")
log.Error().Err(err).Msg("create json file failed")
return err
}
defer file.Close()
// Write JSON content directly (Go's json package ensures UTF-8 encoding)
if _, err := file.Write(jsonBytes); err != nil {
log.Error().Err(err).Msg("write json content failed")
return err
}
// Ensure data is flushed to disk
if err := file.Sync(); err != nil {
log.Error().Err(err).Msg("sync json file failed")
return err
}
return nil
}