FLURL 4.0 use Newtonsoft JSON Serializer on a single request

60 Views Asked by At

Referencing Flurl Serializers it shows that Flurl 4.0 can use the Newtonsoft based serializer, but it is not clear to me how to do this for a single request. I need this to address a circular loop error in my request.

1

There are 1 best solutions below

4
azpc On

Here is sample code to do that

// this is needed when serializing classes to prevent circular loop error
var settings = new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
// create a Newtonsoft-based serializer for Flurl to use
var serializer = new Flurl.Http.Newtonsoft.NewtonsoftJsonSerializer(settings);

var result = await urlOrRequest
    // apply the serializer just to this request
    .WithSettings(settings => settings.JsonSerializer = serializer)
    .PostJsonAsync(data);