neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.35k stars 266 forks source link

Read directly from PipeReader #171

Open Timovzl opened 4 years ago

Timovzl commented 4 years ago

87 asks for reading from Span<byte> and/or Memory<byte>. That's good to have.

However, to be truly efficient when deserializing incoming data, say, in a controller method, everyone will still be implementing the loop that reads from the PipeReader and passes the span to JsonDeserializer. While it is not hard to implement, it is still non-trivial.

Or worse, people will use PipeReader.AsStream() just to get an object that they can feed to JsonDeserializer. That little in-between will copy all the data over once more.

JsonDeserializer could handle the work efficiently once and for all by implementing an async PipeReader-based overload. It would read directly from the input spans, consuming the entire PipeReader. Easy. Optimally performant.

Come to think of it, there should be a Serialize() overload that writes to a PipeWriter as well, of course.