Skip to content

Commit 0990666

Browse files
committed
tweaking serialization a bit more
1 parent e40c1e1 commit 0990666

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/ser.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ impl<'py> Serialize for SerializePyObject<'py> {
6565
} else if ob_type == lookup.float {
6666
serialize!(f64)
6767
} else if ob_type == lookup.string {
68-
serialize!(&str)
69-
} else if ob_type == lookup.bytes || ob_type == lookup.bytearray {
70-
serialize!(&[u8])
68+
let py_str: &PyString = self.obj.cast_as().map_err(map_py_err)?;
69+
let s = py_str.to_str().map_err(map_py_err)?;
70+
serializer.serialize_str(s)
7171
} else if ob_type == lookup.dict {
7272
let py_dict: &PyDict = self.obj.cast_as().map_err(map_py_err)?;
7373

@@ -139,6 +139,8 @@ impl<'py> Serialize for SerializePyObject<'py> {
139139
Ok(dt) => dt.serialize(serializer),
140140
Err(e) => serde_err!("unable to convert time string to TOML time object {:?}", e),
141141
}
142+
} else if ob_type == lookup.bytes || ob_type == lookup.bytearray {
143+
serialize!(&[u8])
142144
} else {
143145
serde_err!("{} is not serializable to TOML", any_repr(self.obj))
144146
}

tests/test_dump.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
({'x': {'a': 1}, 'y': 4}, 'y = 4\n\n[x]\na = 1\n'),
2121
((1, 2, 3), '[1, 2, 3]'),
2222
({'emoji': '😷'}, 'emoji = "😷"\n'),
23+
({'bytes': b'123'}, 'bytes = [49, 50, 51]\n'), # TODO: should this be a string of "123"
2324
({'polish': 'Witaj świecie'}, 'polish = "Witaj świecie"\n'),
2425
],
2526
)

0 commit comments

Comments
 (0)