Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect serialization of bytes? #60

Open
berkowski opened this issue Jan 17, 2022 · 0 comments
Open

Incorrect serialization of bytes? #60

berkowski opened this issue Jan 17, 2022 · 0 comments

Comments

@berkowski
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant