Skip to content

Commit

Permalink
JSON is now read with System.Text.Json instead of Newtonsoft.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLamansky committed Jul 13, 2024
1 parent cdf03b1 commit 1a2e0c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
78 changes: 40 additions & 38 deletions WebAssembly.Tests/Runtime/SpecTestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using JsonSubTypes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text.Json;
using System.Text.Json.Serialization;

// Effect of this is trusting that the source JSONs are valid.
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
Expand All @@ -30,37 +29,12 @@ public static void Run<TExports>(string pathBase, string json, Func<uint, bool>?
where TExports : class
{
TestInfo testInfo;
using (var reader = new StreamReader(Path.Combine(pathBase, json)))
using (var reader = File.OpenRead(Path.Combine(pathBase, json)))
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(Command), "type")
.RegisterSubtype(typeof(ModuleCommand), CommandType.module)
.RegisterSubtype(typeof(AssertReturn), CommandType.assert_return)
.RegisterSubtype(typeof(AssertReturnCanonicalNan), CommandType.assert_return_canonical_nan)
.RegisterSubtype(typeof(AssertReturnArithmeticNan), CommandType.assert_return_arithmetic_nan)
.RegisterSubtype(typeof(AssertInvalid), CommandType.assert_invalid)
.RegisterSubtype(typeof(AssertTrap), CommandType.assert_trap)
.RegisterSubtype(typeof(AssertMalformed), CommandType.assert_malformed)
.RegisterSubtype(typeof(AssertExhaustion), CommandType.assert_exhaustion)
.RegisterSubtype(typeof(AssertUnlinkable), CommandType.assert_unlinkable)
.RegisterSubtype(typeof(Register), CommandType.register)
.RegisterSubtype(typeof(AssertReturn), CommandType.action)
.RegisterSubtype(typeof(AssertUninstantiable), CommandType.assert_uninstantiable)
.Build());
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(TestAction), "type")
.RegisterSubtype(typeof(Invoke), TestActionType.invoke)
.RegisterSubtype(typeof(Get), TestActionType.get)
.Build());
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(TypedValue), "type")
.RegisterSubtype(typeof(Int32Value), RawValueType.i32)
.RegisterSubtype(typeof(Int64Value), RawValueType.i64)
.RegisterSubtype(typeof(Float32Value), RawValueType.f32)
.RegisterSubtype(typeof(Float64Value), RawValueType.f64)
.Build());
testInfo = (TestInfo)JsonSerializer.Create(settings).Deserialize(reader, typeof(TestInfo))!;
testInfo = JsonSerializer.Deserialize<TestInfo>(reader, new JsonSerializerOptions
{
IncludeFields = true,
})!;
}

ObjectMethods? methodsByName = null;
Expand Down Expand Up @@ -500,7 +474,7 @@ public ObjectMethods(object host)
}
}

[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter))]
enum CommandType
{
module,
Expand All @@ -524,7 +498,20 @@ class TestInfo
public Command[] commands;
}

abstract class Command
[JsonPolymorphic(TypeDiscriminatorPropertyName = nameof(type))]
[JsonDerivedType(typeof(ModuleCommand), typeDiscriminator: nameof(CommandType.module))]
[JsonDerivedType(typeof(AssertReturn), typeDiscriminator: nameof(CommandType.assert_return))]
[JsonDerivedType(typeof(AssertReturnCanonicalNan), typeDiscriminator: nameof(CommandType.assert_return_canonical_nan))]
[JsonDerivedType(typeof(AssertReturnArithmeticNan), typeDiscriminator: nameof(CommandType.assert_return_arithmetic_nan))]
[JsonDerivedType(typeof(AssertInvalid), typeDiscriminator: nameof(CommandType.assert_invalid))]
[JsonDerivedType(typeof(AssertTrap), typeDiscriminator: nameof(CommandType.assert_trap))]
[JsonDerivedType(typeof(AssertMalformed), typeDiscriminator: nameof(CommandType.assert_malformed))]
[JsonDerivedType(typeof(AssertExhaustion), typeDiscriminator: nameof(CommandType.assert_exhaustion))]
[JsonDerivedType(typeof(AssertUnlinkable), typeDiscriminator: nameof(CommandType.assert_unlinkable))]
[JsonDerivedType(typeof(Register), typeDiscriminator: nameof(CommandType.register))]
[JsonDerivedType(typeof(NoReturn), typeDiscriminator: nameof(CommandType.action))]
[JsonDerivedType(typeof(AssertUninstantiable), typeDiscriminator: nameof(CommandType.assert_uninstantiable))]
abstract class Command
{
public CommandType type;
public uint line;
Expand All @@ -540,7 +527,7 @@ class ModuleCommand : Command
public override string ToString() => $"{base.ToString()}: {filename}";
}

[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter))]
enum RawValueType
{
i32 = WebAssemblyValueType.Int32,
Expand All @@ -556,13 +543,19 @@ class TypeOnly
public override string ToString() => type.ToString();
}

[JsonPolymorphic(TypeDiscriminatorPropertyName = nameof(type))]
[JsonDerivedType(typeof(Int32Value), typeDiscriminator: nameof(RawValueType.i32))]
[JsonDerivedType(typeof(Int64Value), typeDiscriminator: nameof(RawValueType.i64))]
[JsonDerivedType(typeof(Float32Value), typeDiscriminator: nameof(RawValueType.f32))]
[JsonDerivedType(typeof(Float64Value), typeDiscriminator: nameof(RawValueType.f64))]
abstract class TypedValue : TypeOnly
{
public abstract object BoxedValue { get; }
}

class Int32Value : TypedValue
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public uint value;

public override object BoxedValue => (int)value;
Expand All @@ -572,6 +565,7 @@ class Int32Value : TypedValue

class Int64Value : TypedValue
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public ulong value;

public override object BoxedValue => (long)value;
Expand All @@ -597,13 +591,16 @@ class Float64Value : Int64Value
public override string ToString() => $"{type}: {BoxedValue}";
}

[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter))]
enum TestActionType
{
invoke,
get,
}

[JsonPolymorphic(TypeDiscriminatorPropertyName = nameof(type))]
[JsonDerivedType(typeof(Invoke), typeDiscriminator: nameof(TestActionType.invoke))]
[JsonDerivedType(typeof(Get), typeDiscriminator: nameof(TestActionType.get))]
abstract class TestAction
{
public TestActionType type;
Expand Down Expand Up @@ -647,6 +644,11 @@ class AssertReturn : AssertCommand
public override string ToString() => $"{base.ToString()} = [{string.Join(',', (IEnumerable<TypedValue>)expected)}]";
}

class NoReturn : AssertReturn
{
public override string ToString() => $"{base.ToString()}";
}

class AssertReturnCanonicalNan : AssertCommand
{
public TypeOnly[] expected;
Expand Down
2 changes: 0 additions & 2 deletions WebAssembly.Tests/WebAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 1a2e0c7

Please sign in to comment.