0

I have a json that I am deserializing by using NewtonSoftJson Json library as shown below:

public async Task InvokeAsync(HttpContext httpContext, ISchema schema)
{
    ...
    var request = Deserialize<GraphQLRequest>(httpContext.Request.Body);
    ....
}

public static T Deserialize<T>(Stream s)
{
    using (var reader = new StreamReader(s))
    using (var jsonReader = new JsonTextReader(reader))
    {
        var ser = new JsonSerializer();
        return ser.Deserialize<T>(jsonReader);
    }
}

But for this I have to enable this AllowSynchronousIO flag -

.UseKestrel(o =>
{
    o.AllowSynchronousIO = true;
})

I was reading this wiki as I am working with GraphQL and now I am trying to use System.Text.Json library to deserialize asynchronously as shown below -

public async Task InvokeAsync(HttpContext httpContext, ISchema schema)
{
    ...
    var request = await Deserialize<GraphQLRequest>(httpContext.Request.Body);
    ....
}

public static async Task<T> Deserialize<T>(Stream s)
{
  return await JsonSerializer.DeserializeAsync<T>(s, new JsonSerializerOptions
      {
          PropertyNameCaseInsensitive = true
      }
  );
}

Now if I hit below curl request then I am getting an error -

curl --location --request POST 'http://localhost:1338/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query ($clientId : Int!, $appId : String!){\n    # Write your query or mutation here\n\tclientNavigation(\n\t\tclientId: $clientId\n\t\tfilters: [{ key: \"o\", value: $appId }]\n\t\tcustomerKey: \"league\"\n\t) {\n\t\ttitle\n\t\ttype\n\t\tlinks {\n\t\t\ttext\n\t\t\ttrkId\n\t\t\thref\n\t\t\tproductCount\n\t\t\tresource\n\t\t}\n\t}\n}","variables":{"clientId":"1234","appId":"12"}}'

Error is -

The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.variables.siteId | LineNumber: 0 | BytePositionInLine: 380.

I am using 5.0.202 dotnet version. Any idea what is wrong here with System.Text.Json library and how can I fix this?

AndyP
  • 339
  • 3
  • 17
  • 3
    `System.Text.Json` cannot deserialize to `JToken` because the latter is from Json.NET. If you need that you must use a custom converter. See [How can I serialize a Newtonsoft JToken to JSON using System.Text.Json?](https://stackoverflow.com/q/65386083/3744182) for details on how. – dbc Sep 10 '21 at 20:01

0 Answers0