feat: round output JSON floats to 2dp + unify ensure_ascii for asr/asrx
This commit is contained in:
@@ -401,6 +401,30 @@ pub struct FaceTraceProfile {
|
||||
pub status: String, // pending / bound / merged
|
||||
}
|
||||
|
||||
/// Recursively round all float numbers in a JSON value to 2 decimal places.
|
||||
/// Used for trace_profile.json output only; in-memory values are preserved.
|
||||
pub(crate) fn round_json_value(v: &serde_json::Value) -> serde_json::Value {
|
||||
match v {
|
||||
serde_json::Value::Number(n) => {
|
||||
if let Some(f) = n.as_f64() {
|
||||
let rounded = (f * 100.0).round() / 100.0;
|
||||
serde_json::Value::from(rounded)
|
||||
} else {
|
||||
v.clone()
|
||||
}
|
||||
}
|
||||
serde_json::Value::Array(items) => {
|
||||
serde_json::Value::Array(items.iter().map(round_json_value).collect())
|
||||
}
|
||||
serde_json::Value::Object(map) => serde_json::Value::Object(
|
||||
map.iter()
|
||||
.map(|(k, val)| (k.clone(), round_json_value(val)))
|
||||
.collect(),
|
||||
),
|
||||
_ => v.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
impl FaceTraceProfile {
|
||||
/// Directory: `output/{file_uuid}/{trace_id}/`
|
||||
pub fn trace_dir(output_dir: &str, file_uuid: &str, trace_id: i64) -> PathBuf {
|
||||
@@ -488,7 +512,8 @@ impl FaceTraceProfile {
|
||||
|
||||
// Write trace_profile.json
|
||||
let json =
|
||||
serde_json::to_string_pretty(self).context("Failed to serialize FaceTraceProfile")?;
|
||||
serde_json::to_string_pretty(&round_json_value(&serde_json::to_value(&*self)?))
|
||||
.context("Failed to serialize FaceTraceProfile")?;
|
||||
let profile_path = Self::profile_path(output_dir, &self.file_uuid, self.trace_id);
|
||||
std::fs::write(&profile_path, json)
|
||||
.with_context(|| format!("Failed to write {}", profile_path.display()))?;
|
||||
@@ -636,6 +661,7 @@ mod tests {
|
||||
profile.face_filter = Some(FaceFilterSettings {
|
||||
min_confidence: 85.0,
|
||||
min_size: 96,
|
||||
min_capture_quality: 0.0,
|
||||
});
|
||||
let json = serde_json::to_string(&profile).unwrap();
|
||||
let restored: FileProfile = serde_json::from_str(&json).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user