Skip to content

Commit

Permalink
Update serialize_json.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xb-s authored Dec 1, 2024
1 parent 1f2ea65 commit e0da84a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/hurlfmt/src/format/serialize_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl JValue {
JValue::Object(key_values) => {
let s = key_values
.iter()
.map(|(k, v)| format!("\"{}\":{}", k, v.format()))
.map(|(k, v)| {
format!(
"\"{}\":{}",
k.chars().map(format_char).collect::<String>(),
v.format()
)
})
.collect::<Vec<String>>()
.join(",");
format!("{{{s}}}")
Expand Down Expand Up @@ -119,6 +125,18 @@ pub mod tests {
"[1,2,3]"
);
}
#[test]
pub fn test_format_special_characters() {
let value = JValue::Object(vec![(
"sp\"ecial\\key".to_string(),
JValue::String("sp\nvalue\twith\x08control".to_string()),
)]);

assert_eq!(
value.format(),
r#"{"sp\"ecial\\key":"sp\nvalue\twith\bcontrol"}"#
);
}

#[test]
pub fn format_object() {
Expand Down

0 comments on commit e0da84a

Please sign in to comment.