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.
This commit is contained in:
parent
ded56befb2
commit
f76acf5407
2 changed files with 10 additions and 23 deletions
|
@ -6,6 +6,10 @@
|
||||||
|
|
||||||
== [Unreleased]
|
== [Unreleased]
|
||||||
|
|
||||||
|
=== Fixed
|
||||||
|
|
||||||
|
- fix serializing Unicode
|
||||||
|
|
||||||
== [v0.2.0] - 2022-11-25
|
== [v0.2.0] - 2022-11-25
|
||||||
|
|
||||||
=== Added
|
=== Added
|
||||||
|
|
29
src/ser.rs
29
src/ser.rs
|
@ -121,48 +121,31 @@ impl<'a> serde::ser::Serializer for &'a mut Serializer {
|
||||||
|
|
||||||
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
|
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
|
||||||
self.ensure_top_level_struct()?;
|
self.ensure_top_level_struct()?;
|
||||||
|
|
||||||
let needs_escapes =
|
let needs_escapes =
|
||||||
v.is_empty() || v.contains([' ', '\n', '\r', '\t', '=', '\'', '"', '\\', '/']);
|
v.is_empty() || v.contains([' ', '\n', '\r', '\t', '=', '\'', '"', '\\', '/']);
|
||||||
|
|
||||||
if needs_escapes {
|
if needs_escapes {
|
||||||
self.output += "\"";
|
self.output += "\"";
|
||||||
|
|
||||||
let len = v.len();
|
for c in v.chars() {
|
||||||
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 {
|
match c {
|
||||||
'\t' => {
|
'\t' => {
|
||||||
|
self.output.push('\\');
|
||||||
self.output.push('t');
|
self.output.push('t');
|
||||||
}
|
}
|
||||||
'\n' => {
|
'\n' => {
|
||||||
|
self.output.push('\\');
|
||||||
self.output.push('n');
|
self.output.push('n');
|
||||||
}
|
}
|
||||||
'\r' => {
|
'\r' => {
|
||||||
|
self.output.push('\\');
|
||||||
self.output.push('r');
|
self.output.push('r');
|
||||||
}
|
}
|
||||||
'\x7f'.. => {
|
|
||||||
self.output += &format!("u{:4x}", c as u32);
|
|
||||||
}
|
|
||||||
c => {
|
c => {
|
||||||
self.output.push(c);
|
self.output.push(c);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
start = i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if start < len {
|
|
||||||
self.output += &v[start..];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.output += "\"";
|
self.output += "\"";
|
||||||
|
|
Loading…
Add table
Reference in a new issue