use std::fmt::Write as _;
use std::io;
use serde::Serialize;
use serde::ser::Error as _;
use serde_json::Value;
use serde_json::ser::Formatter;
pub fn to_string(value: &Value) -> Result<String, serde_json::Error> {
let mut buf = Vec::with_capacity(128);
let mut serializer = serde_json::Serializer::with_formatter(&mut buf, PyJsonFormatter);
value.serialize(&mut serializer)?;
String::from_utf8(buf).map_err(serde_json::Error::custom)
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PyJsonFormatter;
impl Formatter for PyJsonFormatter {
#[inline]
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}
#[inline]
fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}
#[inline]
fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + io::Write,
{
writer.write_all(b": ")
}
fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
where
W: ?Sized + io::Write,
{
writer.write_all(layout(&format!("{value:e}")).as_bytes())
}
fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
where
W: ?Sized + io::Write,
{
writer.write_all(python_repr(value).as_bytes())
}
}
fn python_repr(value: f64) -> String {
let shortest = format!("{value:e}");
match even_tie_alternative(value, &shortest) {
Some(even) => layout(&even),
None => layout(&shortest),
}
}
const EXACT_F64_DIGITS: usize = 767;
fn even_tie_alternative(value: f64, shortest: &str) -> Option<String> {
let (mantissa, exponent) = shortest.split_once('e')?;
let last = *mantissa.as_bytes().last()?;
if !last.is_ascii_digit() || last % 2 == 0 {
return None;
}
let mut lower = String::with_capacity(shortest.len());
lower.push_str(mantissa.get(..mantissa.len() - 1)?);
lower.push(char::from(last - 1));
lower.push('e');
lower.push_str(exponent);
if lower.parse::<f64>().ok()? != value {
return None;
}
let exact = format!("{value:.*e}", EXACT_F64_DIGITS - 1);
let (exact_mantissa, exact_exponent) = exact.split_once('e')?;
if exact_exponent != exponent {
return None;
}
let mut midpoint = significant_digits(lower.split_once('e')?.0);
midpoint.push('5');
let exact_digits = significant_digits(exact_mantissa);
let tail = exact_digits.strip_prefix(&midpoint)?;
if tail.bytes().all(|b| b == b'0') {
Some(lower)
} else {
None
}
}
fn significant_digits(mantissa: &str) -> String {
mantissa
.chars()
.filter(|c| c.is_ascii_digit())
.collect::<String>()
}
const MAX_FIXED_DECPT: i32 = 16;
const MIN_FIXED_DECPT: i32 = -3;
fn layout(exp_form: &str) -> String {
let Some((mantissa, exponent)) = exp_form.split_once('e') else {
return exp_form.to_owned();
};
let Ok(exponent) = exponent.parse::<i32>() else {
return exp_form.to_owned();
};
let (sign, mantissa) = match mantissa.strip_prefix('-') {
Some(rest) => ("-", rest),
None => ("", mantissa),
};
let digits: String = mantissa.chars().filter(|c| *c != '.').collect();
if digits.is_empty() {
return exp_form.to_owned();
}
let decpt = exponent + 1;
let mut out = String::with_capacity(digits.len() + sign.len() + 8);
out.push_str(sign);
if !(MIN_FIXED_DECPT..=MAX_FIXED_DECPT).contains(&decpt) {
let (lead, rest) = digits.split_at(1);
out.push_str(lead);
if !rest.is_empty() {
out.push('.');
out.push_str(rest);
}
out.push('e');
out.push(if exponent < 0 { '-' } else { '+' });
let magnitude = exponent.unsigned_abs();
if magnitude < 10 {
out.push('0');
}
let _ = write!(out, "{magnitude}");
return out;
}
if decpt <= 0 {
out.push_str("0.");
for _ in 0..-decpt {
out.push('0');
}
out.push_str(&digits);
} else if decpt as usize >= digits.len() {
let zeros = decpt as usize - digits.len();
out.push_str(&digits);
for _ in 0..zeros {
out.push('0');
}
out.push_str(".0");
} else {
let (whole, fraction) = digits.split_at(decpt as usize);
out.push_str(whole);
out.push('.');
out.push_str(fraction);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn py(value: &Value) -> String {
to_string(value).expect("serializing a Value to a Vec cannot fail")
}
fn float(value: f64) -> String {
py(&json!(value))
}
#[test]
fn separators_match_python_for_objects_and_arrays() {
assert_eq!(py(&json!({"a": 1, "b": 2})), r#"{"a": 1, "b": 2}"#);
assert_eq!(py(&json!([1, 2, 3])), "[1, 2, 3]");
}
#[test]
fn key_order_is_insertion_order_not_sorted() {
let value = json!({"zebra": 1, "apple": 2, "mango": 3, "beta": 4});
assert_eq!(
py(&value),
r#"{"zebra": 1, "apple": 2, "mango": 3, "beta": 4}"#
);
}
#[test]
fn nesting_gets_separators_at_every_level() {
let value = json!({
"outer": {"b": [1, 2, {"z": true, "a": null}], "a": "x"},
"list": [[], {}, [1]],
});
assert_eq!(
py(&value),
r#"{"outer": {"b": [1, 2, {"z": true, "a": null}], "a": "x"}, "list": [[], {}, [1]]}"#
);
}
#[test]
fn empty_containers_have_no_stray_separator() {
assert_eq!(py(&json!({})), "{}");
assert_eq!(py(&json!([])), "[]");
assert_eq!(py(&json!({"a": {}, "b": []})), r#"{"a": {}, "b": []}"#);
}
#[test]
fn scalars_render_as_python_spells_them() {
assert_eq!(py(&Value::Null), "null");
assert_eq!(py(&json!(true)), "true");
assert_eq!(py(&json!(false)), "false");
assert_eq!(py(&json!(42)), "42");
assert_eq!(py(&json!(-7)), "-7");
}
#[test]
fn strings_are_raw_except_the_json_mandatory_escapes() {
let input = "q\"b\\s\nt\ts/ \u{7f} \u{a0} \u{2028} \u{1f980} 世界";
let expected = concat!(
r#""q\"b\\s\nt\ts/ "#,
"\u{7f} \u{a0} \u{2028} \u{1f980} 世界\""
);
assert_eq!(py(&json!(input)), expected);
}
#[test]
fn c0_controls_use_pythons_escape_set() {
let input: String = (0..0x20u8).map(char::from).collect();
assert_eq!(
py(&json!(input)),
r#""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f""#
);
}
#[test]
fn floats_serde_json_already_agreed_on_still_agree() {
assert_eq!(float(1e16), "1e+16");
assert_eq!(float(1e21), "1e+21");
assert_eq!(float(1e30), "1e+30");
assert_eq!(float(1e100), "1e+100");
assert_eq!(float(-0.0), "-0.0");
assert_eq!(float(100.0), "100.0");
assert_eq!(float(1e15), "1000000000000000.0");
}
#[test]
fn floats_serde_json_got_wrong_now_match_python() {
assert_eq!(float(1e-5), "1e-05");
assert_eq!(float(1e-6), "1e-06");
assert_eq!(float(1e-7), "1e-07");
assert_eq!(float(1.5e-7), "1.5e-07");
}
#[test]
fn exponential_form_starts_exactly_where_python_switches() {
assert_eq!(float(9999999999999998.0), "9999999999999998.0");
assert_eq!(float(1e17), "1e+17");
assert_eq!(float(1e-3), "0.001");
assert_eq!(float(1e-4), "0.0001");
assert_eq!(float(9.99e-5), "9.99e-05");
assert_eq!(float(2.5e-8), "2.5e-08");
}
#[test]
fn ordinary_and_signed_floats_keep_their_shortest_digits() {
assert_eq!(float(0.0), "0.0");
assert_eq!(float(0.1), "0.1");
assert_eq!(float(1.5), "1.5");
assert_eq!(float(-2.5), "-2.5");
assert_eq!(float(123.456), "123.456");
assert_eq!(float(1.2345678901234567), "1.2345678901234567");
assert_eq!(float(123456789.0), "123456789.0");
assert_eq!(float(0.1 + 0.2), "0.30000000000000004");
assert_eq!(float(-1e-6), "-1e-06");
}
#[test]
fn extreme_floats_need_no_exponent_padding() {
assert_eq!(float(f64::MAX), "1.7976931348623157e+308");
assert_eq!(float(f64::MIN_POSITIVE), "2.2250738585072014e-308");
assert_eq!(float(1e-320), "1e-320");
assert_eq!(float(5e-324), "5e-324");
}
#[test]
fn exact_midpoint_ties_round_the_last_digit_to_even() {
assert_eq!(
float(f64::from_bits(0x430d_d958_566c_69ea)),
"1050218469363005.2"
);
assert_eq!(
float(f64::from_bits(0x42ea_7939_a330_de54)),
"232863682889458.62"
);
assert_eq!(
float(f64::from_bits(0xc2b7_ae7d_2b3f_3310)),
"-26038191734579.062"
);
assert_eq!(
float(f64::from_bits(0xc308_c375_b9c2_e7ea)),
"-871288729525501.2"
);
}
#[test]
fn near_ties_keep_the_shortest_digits_untouched() {
assert_eq!(float(5e-324), "5e-324");
assert_eq!(float(0.3), "0.3");
assert_eq!(float(1e-7), "1e-07");
assert_eq!(float(9.007199254740993e15), "9007199254740992.0");
}
#[test]
fn known_limit_big_integers_collapse_to_f64() {
let value: Value =
serde_json::from_str("10000000000000000000000000").expect("valid JSON number");
assert!(value.is_f64());
assert_eq!(py(&value), "1e+25");
}
#[test]
fn non_finite_floats_cannot_enter_a_value() {
assert!(serde_json::Number::from_f64(f64::NAN).is_none());
assert!(serde_json::Number::from_f64(f64::INFINITY).is_none());
assert!(serde_json::Number::from_f64(f64::NEG_INFINITY).is_none());
}
#[test]
fn qwen_tool_definition_matches_python_end_to_end() {
let tool = json!({
"type": "function",
"function": {
"name": "get_current_temperature",
"description": "Get current temperature at a location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the temperature for, in the format \"City, State, Country\"."
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit to return the temperature in.",
"default": "celsius"
},
"tolerance": {"type": "number", "default": 1e-6}
},
"required": ["location"]
}
}
});
assert_eq!(
py(&tool),
concat!(
r#"{"type": "function", "function": {"name": "get_current_temperature", "#,
r#""description": "Get current temperature at a location.", "parameters": "#,
r#"{"type": "object", "properties": {"location": {"type": "string", "#,
r#""description": "The location to get the temperature for, in the format "#,
r#"\"City, State, Country\"."}, "unit": {"type": "string", "enum": "#,
r#"["celsius", "fahrenheit"], "description": "The unit to return the "#,
r#"temperature in.", "default": "celsius"}, "tolerance": {"type": "number", "#,
r#""default": 1e-06}}, "required": ["location"]}}}"#
)
);
}
}