0

I'm using Refit for making HTTP calls and it works as expected for request that expects straight-forward JSON structure. We've requirement now wherein the JSON request needs to be represented little differently like below.

Class (With below two properties)

Id,
Name

JSON structure expected:

{
   "Id" : {
      "iv" : "1234567"
   },
   "Name" : {
      "iv" : "Test"
    }
}

We've implemented a JsonConverter that takes care of this conversion. But when I pass in the converter to Refit like below, the class is not serialized as we expected.

var refitSettings = new RefitSettings {
    ContentSerializer = new NewtonsoftJsonContentSerializer(
        new JsonSerializerSettings {
            Converters = new List<JsonConverter> { new InvariantConverter() }
        })
};

services.AddRefitClient<ICustomerRefitClient>(refitSettings)
    .ConfigureHttpClient(x => {
        x.BaseAddress = <URL> ;
    });

It looks as if Refit is not aware of the converter that is configured.

I'm not sure what I'm missing here. Any help ?

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29

1 Answers1

0

Perhaps you should try this: it worked for me

var settings = new RefitSettings(new NewtonsoftJsonContentSerializer());
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
   Converters = { new InvariantConverter() }
};

and I decorated the property with the jsonconverterattribute

[JsonConverter(typeof(InvariantConverter))]
public Tag[] tags { get; set; }
Jacco M.
  • 31
  • 5