remove unmaintained json crate, do our own string escaping

This commit is contained in:
xenofem 2023-08-06 15:16:37 -04:00
parent feb5e4979b
commit 5999d54842
3 changed files with 20 additions and 11 deletions

7
Cargo.lock generated
View file

@ -1059,12 +1059,6 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]] [[package]]
name = "language-tags" name = "language-tags"
version = "0.3.2" version = "0.3.2"
@ -1416,7 +1410,6 @@ dependencies = [
"actix-web", "actix-web",
"bytes", "bytes",
"futures", "futures",
"json",
"lazy_static", "lazy_static",
"log", "log",
"pdf", "pdf",

View file

@ -12,7 +12,6 @@ actix-files = "0.6.0"
actix-web = "4.0.1" actix-web = "4.0.1"
bytes = "1.1" bytes = "1.1"
futures = "0.3" futures = "0.3"
json = "0.12.4"
lazy_static = "1.4" lazy_static = "1.4"
log = "0.4.16" log = "0.4.16"
pdf = "0.8" pdf = "0.8"

View file

@ -66,7 +66,7 @@ impl DataFormat for Csv {
fn header(dataset: &DataSet) -> SerializationChunk { fn header(dataset: &DataSet) -> SerializationChunk {
let mut header = String::from("Date"); let mut header = String::from("Date");
for column in dataset.columns.iter() { for column in dataset.columns.iter() {
write!(&mut header, ",{}", column)?; write!(&mut header, r#","{}""#, column.replace('"', r#""""#))?;
} }
writeln!(&mut header)?; writeln!(&mut header)?;
Ok(header) Ok(header)
@ -97,7 +97,7 @@ impl DataFormat for Json {
fn header(dataset: &DataSet) -> SerializationChunk { fn header(dataset: &DataSet) -> SerializationChunk {
let mut header = String::from(r#"{"columns":["Date""#); let mut header = String::from(r#"{"columns":["Date""#);
for column in dataset.columns.iter() { for column in dataset.columns.iter() {
write!(&mut header, ",{}", json::stringify(column.as_str()))?; write!(&mut header, ",{}", escaped_json_string(column.as_str()))?;
} }
write!(&mut header, r#"],"rows":["#)?; write!(&mut header, r#"],"rows":["#)?;
Ok(header) Ok(header)
@ -115,7 +115,7 @@ impl DataFormat for Json {
)?; )?;
for column in dataset.columns.iter() { for column in dataset.columns.iter() {
if let Some(val) = datapoint.values.get(column) { if let Some(val) = datapoint.values.get(column) {
write!(&mut row, ",{}:{}", json::stringify(column.as_str()), val)?; write!(&mut row, ",{}:{}", escaped_json_string(column.as_str()), val)?;
} }
} }
row += "}"; row += "}";
@ -125,3 +125,20 @@ impl DataFormat for Json {
const ROW_SEPARATOR: &'static str = ","; const ROW_SEPARATOR: &'static str = ",";
const END: &'static str = "]}"; const END: &'static str = "]}";
} }
fn escaped_json_string(s: &str) -> String {
s.chars().fold(String::from(r#"""#), |mut acc, c| {
match c {
'"' => acc.push_str(r#"\""#),
'\\' => acc.push_str(r"\\"),
'\t' => acc.push_str(r"\t"),
'\n' => acc.push_str(r"\n"),
_ => if c.is_ascii_control() {
acc.push_str(&format!(r"\u{:04X}", c as u32))
} else {
acc.push(c)
}
}
acc
}) + r#"""#
}