System.ArrayTypeMismatchException when using System.Text.Json with .NET Framework 4.7 in MSTest

undrgng 0 Reputation points
2025-07-22T12:06:58.8233333+00:00

Hi,

I'm currently using .NET Framework 4.7 along with MSTest (all packages are up to date). I'm trying to deserialize a JSON string using System.Text.Json as shown below:

using System.Text.Json;

private static JsonSerializerOptions GetOptions()
{
    var options = new JsonSerializerOptions();
    options.Converters.Add(new ObjectToInferredTypesConverter());
    return options;
}

[TestMethod]
public void example()
{
    string json = "true";
    var result = JsonSerializer.Deserialize<object>(json, GetOptions());
    Assert.IsInstanceOfType(result, typeof(bool));
    Assert.IsTrue((bool)result);
}

However, during the test execution, I consistently get the following error:

System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.

Interestingly, the same JSON works fine when I use Newtonsoft.Json:

using Newtonsoft.Json;

[TestMethod]
public void example()
{
    string json = "true";
    var result = JsonConvert.DeserializeObject<object>(json);
    Assert.IsInstanceOfType(result, typeof(bool));
    Assert.IsTrue((bool)result);
}

My question is: Is there a known solution or workaround for this issue when using System.Text.Json under .NET Framework 4.7? Or is it more reliable to stick with Newtonsoft.Json in this case?

Thanks in advance for any guidance!

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Adiba Khan 245 Reputation points Microsoft External Staff
    2025-07-23T08:12:39.3966667+00:00

    This System.ArrayTypeMismatchException is a known type of compatibility issue when using System.Text.Json with custom converters (like ObjectToInferredTypersConverter) on .NET framework 4.7x. This error is most Likely due to type inference and boxing/unboxing behaviour that System.Text.Json doesn't handle properly in legacy frameworks.

    1.      Use Newtonsoft.Json-

    You’re already doing this and it’s the best approach if you’re targeting .Net framework 4.7

    2.      Avoid objects and use strong typing with System.Text.Json

    If you must use System.Text.Json , avoid object deserialization with custom converters. Instead deserialize to a concrete type:

    string json = ”true”;

    bool result = JsonSerializer.Deserialize<bool>(json);

    Assert.IsTrue(result);

    This avoids the inference issue entirely.

    3.      Replace or Remove ObjectToInferredTypersConverter

    If you wrote or used a third party ObjectToInferredTypersConverter, it’s likely doing dynamic type inspection or unsafe casting. It may work fine on .Net core/.Net 6+ but break on .Net framework.

    You can either avoid using it entirely or rework it to avoid assumptions about object arrays and boxed values


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.