Would have preferred to add a few comments first, but I don't have the rep.
I tried the example you provided in a quick stub MsTest project, and I was unable to replicate the issue.
However, I'm guessing that there is more than one test method in the class that you're testing? (This is the bit I wanted to comment about to get more info)
I'd want to know the above since, if that's the case, and your project is setup run tests in parallel, I'm wondering if there could be an issue with the creation of the JsonSerializerOptions
since once the options are used it will have issues with adding another converter.
It might be worth trying to have the options as a field that you set during initialization as follows
using System.Text.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestJsonConvert;
[TestClass]
public class ObjectToInferredTypesConverterTests
{
private JsonSerializerOptions _options;
[TestInitialize]
public void Initialize()
{
_options = new JsonSerializerOptions();
_options.Converters.Add(new ObjectToInferredTypesConverter());
}
[TestMethod]
public void Read_TrueBoolean_ReturnsTrue()
{
string json = "true";
var result = JsonSerializer.Deserialize<object>(json, _options);
Assert.IsInstanceOfType(result, typeof(bool));
Assert.IsTrue((bool)result);
}
}
My csproj for the test is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<LangVersion>10</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1"/>
<PackageReference Include="MSTest.TestFramework" Version="3.1.1"/>
<PackageReference Include="System.Text.Json" Version="9.0.7" />
</ItemGroup>
</Project>
In the hopes that it can help with understanding/reproducing the results I received