fix: preserve integers in trace_profile.json — no more .0 suffix on trace_id/frame/bbox
This commit is contained in:
@@ -401,12 +401,16 @@ pub struct FaceTraceProfile {
|
|||||||
pub status: String, // pending / bound / merged
|
pub status: String, // pending / bound / merged
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recursively round all float numbers in a JSON value to 2 decimal places.
|
/// Recursively round float numbers in a JSON value to 2 decimal places.
|
||||||
|
/// Integer numbers are preserved as-is (no `.0` suffix).
|
||||||
/// Used for trace_profile.json output only; in-memory values are preserved.
|
/// 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 {
|
pub(crate) fn round_json_value(v: &serde_json::Value) -> serde_json::Value {
|
||||||
match v {
|
match v {
|
||||||
serde_json::Value::Number(n) => {
|
serde_json::Value::Number(n) => {
|
||||||
if let Some(f) = n.as_f64() {
|
if n.is_i64() || n.is_u64() {
|
||||||
|
// Whole numbers keep their integer representation (no `.0`).
|
||||||
|
v.clone()
|
||||||
|
} else if let Some(f) = n.as_f64() {
|
||||||
let rounded = (f * 100.0).round() / 100.0;
|
let rounded = (f * 100.0).round() / 100.0;
|
||||||
serde_json::Value::from(rounded)
|
serde_json::Value::from(rounded)
|
||||||
} else {
|
} else {
|
||||||
@@ -628,6 +632,33 @@ mod tests {
|
|||||||
assert_eq!(path, PathBuf::from("/output/abc123.profile.json"));
|
assert_eq!(path, PathBuf::from("/output/abc123.profile.json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_round_json_value_preserves_integers() {
|
||||||
|
let v = serde_json::json!({
|
||||||
|
"trace_id": 4560,
|
||||||
|
"frame_count": 21,
|
||||||
|
"start_frame": 150221,
|
||||||
|
"avg_confidence": 0.781234,
|
||||||
|
"best_confidence": 0.81,
|
||||||
|
"bbox": {"x": 336, "y": 71, "width": 113, "height": 113}
|
||||||
|
});
|
||||||
|
let rounded = round_json_value(&v);
|
||||||
|
// Integers stay integers (no `.0` suffix)
|
||||||
|
assert_eq!(rounded["trace_id"], serde_json::json!(4560));
|
||||||
|
assert!(rounded["trace_id"].is_i64());
|
||||||
|
assert_eq!(rounded["frame_count"], serde_json::json!(21));
|
||||||
|
assert!(rounded["frame_count"].is_i64());
|
||||||
|
assert_eq!(rounded["start_frame"], serde_json::json!(150221));
|
||||||
|
assert!(rounded["start_frame"].is_i64());
|
||||||
|
assert_eq!(rounded["bbox"]["x"], serde_json::json!(336));
|
||||||
|
assert!(rounded["bbox"]["x"].is_i64());
|
||||||
|
// Floats rounded to 2 decimals
|
||||||
|
assert_eq!(rounded["avg_confidence"], serde_json::json!(0.78));
|
||||||
|
assert!(rounded["avg_confidence"].is_f64());
|
||||||
|
assert_eq!(rounded["best_confidence"], serde_json::json!(0.81));
|
||||||
|
assert!(rounded["best_confidence"].is_f64());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_registration() {
|
fn test_from_registration() {
|
||||||
let profile = FileProfile::from_registration(
|
let profile = FileProfile::from_registration(
|
||||||
|
|||||||
Reference in New Issue
Block a user