Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions src/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
use std::io;

use crate::Codec;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

#[derive(Default)]
struct ShopifyJsonFormatter;

impl serde_json::ser::Formatter for ShopifyJsonFormatter {
fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
where
W: ?Sized + io::Write,
{
let mut start = 0;
for (index, character) in fragment.char_indices() {
match character {
'/' => {
writer.write_all(&fragment.as_bytes()[start..index])?;
writer.write_all(br"\/")?;
start = index + character.len_utf8();
}
'\u{2028}' => {
writer.write_all(&fragment.as_bytes()[start..index])?;
writer.write_all(br"\u2028")?;
start = index + character.len_utf8();
}
'\u{2029}' => {
writer.write_all(&fragment.as_bytes()[start..index])?;
writer.write_all(br"\u2029")?;
start = index + character.len_utf8();
}
_ => {}
}
}
writer.write_all(&fragment.as_bytes()[start..])
}
}

fn to_shopify_json_vec(value: &serde_json::Value) -> Result<Vec<u8>> {
let mut bytes = Vec::new();
let formatter = ShopifyJsonFormatter;
let mut serializer = serde_json::Serializer::with_formatter(&mut bytes, formatter);
value
.serialize(&mut serializer)
.map_err(|e| anyhow!("Couldn't serialize JSON: {}", e))?;
Ok(bytes)
}

#[derive(Debug, Clone, Default)]
pub enum BytesContainerType {
/// Input bytes.
Expand Down Expand Up @@ -64,8 +109,7 @@ impl BytesContainer {
BytesContainerType::Input => {
let json = serde_json::from_slice::<serde_json::Value>(&raw)
.map_err(|e| anyhow!("Invalid input JSON: {}", e))?;
let minified_buffer = serde_json::to_vec(&json)
.map_err(|e| anyhow!("Couldn't serialize JSON: {}", e))?;
let minified_buffer = to_shopify_json_vec(&json)?;

Ok(Self {
codec,
Expand Down Expand Up @@ -136,3 +180,52 @@ impl BytesContainer {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn json_input_escapes_solidus_like_shopify_json() -> Result<()> {
let input = br#"{"id":"gid://shopify/Product/1"}"#.to_vec();

let container = BytesContainer::new(BytesContainerType::Input, Codec::Json, input)?;

assert_eq!(
String::from_utf8(container.raw).unwrap(),
r#"{"id":"gid:\/\/shopify\/Product\/1"}"#
);

Ok(())
}

#[test]
fn json_input_escapes_line_and_paragraph_separators_like_shopify_json() -> Result<()> {
let input = "{\"line\":\"before\u{2028}after\",\"paragraph\":\"before\u{2029}after\"}"
.as_bytes()
.to_vec();

let container = BytesContainer::new(BytesContainerType::Input, Codec::Json, input)?;

assert_eq!(
String::from_utf8(container.raw).unwrap(),
r#"{"line":"before\u2028after","paragraph":"before\u2029after"}"#
);

Ok(())
}

#[test]
fn json_input_preserves_object_order() -> Result<()> {
let input = br#"{"b":1,"a":2}"#.to_vec();

let container = BytesContainer::new(BytesContainerType::Input, Codec::Json, input)?;

assert_eq!(
String::from_utf8(container.raw).unwrap(),
r#"{"b":1,"a":2}"#
);

Ok(())
}
}
Loading