use std::sync::Arc;
use unityrs_core::limits::{ChargeError, Meter, MeteredVec};
use unityrs_core::pyfloat::py_float_repr;
use unityrs_core::{Map, Parsed, PrimArray, Value};
pub trait Sink {
fn put(&mut self, bytes: &[u8]);
fn check(&self) -> Result<(), ChargeError> {
Ok(())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Count(pub u64);
impl Sink for Count {
fn put(&mut self, bytes: &[u8]) {
let n = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
self.0 = self.0.saturating_add(n);
}
}
impl Sink for Vec<u8> {
fn put(&mut self, bytes: &[u8]) {
self.extend_from_slice(bytes);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error("a bytes value, which json.dumps cannot write")]
pub struct Unwritable;
#[derive(Clone, Copy, Debug)]
pub struct Options {
pub indent: usize,
pub ascii_only: bool,
}
impl Options {
pub const DEFAULT: Self = Self {
indent: 2,
ascii_only: true,
};
pub const MONO_BEHAVIOUR: Self = Self {
indent: 4,
ascii_only: false,
};
}
fn newline_with(level: usize, options: Options, out: &mut impl Sink) {
out.put(b"\n");
for _ in 0..level {
for _ in 0..options.indent {
if out.check().is_err() {
return;
}
out.put(b" ");
}
}
}
pub fn newline(level: usize, out: &mut impl Sink) {
out.put(b"\n");
for _ in 0..level {
out.put(b" ");
}
}
pub fn float(x: f64, out: &mut impl Sink) {
super::pyjson_numbers::float(x, out);
}
#[must_use]
pub fn float_repr(x: f64) -> String {
if x.is_nan() {
return "NaN".to_owned();
}
if x.is_infinite() {
return if x < 0.0 { "-Infinity" } else { "Infinity" }.to_owned();
}
py_float_repr(x)
}
pub fn decode_escaped(bytes: &[u8], mut each: impl FnMut(Result<char, u8>)) {
decode_escaped_while(bytes, |c| {
each(c);
true
});
}
fn decode_escaped_while(bytes: &[u8], mut each: impl FnMut(Result<char, u8>) -> bool) {
let mut rest = bytes;
while !rest.is_empty() {
let (chunk, after_chunk) = rest.split_at(rest.len().min(1024));
match std::str::from_utf8(chunk) {
Ok(s) => {
for c in s.chars() {
if !each(Ok(c)) {
return;
}
}
rest = after_chunk;
}
Err(e) => {
let valid = e.valid_up_to();
let (good, tail) = rest.split_at(valid);
if let Ok(s) = std::str::from_utf8(good) {
for c in s.chars() {
if !each(Ok(c)) {
return;
}
}
}
if e.error_len().is_none() && chunk.len() < rest.len() {
rest = tail;
continue;
}
let bad = e.error_len().unwrap_or(tail.len()).min(tail.len());
let (invalid, after) = tail.split_at(bad);
for &b in invalid {
if !each(Err(b)) {
return;
}
}
rest = after;
}
}
}
}
pub fn string(bytes: &[u8], out: &mut impl Sink) {
string_with(bytes, true, out);
}
fn string_with(bytes: &[u8], ascii_only: bool, out: &mut impl Sink) {
out.put(b"\"");
decode_escaped_while(bytes, |item| {
if out.check().is_err() {
return false;
}
match item {
Ok(c) if !ascii_only && c >= ' ' => match c {
'"' | '\\' => char_escaped(c, out),
_ => {
let mut buf = [0; 4];
out.put(c.encode_utf8(&mut buf).as_bytes());
}
},
Ok(c) => char_escaped(c, out),
Err(b) if !ascii_only => out.put(&[b]),
Err(b) => unit(0xDC00 + u32::from(b), out),
}
out.check().is_ok()
});
out.put(b"\"");
}
fn char_escaped(c: char, out: &mut impl Sink) {
match c {
'"' => out.put(b"\\\""),
'\\' => out.put(b"\\\\"),
'\n' => out.put(b"\\n"),
'\r' => out.put(b"\\r"),
'\t' => out.put(b"\\t"),
'\u{8}' => out.put(b"\\b"),
'\u{c}' => out.put(b"\\f"),
' '..='~' => {
let mut buf = [0u8; 4];
out.put(c.encode_utf8(&mut buf).as_bytes());
}
_ => {
let mut units = [0u16; 2];
for u in c.encode_utf16(&mut units) {
unit(u32::from(*u), out);
}
}
}
}
fn unit(u: u32, out: &mut impl Sink) {
let hex = b"0123456789abcdef";
let mut bytes = *b"\\u0000";
for (i, dst) in bytes.iter_mut().skip(2).enumerate() {
*dst = hex
.get(((u >> (12 - i * 4)) & 15) as usize)
.copied()
.unwrap_or(b'0');
}
out.put(&bytes);
}
pub fn int(i: i128, out: &mut impl Sink) {
super::pyjson_numbers::int(i, out);
}
fn f32_value(bits: u32) -> f64 {
f64::from(f32::from_bits(bits))
}
fn prim(arr: &PrimArray, i: usize, out: &mut impl Sink) {
match arr {
PrimArray::Bool(v) => {
if let Some(&b) = v.get(i) {
out.put(if b { b"true" } else { b"false" });
}
}
PrimArray::U8(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::I8(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::U16(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::I16(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::U32(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::I32(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::U64(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::I64(v) => v.get(i).into_iter().for_each(|&x| int(x.into(), out)),
PrimArray::F32(v) => v.get(i).into_iter().for_each(|&x| float(f32_value(x), out)),
PrimArray::F64(v) => v
.get(i)
.into_iter()
.for_each(|&x| float(f64::from_bits(x), out)),
}
}
enum Items<'a> {
Seq(&'a [Value]),
Dict(&'a Map),
Prims(&'a PrimArray),
}
impl Items<'_> {
fn len(&self) -> usize {
match self {
Self::Seq(v) => v.len(),
Self::Dict(m) => m.len(),
Self::Prims(p) => p.len(),
}
}
}
struct Frame<'a> {
items: Items<'a>,
next: usize,
level: usize,
}
fn open(value: &Value) -> Option<Items<'_>> {
let items = match value {
Value::List(v) => Items::Seq(v),
Value::Tuple(v) => Items::Seq(v),
Value::Dict(m) => Items::Dict(m),
Value::Prims(p) => Items::Prims(p),
_ => return None,
};
Some(items)
}
fn begin<'a>(
parsed: &Parsed,
value: &'a Value,
level: usize,
options: Options,
out: &mut impl Sink,
) -> Result<Option<Frame<'a>>, WriteError> {
out.check()?;
let _ = parsed;
match value {
Value::Bool(b) => out.put(if *b { b"true" } else { b"false" }),
Value::Int(i) => int((*i).into(), out),
Value::UInt(u) => int((*u).into(), out),
Value::F32(bits) => float(f32_value(*bits), out),
Value::F64(bits) => float(f64::from_bits(*bits), out),
Value::Str(s) => string_with(s.as_bytes(), options.ascii_only, out),
Value::Bytes(_) => return Err(Unwritable.into()),
_ => {
let Some(items) = open(value) else {
return Err(Unwritable.into());
};
let dict = matches!(items, Items::Dict(_));
if items.len() == 0 {
out.put(if dict { b"{}" } else { b"[]" });
return Ok(None);
}
out.put(if dict { b"{" } else { b"[" });
return Ok(Some(Frame {
items,
next: 0,
level,
}));
}
}
out.check()?;
Ok(None)
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum WriteError {
#[error(transparent)]
Unwritable(#[from] Unwritable),
#[error("the JSON writer's stack: {0}")]
Charge(#[from] ChargeError),
}
pub fn value(
parsed: &Parsed,
value: &Value,
level: usize,
out: &mut impl Sink,
meter: &Arc<Meter>,
) -> Result<(), WriteError> {
value_with(parsed, value, level, Options::DEFAULT, out, meter)
}
pub fn value_with(
parsed: &Parsed,
value: &Value,
level: usize,
options: Options,
out: &mut impl Sink,
meter: &Arc<Meter>,
) -> Result<(), WriteError> {
let mut stack: MeteredVec<Frame<'_>> = MeteredVec::new(meter);
if let Some(f) = begin(parsed, value, level, options, out)? {
out.check()?;
stack.push(f)?;
}
while let Some(top) = stack.as_mut_slice().last_mut() {
out.check()?;
let i = top.next;
let inner = top.level + 1;
if i >= top.items.len() {
let close: &[u8] = if matches!(top.items, Items::Dict(_)) {
b"}"
} else {
b"]"
};
let lvl = top.level;
stack.truncate(stack.len().saturating_sub(1));
newline_with(lvl, options, out);
out.put(close);
continue;
}
top.next += 1;
if i > 0 {
out.put(b",");
}
newline_with(inner, options, out);
let child = match &top.items {
Items::Prims(p) => {
prim(p, i, out);
None
}
Items::Seq(v) => v.get(i),
Items::Dict(m) => match m.entries().get(i) {
Some((k, v)) => {
string_with(parsed.key_name(m, *k), options.ascii_only, out);
out.put(b": ");
Some(v)
}
None => None,
},
};
if let Some(v) = child
&& let Some(f) = begin(parsed, v, inner, options, out)?
{
out.check()?;
stack.push(f)?;
}
}
out.check()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::{Count, decode_escaped, float_repr, string};
#[test]
fn chunk_boundaries_preserve_utf8_and_surrogateescape() {
for prefix_len in 1020..1026 {
let prefix = "a".repeat(prefix_len);
let mut input = format!("{prefix}é😀\u{2028}\n").into_bytes();
input.extend_from_slice(b"\xe4\xb8\xff");
assert_eq!(
json(&input),
format!("\"{prefix}\\u00e9\\ud83d\\ude00\\u2028\\n\\udce4\\udcb8\\udcff\"")
);
let mut output = Vec::new();
super::string_with(&input, false, &mut output);
let mut expected = format!("\"{prefix}é😀\u{2028}\\n").into_bytes();
expected.extend_from_slice(b"\xe4\xb8\xff\"");
assert_eq!(output, expected);
}
}
#[test]
fn floats_are_written_as_python_reprs_them() {
for (x, want) in [
(1.0, "1.0"),
(0.1, "0.1"),
(1e-05, "1e-05"),
(0.0001, "0.0001"),
(1e16, "1e+16"),
(1e15, "1000000000000000.0"),
(-0.0, "-0.0"),
(0.0, "0.0"),
(f64::NAN, "NaN"),
(f64::INFINITY, "Infinity"),
(f64::NEG_INFINITY, "-Infinity"),
(f64::from(f32::MAX), "3.4028234663852886e+38"),
(f64::from(0.1f32), "0.10000000149011612"),
(f64::from(1e-5f32), "9.999999747378752e-06"),
(16_777_216.0, "16777216.0"),
(5e-324, "5e-324"),
(1.5, "1.5"),
(-2.5e-7, "-2.5e-07"),
(f64::MAX, "1.7976931348623157e+308"),
] {
assert_eq!(float_repr(x), want, "{x:e}");
}
}
#[test]
fn ties_go_to_the_even_digits_and_powers_of_two_to_the_nearest() {
for (exact, want) in [
("123456789012345680.0", "1.2345678901234568e+17"),
("1551563132437320.25", "1551563132437320.2"),
("-1551563132437320.25", "-1551563132437320.2"),
("79686618578806.625", "79686618578806.62"),
("-22620844907483.3125", "-22620844907483.312"),
("2.98023223876953125e-8", "2.9802322387695312e-08"),
("-5.9604644775390625e-7", "-5.960464477539062e-07"),
("9007199254740992.0", "9007199254740992.0"),
] {
let x: f64 = exact.parse().unwrap();
assert_eq!(float_repr(x), want, "{exact}");
}
}
fn json(bytes: &[u8]) -> String {
let mut out = Vec::new();
string(bytes, &mut out);
String::from_utf8(out).unwrap()
}
#[test]
fn strings_escape_as_ensure_ascii_with_surrogateescape() {
const B: char = '\\';
assert_eq!(json(b"a\"b\\c"), r#""a\"b\\c""#);
assert_eq!(
json(b"\n\r\t\x08\x0c\x01\x7f"),
r#""\n\r\t\b\f\u0001\u007f""#
);
assert_eq!(
json("\u{e4}\u{7cbe}".as_bytes()),
format!("\"{B}u00e4{B}u7cbe\"")
);
assert_eq!(
json("\u{1f600}".as_bytes()),
format!("\"{B}ud83d{B}ude00\"")
);
assert_eq!(json(b"bad\xff"), r#""bad\udcff""#);
assert_eq!(json(b"\xe4\xb8a"), r#""\udce4\udcb8a""#);
}
#[test]
fn decoding_gives_one_item_per_invalid_byte() {
let mut n = 0;
decode_escaped(b"\xf0\x80x\xc3", |_| n += 1);
assert_eq!(n, 4);
let mut c = Count::default();
string(b"ab", &mut c);
assert_eq!(c.0, 4);
}
}