GithubHelp home page GithubHelp logo

Cannot deserialise into &[u8] about bytes HOT 2 CLOSED

serde-rs avatar serde-rs commented on August 10, 2024
Cannot deserialise into &[u8]

from bytes.

Comments (2)

1tgr avatar 1tgr commented on August 10, 2024

I have this workaround for deserializers that support visit_borrowed_bytes, ie formats where the bytes can be sourced directly from the data:

use std::fmt;
use std::marker::PhantomData;

use serde::de::{Deserializer, Error, Visitor};
use serde::ser::Serializer;

struct BorrowedBytesVisitor<T>(PhantomData<T>);

impl<'de, T> Visitor<'de> for BorrowedBytesVisitor<T> where &'de [u8]: Into<T> {
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a borrowed byte array")
    }

    #[inline]
    fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<T, E>
        where E: Error
    {
        Ok(v.into())
    }
}

pub fn serialize<S, T>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer, T: AsRef<[u8]>
{
    serializer.serialize_bytes(bytes.as_ref())
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
    where D: Deserializer<'de>, &'de [u8]: Into<T>
{
    Ok(deserializer.deserialize_bytes(BorrowedBytesVisitor(PhantomData))?.into())
}

In the general case I guess serde_bytes has to go via Vec<u8> to support formats like JSON where bytes must be parsed from a string or similar in the input data.

from bytes.

dtolnay avatar dtolnay commented on August 10, 2024

Fixed in 0.11.0 -- both of the following should now work:

#[derive(Serialize, Deserialize)]
struct TestSlice<'a> {
    #[serde(with = "serde_bytes")]
    a: &'a [u8]
}
#[derive(Serialize, Deserialize)]
struct TestBytes<'a> {
    #[serde(borrow)]
    a: &'a Bytes,
}

from bytes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.