Skip to content

Commit 2626c03

Browse files
authored
Merge pull request #70 from sigmaris/serialize-tuple-structs
Add support for serialising tuple structs
2 parents b56c7b4 + 5100a7e commit 2626c03

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Support for serializing tuple structs. These are serialized as JSON arrays,
13+
which matches `serde_json` behaviour.
14+
815
## [v0.5.0] - 2022-11-04
916

1017
### Changed
@@ -61,6 +68,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6168

6269
Initial release
6370

71+
[Unreleased]: https://github.com/rust-embedded-community/serde-json-core/compare/v0.5.0...HEAD
6472
[v0.5.0]: https://github.com/rust-embedded-community/serde-json-core/compare/v0.4.0...v0.5.0
6573
[v0.4.0]: https://github.com/rust-embedded-community/serde-json-core/compare/v0.3.0...v0.4.0
6674
[v0.3.0]: https://github.com/rust-embedded-community/serde-json-core/compare/v0.2.0...v0.3.0

src/ser/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'a, 'b: 'a> ser::Serializer for &'a mut Serializer<'b> {
178178
type Error = Error;
179179
type SerializeSeq = SerializeSeq<'a, 'b>;
180180
type SerializeTuple = SerializeSeq<'a, 'b>;
181-
type SerializeTupleStruct = Unreachable;
181+
type SerializeTupleStruct = SerializeSeq<'a, 'b>;
182182
type SerializeTupleVariant = Unreachable;
183183
type SerializeMap = SerializeMap<'a, 'b>;
184184
type SerializeStruct = SerializeStruct<'a, 'b>;
@@ -385,9 +385,9 @@ impl<'a, 'b: 'a> ser::Serializer for &'a mut Serializer<'b> {
385385
fn serialize_tuple_struct(
386386
self,
387387
_name: &'static str,
388-
_len: usize,
388+
len: usize,
389389
) -> Result<Self::SerializeTupleStruct> {
390-
unreachable!()
390+
self.serialize_seq(Some(len))
391391
}
392392

393393
fn serialize_tuple_variant(
@@ -822,6 +822,32 @@ mod tests {
822822
);
823823
}
824824

825+
#[test]
826+
fn test_tuple_struct() {
827+
#[derive(Serialize)]
828+
struct A<'a>(u32, Option<&'a str>, u16, bool);
829+
830+
let a = A(42, Some("A string"), 720, false);
831+
832+
assert_eq!(
833+
&*crate::to_string::<_, N>(&a).unwrap(),
834+
r#"[42,"A string",720,false]"#
835+
);
836+
}
837+
838+
#[test]
839+
fn test_tuple_struct_roundtrip() {
840+
use serde_derive::Deserialize;
841+
842+
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
843+
struct A<'a>(u32, Option<&'a str>, u16, bool);
844+
845+
let a1 = A(42, Some("A string"), 720, false);
846+
let serialized = crate::to_string::<_, N>(&a1).unwrap();
847+
let (a2, _size): (A<'_>, usize) = crate::from_str(&serialized).unwrap();
848+
assert_eq!(a1, a2);
849+
}
850+
825851
#[test]
826852
fn test_serialize_bytes() {
827853
use core::fmt::Write;

src/ser/seq.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@ impl<'a, 'b: 'a> ser::SerializeTuple for SerializeSeq<'a, 'b> {
5151
ser::SerializeSeq::end(self)
5252
}
5353
}
54+
55+
impl<'a, 'b: 'a> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> {
56+
type Ok = ();
57+
type Error = Error;
58+
59+
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
60+
where
61+
T: ser::Serialize,
62+
{
63+
ser::SerializeSeq::serialize_element(self, value)
64+
}
65+
66+
fn end(self) -> Result<Self::Ok> {
67+
ser::SerializeSeq::end(self)
68+
}
69+
}

0 commit comments

Comments
 (0)