0

I have problem with serializing JSON to object with JsonArray field. Here's code I've used:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Nodes;

var input = "{ \"data\": [{ \"test\": \"123\" }] }";

var deserialized = JsonConvert.DeserializeObject<Input>(input, new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy()
    }
});

public class Input
{
    public JsonArray Data { get; set; }
}

And this is error I'm getting:

Newtonsoft.Json.JsonSerializationException: „Unable to find a constructor to use for type System.Text.Json.Nodes.JsonArray. Path 'data', line 1, position 11.”

KwarcPL
  • 67
  • 7

1 Answers1

1

Newtonsoft Json serializer does not know how to handle JsonArray from System.Text.Json namespace. Either declare data property as JArray, or use System.Text.Json.JsonSerializer.

Generally, use only one library to handle Json (either Newtonsoft.Json or System.Text.Json), do not mix them together.

pakeha_by
  • 2,081
  • 1
  • 15
  • 7