Converting ReadOnlySequence of bytes to object
I have to convert a
ReadOnlySequence<byte>
to an object, the way I'm doing it currently is like so:
Is there a better way to handle this? BitConverter doesn't support ReadOnlySequence, so I would have to convert it to a byte array which is not ideal. I also have tons of properties on this model so it is a ton of code. Thanks in advance!29 Replies
You want to use
SequenceReader<byte>
instead of BitConverter
Here's a small example of reading two integers from a ReadOnlySequence<byte>
without unnecessary allocations.If I have a
string
type, how would I read that out?
Do I just need to keep looping to read all of the properties
I'm just a bit confused on how to parse the ReadOnlySequence<byte>
into the model itselfWell, hand de-serializing a raw byte sequence is not exactly a simple beginners exercise...why are you having to do that in the first place, as opposed to using an off-the-shelf serializer?
Strings can be tricky because you have to also encode the length somewhere unless it's a fixed length.
Assuming we have the easy task of just having a single string at the end, here is an example of reading a string from the byte sequence.
There is a single string, but I also have float and booleans as well
Do you know of a good off the shelf serializer I can use?
I've tried
MarshalAs
but I can't skip bytes that I don't need
This for example
I have to use a "filler" string for the first 8 bytes, because I want to skip themAre you passing this to C code or something?
By off the shelf serializer I meant something like System.Text.Json
No I'm just trying to serialize that
ReadOnlySequence<byte>
into that StandControllerDataModel
I was wondering why you are using a ReadOnlySequence<byte> in the first place.
Well I'm receiving it from tcp
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
Using
pipe.Reader.ReadAsync
which gives me that ReadOnlySequence<byte>
I guess I'm wondering why on the side where the data is originating you don't just serialize to something simple like json - now you have a stream of text you can send over the wire (using your tcp implementation) and then on the other side you just deserialize that stream back into your original object.
Instead of writing your own completely custom binary serialization mechanism.
Also, did you consider just using a standard implementation like gRPC rather than rolling your own?
Serialize the byte array to json?
How would I do that
I guess I'm just trying to figure out how I can serialize that byte array into a model using any method possible
No, I mean instead of a byte array
Who creates the byte array?
Have that code create json instead
Then convert that to bytes to send over the wire
Then it's just simple text.
That you can trivially deserialize without writing a bunch of custom code.
I don't have control over the device that is sending the messages
That's highly unfortunate
Yes, so in my situation, what do you recommend I do
Use SequenceReader and Encoding to read the data as I illustrated.
If you know all of the fields and they have fixed sizes it shouldn't be too bad.
The SequenceReader advances on each read so you don't have to do all that manual offset arithmetic.
Ok, does that have a way to read in bools and floats or should I code out a way to figure out the type
Only have strings, floats, bools, and ints
I'm pretty sure it supports all of the primitive types
Er
For bool you probably need to read it as a byte and then convert it to bool
But that's trivial
You might need to do something a little different for float
I'll figure something out, thanks for the help
@mtreit isn't that part of the stuff introduced for Pipelines?
This is how you can read a float:
I really need to use those at some point, it looks so neat
I'm not exactly sure
I also want to try out pipelines
yeah, the PipeReader returns a buffer as a ReadOnlySequence
We had someone take some code I wrote and re-write it to use pipelines and it completely regressed performance and I made him revert it. However, I believe he subsequently figured out he had a mistake in how he implemented it. I haven't circled back on it though.
To be honest I had never seen a ReadOnlySequence until @Determinism asked this question.
yeah, Pipelines isn't necessarily faster
it just makes faster, correct, code easier to write
SequenceReader.TryAdvanceToAny(ReadOnlySpan, Boolean) Method (Syste...
Searches for any of a number of specified delimiters and optionally advances past the first one to be found.
we should move to #chat