JSON: use Encoding.CreateTranscodingStream when reading JSON in materializer
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
The end result seems to be thing:
```c#
new MemoryStream(Encoding.UTF8.GetBytes(dataReader.GetFieldValue(0)))
```
Regardless of ADO.NET drivers where we can get UTF8 directly out (#31216), we can optimize this by using [Encoding.CreateTranscodingStream](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.createtranscodingstream?view=net-7.0#system-text-encoding-createtranscodingstream(system-io-stream-system-text-encoding-system-text-encoding-system-boolean)) over a simple Stream to read the string as raw bytes:
```c#
Span buffer = // buffer to read into
var jsonValue = "{}";
MemoryMarshal.Cast(jsonValue.AsSpan(0, Math.Min(jsonValue.Length, buffer.Length / 2))).CopyTo(buffer);
```
Thanks to @NinoFloris for pointing out all the above.
_Originally posted by @roji in https://github.com/dotnet/efcore/pull/31160#discussion_r1258471544_
Contributor guide
Assessment
This issue has not been assessed yet.