Skip to content

Incorrect serialization of bytes? #60

Open
@berkowski

Description

@berkowski

I'm extending serde-json-core for an embedded project to support pretty-formatted serialized strings, tuple variants, and struct variants. It's passing all of the existing tests, except for ser::tests::test_serialize_bytes:

# (Again, my code, not a test error with serde-json-core as-is)
thread 'ser::tests::test_serialize_bytes' panicked at 'assertion failed: `(left == right)`
  left: `"[49,46,53,54]"`,
 right: `"1.56"`', src/ser/mod.rs:1996:9

The current method in ser::Serializer just extends the underlying byte buffer:

    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok> {
        self.extend_from_slice(v)
    }

while the Serde guide serialize_bytes should produce an array:

    // Serialize a byte array as an array of bytes. Could also use a base64
    // string here. Binary formats will typically represent byte arrays more
    // compactly.
    fn serialize_bytes(self, v: &[u8]) -> Result<()> {
        use serde::ser::SerializeSeq;
        let mut seq = self.serialize_seq(Some(v.len()))?;
        for byte in v {
            seq.serialize_element(byte)?;
        }
        seq.end()
    }

serde-json also produces a sequence of bytes. It seems like the test_serialize_bytes test shouldn't be passing as it is currently in serde-json-core. Am I missing something about the implementation specific to serde-json-core?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions