1
Fork 0

Compare commits

..

No commits in common. "39486e8503488a92b3e84af7cbf93fb67988e13f" and "ded56befb251e054a4756bfdfa82ca054b53fc40" have entirely different histories.

3 changed files with 24 additions and 13 deletions

View file

@ -6,12 +6,6 @@
== [Unreleased]
== [v0.2.1] - 2022-12-28
=== Fixed
- fix serializing Unicode
== [v0.2.0] - 2022-11-25
=== Added

View file

@ -1,6 +1,6 @@
[package]
name = "serde_sjson"
version = "0.2.1"
version = "0.2.0"
edition = "2021"
keywords = ["serde", "serialization", "sjson"]
description = "An SJSON serialization file format"

View file

@ -121,31 +121,48 @@ impl<'a> serde::ser::Serializer for &'a mut Serializer {
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
self.ensure_top_level_struct()?;
let needs_escapes =
v.is_empty() || v.contains([' ', '\n', '\r', '\t', '=', '\'', '"', '\\', '/']);
if needs_escapes {
self.output += "\"";
for c in v.chars() {
let len = v.len();
let chars = v.chars();
let mut start = 0;
for (i, c) in chars.enumerate() {
if ('\x20'..='\x7e').contains(&c)
&& !['\t', '\n', '\r', '\"', '\\', '/'].contains(&c)
{
continue;
}
self.output += &v[start..i];
self.output.push('\\');
match c {
'\t' => {
self.output.push('\\');
self.output.push('t');
}
'\n' => {
self.output.push('\\');
self.output.push('n');
}
'\r' => {
self.output.push('\\');
self.output.push('r');
}
'\x7f'.. => {
self.output += &format!("u{:4x}", c as u32);
}
c => {
self.output.push(c);
}
};
start = i + 1;
}
if start < len {
self.output += &v[start..];
}
self.output += "\"";