{"record":{"id":"6f42d9fa5a006359","repo":"vectordotdev/vector","slug":"could-not-convert-number-to-json","errorCode":null,"errorMessage":"Could not convert number to JSON","messagePattern":"Could not convert number to JSON","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-config/src/stdlib.rs","lineNumber":201,"sourceCode":"            fn to_value(&self) -> Value {\n                let into = $into;\n                Value::Number(into(*self))\n            }\n        }\n    };\n}\n\nimpl_configurable_numeric!(u8 => Into::into);\nimpl_configurable_numeric!(u16 => Into::into);\nimpl_configurable_numeric!(u32 => Into::into);\nimpl_configurable_numeric!(u64 => Into::into);\nimpl_configurable_numeric!(usize => Into::into);\nimpl_configurable_numeric!(i8 => Into::into);\nimpl_configurable_numeric!(i16 => Into::into);\nimpl_configurable_numeric!(i32 => Into::into);\nimpl_configurable_numeric!(i64 => Into::into);\nimpl_configurable_numeric!(isize => Into::into);\nimpl_configurable_numeric!(f32 => |v| Number::from_f64(v as f64).expect(\"Could not convert number to JSON\"));\nimpl_configurable_numeric!(f64 => |v| Number::from_f64(v).expect(\"Could not convert number to JSON\"));\nimpl_configurable_numeric!(NonZeroU8 => |v: NonZeroU8| v.get().into());\nimpl_configurable_numeric!(NonZeroU16 => |v: NonZeroU16| v.get().into());\nimpl_configurable_numeric!(NonZeroU32 => |v: NonZeroU32| v.get().into());\nimpl_configurable_numeric!(NonZeroU64 => |v: NonZeroU64| v.get().into());\nimpl_configurable_numeric!(NonZeroI8 => |v: NonZeroI8| v.get().into());\nimpl_configurable_numeric!(NonZeroI16 => |v: NonZeroI16| v.get().into());\nimpl_configurable_numeric!(NonZeroI32 => |v: NonZeroI32| v.get().into());\nimpl_configurable_numeric!(NonZeroI64 => |v: NonZeroI64| v.get().into());\nimpl_configurable_numeric!(NonZeroUsize => |v: NonZeroUsize| v.get().into());\n\n// Arrays and maps.\nimpl<T> Configurable for Vec<T>\nwhere\n    T: Configurable + ToValue + 'static,\n{\n    fn metadata() -> Metadata {\n        T::metadata().convert()","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/vector-config/src/stdlib.rs#L183-L219","documentation":"The `ToValue` implementation for `f32`/`f64` converts a float to a `serde_json::Number` via `Number::from_f64`, which returns `None` for non-finite values (NaN, +inf, -inf) since JSON cannot represent them, and then panics. It signals that a floating-point config value being serialized to the event/value model is not a valid JSON number.","triggerScenarios":"Calling `to_value()` on an `f32`/`f64` that is NaN or infinite — e.g. a config value computed at runtime, a default derived from a division by zero, or an instrument/metric constant built from non-finite math.","commonSituations":"Config values computed as `0.0/0.0` (NaN) or overflow to infinity (e.g. `f64::MAX * 2.0`); parsing user input like \"inf\"/\"nan\" into f64 config fields; interpolating environment-driven defaults that produce non-finite results.","solutions":["Validate the float is finite before converting: `assert!(v.is_finite())` or return an error","Clamp or replace non-finite values with a sentinel (e.g. 0.0 or a configured default) at config-parse time","Fix the source computation producing NaN/inf (division by zero, overflow)","If parsing user/env input, reject non-finite strings during config validation"],"exampleFix":"// before\nlet ratio = numerator / denominator;\nconfig.threshold = ratio;\n// after\nlet ratio = if denominator == 0.0 { 0.0 } else { numerator / denominator };\nassert!(ratio.is_finite(), \"threshold must be a finite number\");\nconfig.threshold = ratio;","handlingStrategy":"validation","validationCode":"fn ensure_finite_json_number(v: f64) -> Result<f64, String> {\n    if v.is_finite() { Ok(v) } else { Err(format!(\"value {v} is not a valid JSON number\")) }\n}","typeGuard":"fn is_json_safe_f64(v: &f64) -> bool { v.is_finite() }","tryCatchPattern":"match Number::from_f64(v) {\n    Some(n) => Value::Number(n),\n    None => return Err(anyhow::anyhow!(\"cannot serialize non-finite float {v}\")),\n}","preventionTips":["Check `is_finite()` on any computed float before storing it in config or events","Reject \"inf\"/\"nan\" strings when parsing numeric config from user input","Guard divisions by zero and multiplication overflow in default-value computations"],"tags":["rust","json","floating-point"],"backgroundTag":"value-out-of-range","analyzedSha":"bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013","analyzedAt":"2026-09-16T02:53:35.741Z","contentChangedAt":"2026-09-16T02:53:35.741Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}