1
Fork 0

Compare commits

..

2 commits

Author SHA1 Message Date
39486e8503
version: v0.2.1 2022-12-28 19:48:42 +01:00
f76acf5407
fix: Fix serializing Unicode
The index operator doesn't use the `char` boundaries, but rather
byte boundaries. So I switched back to a simpler, but slightly
less efficient loop that simply adds individual characters to the
output.

It also doesn't escape Unicode anymore, as this shouldn't be an issue
in UTF-8 encoded output files.
2022-12-28 19:48:01 +01:00
3 changed files with 13 additions and 24 deletions

View file

@ -6,6 +6,12 @@
== [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.0"
version = "0.2.1"
edition = "2021"
keywords = ["serde", "serialization", "sjson"]
description = "An SJSON serialization file format"

View file

@ -121,48 +121,31 @@ 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 += "\"";
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('\\');
for c in v.chars() {
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 += "\"";