use std::fmt::{self, Write as _};
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Json {
Null,
Bool(bool),
Int(i64),
Real(f64),
Str(String),
Array(Vec<Json>),
Object(Vec<(String, Json)>),
}
impl Json {
pub(crate) fn object<K: Into<String>>(fields: impl IntoIterator<Item = (K, Json)>) -> Self {
Self::Object(
fields
.into_iter()
.map(|(key, value)| (key.into(), value))
.collect(),
)
}
pub(crate) fn str(text: impl Into<String>) -> Self {
Self::Str(text.into())
}
pub(crate) fn opt_str(text: Option<impl Into<String>>) -> Self {
text.map_or(Self::Null, |text| Self::Str(text.into()))
}
pub(crate) fn uint(value: u64) -> Self {
Self::Int(i64::try_from(value).unwrap_or(i64::MAX))
}
pub(crate) fn opt_uint(value: Option<u64>) -> Self {
value.map_or(Self::Null, Self::uint)
}
pub(crate) fn pretty(&self) -> String {
let mut out = String::new();
self.write(&mut out, 0).expect("writing to a String");
out.push('\n');
out
}
fn write(&self, out: &mut String, depth: usize) -> fmt::Result {
match self {
Self::Null => out.write_str("null"),
Self::Bool(value) => write!(out, "{value}"),
Self::Int(value) => write!(out, "{value}"),
Self::Real(value) if value.is_finite() => {
write!(out, "{value:?}")
}
Self::Real(_) => out.write_str("null"),
Self::Str(text) => write_string(out, text),
Self::Array(items) if items.is_empty() => out.write_str("[]"),
Self::Array(items) => {
out.write_str("[\n")?;
for (index, item) in items.iter().enumerate() {
indent(out, depth + 1);
item.write(out, depth + 1)?;
out.write_str(if index + 1 < items.len() { ",\n" } else { "\n" })?;
}
indent(out, depth);
out.write_str("]")
}
Self::Object(fields) if fields.is_empty() => out.write_str("{}"),
Self::Object(fields) => {
out.write_str("{\n")?;
for (index, (key, value)) in fields.iter().enumerate() {
indent(out, depth + 1);
write_string(out, key)?;
out.write_str(": ")?;
value.write(out, depth + 1)?;
out.write_str(if index + 1 < fields.len() {
",\n"
} else {
"\n"
})?;
}
indent(out, depth);
out.write_str("}")
}
}
}
}
fn indent(out: &mut String, depth: usize) {
for _ in 0..depth {
out.push_str(" ");
}
}
fn write_string(out: &mut String, text: &str) -> fmt::Result {
out.push('"');
for c in text.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if u32::from(c) < 0x20 => write!(out, "\\u{:04x}", u32::from(c))?,
c => out.push(c),
}
}
out.push('"');
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strings_and_numbers_are_escaped_and_exact() {
let value = Json::object([
("text", Json::str("a\"b\\c\n\u{1}é")),
("real", Json::Real(2.0)),
("nan", Json::Real(f64::NAN)),
("list", Json::Array(vec![Json::Int(1), Json::Null])),
("empty", Json::Array(Vec::new())),
]);
assert_eq!(
value.pretty(),
"{\n \"text\": \"a\\\"b\\\\c\\n\\u0001é\",\n \"real\": 2.0,\n \"nan\": null,\n \
\"list\": [\n 1,\n null\n ],\n \"empty\": []\n}\n"
);
}
}