Open dougbu opened 1 year ago
System.Runtime.Serialization.Json
is a super old NuGet package from the .NET Core 1.x era and does not need to be installed in modern .NET applications. Not that it would fix the bug, the inbox assemblies would still be used because they have a higher version.
System.Runtime.Serialization.Json
is a super old NuGet package from the .NET Core 1.x era and does not need to be installed in modern .NET applications. Not that it would fix the bug, the inbox assemblies would still be used because they have a higher version.
netstandard1.3
even in very new projectsSystem.Runtime.Serialization
assembly suffices.To help with triage, I quickly ran the repro on a few different runtimes... it seems this has been an issue going back to at least 4.8.
Description
If
DataContractJsonSerializer
is given a non-UTF8Stream
containing a byte order mark and not given a specific encoding, it will attempt auto-detection of the encoding. This eventually calls the code at https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs#L474-L503 and incorrectly "detects" UTF8, causing later decoding issues.Reproduction Steps
dotnet new console --output DCSTest
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
to an item group in the project filedotnet run
Expected behavior
Successful execution of the project.
Actual behavior
Program
throw
s:Regression?
Maybe. I haven't tested w/ older versions of the package. [Edit - @StephenMolloy] Looks like this has been an issue since at least 4.8, so not a regression.
Known Workarounds
Encoding.GetTranscodedStream
if targeting a recent-enough TFM (5.0 or later) instead of relying on the serializer to auto-detectJsonReaderWriterFactory
if targeting pretty much anything other than pre-netstandard2.0
TFMs and specify the encoding explicitlyMy core recommendation here is actually to remove
JsonEncodingStreamWrapper
and the XMLEncodingStreamWrapper
. UseTranscodingStream
under the covers. And detect the encoding (when necessary) some other way, perhaps using something bulletproof likeDetectEncoding()
inStreamReader
.That recommendation relates to my need to use DCS for both JSON and XML in
netstandard1.3
projects. In addition, this would remove unnecessary encoding restrictions, support non-UTF8 XML deserialization without the (silly?) XML declaration requirement, and simplify your code.Configuration
No response
Other information
No response