Coder Social home page Coder Social logo

Comments (4)

3Hren avatar 3Hren commented on July 21, 2024

I also suffer from new Rust serialize interface.

It would appear that the only option left is to re-implement msgpack::Decoder#decode_value() in terms > of the generic serialize::Decoder trait.

But how? By reading a single byte one-by-one via read_u8() and analyzing it with further decoding?
What if you actually need to access Decoder<R: Reader> impl (for example, you might want to cache something)?

from rust-msgpack.

drbawb avatar drbawb commented on July 21, 2024

In looking at rustc-serialize::json: they don't even implement Decodable for their enum Json which is the equivalent of our enum Value type.

Instead they have a Builder which takes a Reader and yields a Json enumeration. (This is all hidden behind some helper functions: from_str, from_reader, etc.)

Then their implementation of Decoder just runs through this parsed representation.
So in essence json::Builder does all the work of actually parsing and decoding Json.

impl serialize::Decoder for json::Decoder {} is just there to shove the pre-parsed Json value into the requested Rust type. Since json::Builder doesn't implement the serialize::Decoder interface directly it can do all the caching, etc. that it wants.

The unfortunate thing about this strategy is that it appears to require reading the stream to completion.

from rust-msgpack.

3Hren avatar 3Hren commented on July 21, 2024

The unfortunate thing about this strategy is that it appears to require reading the stream to completion.

This is bad for me, because I use msgpack in conjunction with TcpStream, which yields msgpack object every time it's ready.
Moreover, this is bad even for json, because many applications need to stream json'ed values through sockets.

from rust-msgpack.

drbawb avatar drbawb commented on July 21, 2024

It's worth noting the distinction: the json::Builder which can be constructed from a Reader is built on top of json::Parser which is a streaming parser. -- json::Builder calls #read_to_end() on the Reader so constructing it from a TcpStream would not be a good idea.

In practice I can't say I've ever done that. I usually pull TcpStream#read() into a buffer and then construct the entire Json value from that buffer. In my recent applications I'm using websockets; but any protocol w/ basic framing can be used to guarantee you've received the entire message before trying to deserialize it.

If you really need to do streaming (e.g: you're receiving fragments of a message) then you would have to use serialize::json::Parser directly. Again that's not a common use-case for me; especially considering std::io is synchronous.

from rust-msgpack.

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.