use serde_json::ser::Formatter;
use std::io;
pub struct PythonFormatter;
impl Formatter for PythonFormatter {
#[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": ")
}
#[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", ")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Serialize;
use serde_json::ser::Serializer;
use serde_json::Value;
fn serialize(v: &Value) -> String {
let mut buf = Vec::new();
let mut ser = Serializer::with_formatter(&mut buf, PythonFormatter);
v.serialize(&mut ser).unwrap();
String::from_utf8(buf).unwrap()
}
#[test]
fn matches_python_separators() {
let v: Value = serde_json::json!({"level": "INFO", "exit": 0, "alts": ["a", "b"]});
let s = serialize(&v);
assert!(s.contains("\"level\": \"INFO\""), "colon must be ': ': {s}");
assert!(s.contains("\"exit\": 0,"), "comma must be ', ': {s}");
assert!(s.contains("[\"a\", \"b\"]"), "array sep must be ', ': {s}");
}
#[test]
fn no_unicode_escaping() {
let v: Value = serde_json::json!({"msg": "中文 ✓"});
let s = serialize(&v);
assert!(s.contains("中文"));
assert!(!s.contains("\\u"));
}
}