-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the field offset of UTF8Encoding.emitUtf8Identifier is not known b…
…ased on the runtime and process word size, scan for it automatically. Determine runtime using reflection instead of compile-time preprocessor directives, which had prevented this library from targeting .NET Standard and thus more runtimes than just .NET Core and Framework. Added workaround to fix crash in Utf8Json due to dependency on the original Encoding.UTF8.GetPreamble() on static type initialization.
- Loading branch information
Showing
11 changed files
with
204 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Due to [1], you may have to manually change the "Build Action" of this file to "C# analyzer additional file". | ||
# [1] https://github.com/dotnet/roslyn/issues/4655 | ||
|
||
# This file adjusts exception information used by Tetractic.CodeAnalysis.ExceptionAnalyzers. | ||
# Usage: <memberId>[ <accessor>] (-/+)<exceptionTypeId> | ||
# See ECMA-334, 5th Ed. § D.4.2 "ID string format" for a description of the ID format. | ||
|
||
P:System.Array.Length get -T:System.OverflowException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Bom.Squad; | ||
|
||
internal static class Workarounds { | ||
|
||
public static void ApplyWorkarounds() { | ||
FixUtf8Json(); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Utf8Json needs to read the UTF-8 BOM once when it first loads.</para> | ||
/// <para>To prevent it from crashing with an <see cref="IndexOutOfRangeException"/> inside <c>JsonSerializer.DeserializeAsync</c> and related methods, construct a throwaway <c>JsonReader</c> instance before disabling the BOM.</para> | ||
/// <para>https://www.nuget.org/packages/ZCS.Utf8Json</para> | ||
/// </summary> | ||
private static void FixUtf8Json() { | ||
try { | ||
Type.GetType("Utf8Json.JsonReader, Utf8Json")? | ||
.GetConstructor(new[] { typeof(byte[]) })? | ||
.Invoke(new object[] { new[] { (byte) '1', (byte) '1', (byte) '1' } }); | ||
} catch (Exception e) when (e is not OutOfMemoryException) { } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Bom.Squad; | ||
using FluentAssertions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
#if !NET452 | ||
using Utf8Json; | ||
#endif | ||
|
||
namespace Test; | ||
|
||
public class WorkaroundsTest: IDisposable { | ||
|
||
private static readonly Encoding Utf8 = new UTF8Encoding(false, true); | ||
|
||
public void Dispose() { | ||
BomSquad.RearmUtf8Bom(); | ||
} | ||
|
||
#if !NET452 | ||
[Fact] | ||
public async Task ZcsUtf8Json() { | ||
BomSquad.DefuseUtf8Bom(); | ||
|
||
MemoryStream stream = new(Utf8.GetBytes("""{"hello":"world"}""")); | ||
var deserialized = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream); | ||
deserialized["hello"].Should().Be("world"); | ||
} | ||
#endif | ||
|
||
} |